I learned that in awk, $2 is the 2nd column. How to specify the ith line and the element at the ith row and jth column?

share|improve this question
    
I added another example. – Dennis Williamson Oct 2 '09 at 0:10
up vote 102 down vote accepted

To print the second line:

awk 'FNR == 2 {print}'

To print the second field:

awk '{print $2}'

To print the third field of the fifth line:

awk 'FNR == 5 {print $3}'

Edit: Here's an example with a header line and (redundant) field descriptions:

awk 'BEGIN {print "Name\t\tAge"}  FNR == 5 {print "Name: "$3"\tAge: "$2}'

There are better ways to align columns than "\t\t" by the way.

share|improve this answer
    
Thanks! Is it possible to print the columns with a specific string? – Tim Oct 1 '09 at 23:57

To print the columns with a specific string, you use the // search pattern. For example, if you are looking for second columns that contains abc:

awk '$2 ~ /abc/'

... and if you want to print only a particular column:

awk '$2 ~ /abc/ { print $3 }'

... and for a particular line number:

awk '$2 ~ /abc/ && FNR == 5 { print $3 }'
share|improve this answer
    
Thanks for showing "~" usage – Vishnu Kumar Jan 27 '15 at 10:19

To expand on Dennis's answer, use awk's -v option to pass the i and j values:

# print the j'th field of the i'th line
awk -v i=5 -v j=3 'FNR == i {print $j}'
share|improve this answer
    
May we have an example of when using the -v option would be helpful? – Rani Kheir Jan 23 '17 at 18:28
1  
Here's one: stackoverflow.com/q/9418617/7552 – glenn jackman Jan 23 '17 at 18:41
1  
@dessert the j is filled in with the value 3, so when it does print $j it's doing print $3 which is printing the third column, so it is correct as it stands. – Davy M Dec 4 '17 at 17:02

Since awk and perl are closely related...


Perl equivalents of @Dennis's 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's solution:

Print the j'th field of the i'th line

perl -lanse 'print $F[$j-1] if $. == $i' -- -i=5 -j=3 file


Perl equivalents of @Hai's solutions:

if you are looking for second columns that contains abc:

perl -lane 'print if $F[1] =~ /abc/' foo

... and if you want to print only a particular column:

perl -lane 'print $F[2] if $F[1] =~ /abc/' foo

... and for a particular line number:

perl -lane 'print $F[2] if $F[1] =~ /abc/ && $. == 5' foo


-l removes newlines, and adds them back in when printing
-a autosplits the input line into array @F, using whitespace as the delimiter
-n loop over each line of the input file
-e execute the code within quotes
$F[1] is the second element of the array, since Perl starts at 0
$. is the line number

share|improve this answer

I found this working command

root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE\|TAG' |awk 'FNR == 1 {print $1}'

STATE

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.