Parse Text File and Output to File

J

John M. Lembo

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
(e-mail address removed) (Alt. email)
 
B

Brian McCauley

John M. Lembo said:
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\\
 
J

JS Bangs

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 (e-mail address removed)
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?"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top