Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Parse Text File and Output to File

Reply
Thread Tools

Parse Text File and Output to File

 
 
John M. Lembo
Guest
Posts: n/a
 
      08-01-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alt. email)


 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-01-2003
"John M. Lembo" <(E-Mail Removed)> 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\\
 
Reply With Quote
 
 
 
 
JS Bangs
Guest
Posts: n/a
 
      08-01-2003
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 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?"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: text file search to text file output possible? Whiskers Computer Support 3 10-07-2006 06:32 PM
Re: text file search to text file output possible? Mitch Computer Support 0 10-06-2006 11:15 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
My simple script parse output screen and to a new file! chuck amadi Python 11 06-16-2004 10:10 PM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM



Advertisments