Newbie trying to cleanup/format text file

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Newbie trying to cleanup/format text file

    Hi all:

    I'm trying to cleanup and format this text file of user names, so that I
    have one column of user names. Here's the text file:

    The request will be processed at a domain controller for domain FOOBAR.

    Group name Misc
    Comment These are the FOOBAR users

    Members

    -------------------------------------------------------------------------------
    arod cschilling csenior
    ecostello ffrank gbennett
    LBird PMartinez
    The command completed successfully.

    I would like an output file to read like this:

    arod
    cschilling
    csenior
    ecostello
    ffrank
    gbennett
    LBird
    PMartinez

    I know how to put the names into one column, if I don't have all the extra
    lines. So, I've been trying to figure out how to eliminate all lines
    except for the names.

    I tried opening the file, then using a "not match" regex to eliminate all
    lines except the name lines. So far, I can't seem to eliminate more than
    one line of "not match". Any help will be appreciated. Here's my feeble
    newbie code. This code will eliminate the "Group" line, but that's it. I
    tried adding other conditionals to eliminate other unwanted lines, but
    they don't work.

    open(FH, "c:\\foobar.txt") or die "sourcefile open failed - $!";
    while (<FH>){
    if (not $_ =~ /^Group/) {
    print $_ ;
    }
    }
    Stuart Clemons Guest

  2. Similar Questions and Discussions

    1. Text Spacing cleanup?
      Does anyone know of an easy way to clean up sloppy text for DW? I pull text from Outlook, PDF's, and Docs all the time, and these seems to be no...
    2. problem using the text cleanup script
      I am trying to use the cleanup script to get rid of double returns and double spacing, but on running the script I get a prompt asking me to 'select...
    3. Newbie with 2 questions about vector format and text/outlines
      Hi. I normally do very dense and long manuscript layout using Adobe InDesign. I have been asked to create a simple t-shirt logo and design (something...
    4. newbie q: External text file
      hi, I need to import a text file, so that it will read the external file rather then view it in director, so that when i make the projector i can...
    5. format a retrieved text file
      "John" wrote ... Hi John, I've often wanted to know how to do this also, so cheers for the code, might come in handy... With regards to...
  3. #2

    Default Re: Newbie trying to cleanup/format text file

    [email]stuart_clemons@us.ibm.com[/email] wrote:
    > I kept working at it and was finally able to get rid of the unwanted lines
    > in the text file using a series of nested if's. It's certainly not
    > elegant, but it works !
    >
    > Surely there's a more efficient way to get rid of the unwanted lines.
    Definitely!
    >
    >
    > Anyway, here's what I came up with for now. This leaves the names in
    > one, two, or three columns, but I know how to reformat them into one name
    > per column.
    >
    > #!/usr/bin/perl -w
    > use strict;
    > $_ = "";
    > open(INFILE, "c:\\foobar.txt")or die "sourcefile open failed - $!";
    >
    > while (<INFILE>){
    > if (not $_ =~ /^The request/){
    > if (not $_ =~ /^Group/){
    > if (not $_ =~ /^Comment/){
    > if (not $_ =~ /^Members/){
    > if (not $_ =~ /^------/){
    > if (not $_ =~ /^The command/){
    > if ($_ =~ /\S/){
    > print $_ ;
    > }
    > }
    > }
    > }
    > }
    > }
    > }
    > }
    > close
    Greetings! E:\d_drive\perlStuff>perl -w -Mstrict
    while (<DATA>) {
    last if /^\-\-\-\-\-/; #should be enough to identify the line;
    }

    my @just_usernames;
    while (<DATA>) {
    last if /^The command/;
    chomp;
    s/^\s+//;
    s/\s+$//;
    my @tokens = split /\s+/, $_;
    push @just_usernames, "$_\n" foreach @tokens;
    }
    print for @just_usernames;

    __DATA__
    Group name Misc
    Comment These are the FOOBAR users

    Members

    -------------------------------------------------------------------
    arod cschilling csenior
    ecostello ffrank gbennett
    LBird PMartinez
    The command completed successfully.
    arod
    cschilling
    csenior
    ecostello
    ffrank
    gbennett
    LBird
    PMartinez

    R. Joseph Newton Guest

  4. #3

    Default Re: Newbie trying to cleanup/format text file

    "John W. Krahn" wrote:
    >
    > Stuart Clemons wrote:
    > >
    > > I'm trying to cleanup and format this text file of user names, so that I
    > > have one column of user names. Here's the text file:
    > >
    > > The request will be processed at a domain controller for domain FOOBAR.
    > >
    > > Group name Misc
    > > Comment These are the FOOBAR users
    > >
    > > Members
    > >
    > > -------------------------------------------------------------------------------
    > > arod cschilling csenior
    > > ecostello ffrank gbennett
    > > LBird PMartinez
    > > The command completed successfully.
    >
    > open FH, 'c:/foobar.txt' or die "sourcefile open failed - $!";
    > while ( <FH> ) {
    > next if 1 .. /^-+$/;
    > last if /^The command completed successfully/;
    > print "$_\n" for /\S+/g;
    > }
    Someone (off-list) asked for an explanation of this code.

    while ( <FH> ) {

    This reads the current line from the filehandle FH into the $_ variable
    and sets the $. variable to the current line number.

    next if 1 .. /^-+$/;

    Using a constant integer with the range operator in a while loop is a
    shortcut that compares $. to the constant so the range is everything
    from the first line to the line that contains only hyphens. next sends
    execution back to the beginning of the loop.

    last if /^The command completed successfully/;

    last exits the loop if the current line starts with the text 'The
    command completed successfully'.

    print "$_\n" for /\S+/g;

    The regex in list context creates a list of all the non-whitespace
    strings from the current line and each member of the list is printed out
    with a trailing newline. You could also create the list with the split
    function:

    print "$_\n" for split;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139