Pages

AWK tutorial

Sed and AWK are powerful yet simple programming languages.

The simple format to use awk is

awk '{print}' fileName.txt

awk '{print $0 }' fileName.txt will print all the lines in the file
awk '{print $1 }' fineName.txt will display the first column
awk '{print $1, $2}' fineName.txt will display the first and the second column



To Understand NR and NF options, See this marks.txt

cat marks.txt gives

Kannan 12 34 56 78 43
Kamalan 76 23 56 12 90
Mukilan 41 76 34 12 34
Selvam 90 89 23 12 56
Pandi 56 78 87 23 12

In this, if you print

awk '{NR, "=====" NF}' marks.txt will display

1 ===== 43
2 ===== 90
3 ===== 34
4 ===== 56
5 ===== 12

From this we understand that the NF gives the last coloumn and the NR gives the line numbers.

Similarly,
awk '{NR, $1, "=====" NF}' marks.txt will display
1 Kannan ===== 43
2 Kamalan ===== 90
3 Mukilan ===== 34
4 Selvam ===== 56
5 Pandi ===== 12


No comments:

Post a Comment

You may be interested in these posts

Error in image file conversion: convert-im6.q16: not authorized `test.eps' @ error/constitute.c/WriteImage/1037.

This error is because of the vulnerability. This allows remote execution of code using image formats. So, some Linux distributions by defaul...