[EMAIL PROTECTED] wrote:
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

#!/usr/bin/perl
$\ = "\n";
while ( <> ) {
    my @F = split;
    print $F[2] if /searchterm/;
    }
__END__



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to