Since awk and perl are closely related ...
Perl equivalents of @ Dennis awk solutions:
To print the second line:
perl -ne 'print if $. == 2' file
To print the second field:
perl -lane 'print $F[1]' file
To print the third field of the fifth line:
perl -lane 'print $F[2] if $. == 5' file
Perl equivalent of @Glenn solution:
Printing the j-th field of the i-th row
perl -lanse 'print $F[$j-1] if $. == $i' -- -i=5 -j=3 file
@Hai Pearl Equivalents:
if you are looking for a second column containing abc:
perl -lane 'print if $F[1] =~ /abc/' foo
... and if you want to print only a specific column:
perl -lane 'print $F[2] if $F[1] =~ /abc/' foo
... and for a specific line number:
perl -lane 'print $F[2] if $F[1] =~ /abc/ && $. == 5' foo
-l removes newline characters and adds them back when printing -a autosplits the input line to the @F array, using a space as a separator -n loop over each line of the input file
-e execute code inside quotes
$F[1] is the second element of the array, since Perl starts at 0
$. - line number
Chris Koknat Nov 03 '15 at 0:03 2015-11-03 00:03
source share