What is the easiest method to search in a file for a particular term,
and output a desired field.
For example, in awk, I would simply do:

awk '/searchterm/ {print $2}' input.txt

to get my result.

But in Perl, the shortest way I could find achieve the same result
was:

$inputfile=input.txt;
open(IFILE, $inputfile) or die "Could not open $inputfile\n";
map { if ( /searchterm/ ) {$result=substr($_, 17, 6);}  } <IFILE>;
print "$result\n";
close IFILE;

It just seems like a lot of code to do such a simple task.

This one-liner does pretty much what I would want but I do not know
how to convert it to a script.

perl -lane 'print $F[2] if /searchterm/' input.txt

Any help would be much appreciated.
Thank you.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to