Unix command line:
grep Results File.txt
Returns lots of lines:
Results = 1
Results = 11
Results = 2
etc, etc, etc.
Perl script:
#!/usr/bin/perl
open (IN, "< File.txt");
while ( grep /Results/, <IN> ) {
print "$_\n";
}
close (IN);
Returns nothing. Why?
Basically because you're not using grep correctly. You're using grep in
the condition to a while. That means you're using it in scalar context.
In a scalar context, grep returns the number of times its condition
statement (in this case: /Results/ ) returned true. $_ is never set
within the while loop.
What you want is to return the lines that grep matched correctly:
my @lines = grep /Results/, <IN>;
print @lines;
Or even just:
print grep /Results/, <IN>;
If you're going to use a while loop, you shouldn't be using grep:
while (<IN>){
print if /Results/;
}
Or you could just simplify this into a oneliner:
perl -ne 'print if /Results/' File.txt
By the way, I'm pretty sure if you had enabled warnings, you would have
gotten an uninitialize variable warning for the $_ inside the while loop.
That would have given you a clue as to what was going wrong. Please
enable warnings in the future before posting.
Paul Lalli