You never actually print to anywhere, try opening another
file for output. I've rewritten your example above, call
this with
progname.pl datafile results
#!/usr/bin/perl -w
use strict;
my($in, $out, @rest) = @ARGV;
open (INFILE, "<$in") or die "can't open $in $!";
open (OUTFILE,">$out") or die "can't open $out $!";
while (<INFILE>)
{
print OUTFILE "$1 $2\n" if /^total.*(\d+)\s+(\d+)\s*$/;
}
close INFILE;
close OUTFILE;
Notice you can use the same regular expression to match
the line and extract the data, also the regular expression
is more general and
will catch lines that aren't formatted exactly as you
anticipated.
Update: I should probably explain the
regular expression, just in case you aren't totally familiar
with them. It matches any line that begins with total, has
anything at all in the middle, but ends with tw0 numbers
separated by whitespace. It also has optional whitespace
at the end (spaces at the ends of lines can be a real pain).
Note the use of + for items that definitely have to be there
and * for things taht are optional.
Nuance
|