Awk examples

Learning awk can be difficult but it is good to know some best practise with it.

Most used example is field separation. You can use any kind of string for separation, not just limited with single character:

awk -F "ip " {'print $2'}

You can even use some kind of regular expressions as field separators:

awk -F "[iI]p " {'print $2'}

Find device names “sd” or with major number 4 and device name “tty”. Print the record number NR, plus the major number and minor number:

awk '$2 == "sd"||$1 == 4 && $2 == "tty" { print NR,$1,$2}' /proc/devices

Find device name equal to “sound”.

awk '/sound/{print NR,$1,$2}' /proc/devices

Print the 5th record, first field, in file test:

awk 'NR==5{print $1}' test

Print a record, skip 4 records, print a record etc from file1:

awk '(NR-1) % 4 == 0 {print $1}' file1

Print all records except the last one from file1:

tac file1|awk 'NR > 1 {print $0}' | tac

Print A,B,C …Z on each line, cycling back to A if greater than 26 lines:

awk '{ print substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",(NR-1)%26+1,1),$0}' file1

Find number of bytes in a directory:

ls -l | awk 'BEGIN {c=0} { c+=$5} END {print c}'

Remove duplicate, nonconsecutive line. As an advantage over “sort|uniq” you can eliminate duplicate lines in an unsorted file:

awk '! a[$0]++' file1

Or the more efficient script:

awk '!($0 in a) {a[$0];print}' file1

Print only the lines in file1 that have 80 characters or more:

awk 'length < 80' file1

Print line number 25 on an extremely large file – note it has to be efficient and exit after printing line number 25:

awk 'NR==25 {print; exit}' verybigfile