Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print filename and columns with awk

Tags:

awk

I would like to select the column 23,24 and 27 of the 8th row.

Here is the way I have tried.

awk 'FNR == 8 {print $23,$24,$37}' file.txt

It works perfectly. However, I'm wondering how to add the corresponding file name as the $0

Then, the output should look like

file.txt colunm23 colunm24 colunm 27     <---- The 8th row.    

I'm not using gawk. Please suggest based on the version before gawk 4.0.0 Thank you.

like image 737
user3631848 Avatar asked Dec 04 '16 08:12

user3631848


People also ask

What is awk '{ print $4 }'?

The AWK language is useful for manipulation of data files, text retrieval and processing. -F <value> - tells awk what field separator to use. In your case, -F: means that the separator is : (colon). '{print $4}' means print the fourth field (the fields being separated by : ).

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output.

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.


1 Answers

Use the FILENAME variable:

awk 'FNR == 8 {print FILENAME,$23,$24,$37}' file.txt
like image 108
user000001 Avatar answered Dec 27 '22 09:12

user000001