awk basics

I’ve tinkered with awk and sed for quick one off tasks before in the past but I always have to go back and check the syntax for awk as it’s one of those things I use only once in a while.

Some quick notes for reference for later:

  • awk splits records in an input file by default by white space chars
  • uses $0 to refer to a whole line, and $1 … $n to refer to each matching token on a line
  • to split using column delimiters other than whitespace use -F

Examples:

Example file – example1.txt:

aaa bbb ccc
ddd eee fff

$ awk {'print $1'} example1.txt

Will print the first matching column:

aaa
ddd

If file has other column delimiters use -F to specify the delimiter, for example, example2.txt:

aaa,bbb,ccc
ddd,eee,fff

$ awk -F , {'print $2'} example2.txt

Will match column 2:

bbb
eee

More info on awk here.