Basic syntax

awk [ options ] script filename

Where script is either a single awk command or file of awk commands. Awk commands look like this:

TODO

and filename can be omitted if you’re piping in standard input.

Commands

Options

Examples

Length of the longest line in a file

awk '{ if (length($0) > max) max = length($0) }; END { print max }' file

Selecting, filtering, and basic arithmetic

Sum a field, specify delimiter

awk -F '\t' '{print sum+=$1;} END {print sum}' file.txt

will print the sum of column one ($1) of a tab-separated (\t) file (file.txt).

awk -F ',' '$1 ~ /smiths/ {sum += $3} END {print sum}' file.txt

sums values in column three where column 1 equals smiths in a comma-separated file.

Using named columns is a bit more difficult. The following snippet lets you do that. Just replace the second command ({print $(f["foo"])... with the command you wish to execute. I haven’t tried this with large files, but I imagine it can get slow.

awk 'NR==1 {for (i=1; i<=NF; i++) {f[$i] = i}} {print $(f["foo"]), $(f["baz"])}' file

String substitution

Replace all instances of "-" in column 54 of a comma-separated file and print new file to stdout:

awk '{ FS=","; OFS=","; gsub("-", "", $54); print }' file.csv > new_file.csv