Parse Text File and Output to File

Discussion in 'Perl Misc' started by John M. Lembo, Aug 1, 2003.

  1. I am using Perl to parse a text file and output to another file. The text
    file has data on Unix virtual memory (vmstat) and I want to delete lines
    that I don't want and output lines that I want to a new file. The script I
    have is giving me blank lines in the new file.

    #!/usr/local/bin/perl
    #program to read systemdata file
    #and write to systemstats file
    #
    #
    $file = '/users/rit0/g3/jml9810/metrics/systemdata';
    open(INFO, $file); #opens file systemstats
    open(DATA, ">systemstats");#file to write data to
    @lines = <INFO>; #assigns lines to array

    foreach $line (@lines) #go through each line in file
    {
    if ($line ^V)
    {
    $line =~ s/$line//;
    }

    DATA == $line;
    print DATA "\n";
    }

    close(INFO); #closes file
    close(DATA);

    The data I am reading is vmstat as follows:

    0308011610
    Virtual Memory Statistics: (pagesize = 8192)
    procs memory pages intr cpu
    r w u act free wire fault cow zero react pin pout in sy cs us sy
    id
    5 483 41 113K 43K 34K 958M 15M 36M 48K 17M 3716 88 1K 914 2 5
    93
    === end at Fri Aug 1 12:10:01 EDT 2003 ===
    0308011615
    Virtual Memory Statistics: (pagesize = 8192)
    procs memory pages intr cpu
    r w u act free wire fault cow zero react pin pout in sy cs us sy
    id
    5 496 35 114K 43K 33K 958M 15M 36M 48K 17M 3716 88 1K 914 2 5
    93
    === end at Fri Aug 1 12:15:01 EDT 2003 ===

    I want to output the date and memory data only to a new file and delete the
    other lines. Any help is appreciated.

    --

    Regards,

    John M. Lembo
    (585) 594-1753 (ET)
    (585) 594-4028 Fax
    (Alt. email)
     
    John M. Lembo, Aug 1, 2003
    #1
    1. Advertising

  2. "John M. Lembo" <> writes:

    > I am using Perl to parse a text file and output to another file. The text
    > file has data on Unix virtual memory (vmstat) and I want to delete lines
    > that I don't want and output lines that I want to a new file. The script I
    > have is giving me blank lines in the new file.


    This is FAQ: How do I [...] delete a line in a file [...]?

    Please check the FAQ before you post.


    > #!/usr/local/bin/perl
    > #program to read systemdata file
    > #and write to systemstats file


    Try to get into the habit of writing your code in a fashion consitant
    with strict and warnings then put "use strict" and "use warnings" at
    the top of your script. If you stick with Perl sooner or later you'll
    start doing this[1]. The longer the you leave it the more you will
    regret it.

    > #
    > #
    > $file = '/users/rit0/g3/jml9810/metrics/systemdata';
    > open(INFO, $file); #opens file systemstats


    Always check that open() succeded. At the very least put "or die $!".

    > open(DATA, ">systemstats");#file to write data to


    The file handle DATA is special. You _can_ treat it as a normal
    handle but it will confuse people who are familar with Perl.

    Comments should add something to the program. Don't bother with
    comments that just add noise. If a statement is opviously opening a
    file for output it is redundant to have a comment that say so too.


    > @lines = <INFO>; #assigns lines to array


    Why do you slurp?

    >
    > foreach $line (@lines) #go through each line in file
    > {
    > if ($line ^V)


    What do to think that does and why?

    What is actually does is is construct a string that is the same as
    $line except with bits 3,4 and 5 of the first character inverted. It
    then tests if that string is true. It will always be true.

    > {
    > $line =~ s/$line//;


    What do to think that does and why?

    What it actually does is takes the contents of $line and compiles it
    as a regular expression. It then takes $line again and replaces the
    first match with a null string. For values of $line that contain no
    regex metacharacters this is equivalent to

    $line = '';

    For most other values of $line this is either an error or a no-op.

    For a few value of $line is will do some really weird and worderfull
    things.

    > }
    >
    > DATA == $line;


    What do you think that does and why?

    What is actually does is convert the value of $line into a number and
    tests to see if that number is with the same as the numeric value of
    the string 'DATA' (i.e. zero). It then throws away the result of this
    comparison.

    If you'd had warnings and strictures enabled Perl would have had a lot
    to say about that line.

    > print DATA "\n";


    So the only thing you are printing is a file full of blank lines!

    > }
    >
    > close(INFO); #closes file


    Comments should add something to the program. Don't bother with
    comments that just add noise.

    > close(DATA);


    Note:

    [1] Not all perl users eventually come arround to using strict and
    warnings. Some turn into bitter and twisted trolls.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Aug 1, 2003
    #2
    1. Advertising

  3. John M. Lembo

    JS Bangs Guest

    John M. Lembo sikyal:

    > I am using Perl to parse a text file and output to another file. The text
    > file has data on Unix virtual memory (vmstat) and I want to delete lines
    > that I don't want and output lines that I want to a new file. The script I
    > have is giving me blank lines in the new file.


    Brian did a pretty good job of ripping your existing script to shreds, so
    I won't repeat his wonderful work :). Instead, I'll provide an example of
    a reasonably quick way to do what you want. This could easily be reduced
    to a one-liner with a little more effort.

    open INFO, '</users/rit0/g3/jml19810/metrics/systemdata';
    open OUT '>systemstats';

    print OUT grep { foo($_) } <INFO>;

    foo() should be a function that returns true for the lines you want to
    keep, otherwise false. Implementing foo() is left as a problem to the
    reader. (It needn't be an actual sub, as an inline regex should do just as
    well.)


    --
    Jesse S. Bangs
    http://students.washington.edu/jaspax/
    http://students.washington.edu/jaspax/blog

    Jesus asked them, "Who do you say that I am?"

    And they answered, "You are the eschatological manifestation of the ground
    of our being, the kerygma in which we find the ultimate meaning of our
    interpersonal relationship."

    And Jesus said, "What?"
     
    JS Bangs, Aug 1, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl
    Replies:
    0
    Views:
    12,717
    John M. Lembo
    Aug 1, 2003
  2. chuck amadi
    Replies:
    11
    Views:
    668
    Cameron Laird
    Jun 16, 2004
  3. chuck amadi
    Replies:
    1
    Views:
    491
    Larry Bates
    Jun 23, 2004
  4. Replies:
    19
    Views:
    1,131
    Daniel Vallstrom
    Mar 15, 2005
  5. Jack
    Replies:
    2
    Views:
    192
    Dave Weaver
    Jan 7, 2008
Loading...

Share This Page