The awk command is a command line tool for processing and analyzing text that works by lines and columns. It is essentially a small program that reads data, selects parts of lines, operates on them and prints the result. It shines brightest when processing tables, logs and CSV files.
awk '{ print $1 }' file.txt
What Is awk
The name awk comes from the surnames of its three authors: Aho, Weinberger and Kernighan. It splits each line of input text into fields (columns) separated by spaces. Fields are accessible as variables $1 through $NF, and the whole line as $0.
Printing Columns
The most common use of awk is printing a specific column from text. Column 1 is $1, the last column is $NF (number of fields).
cat names.txt
# output: Ana Smith 34
awk '{ print $2 }' names.txt
# output: Smith
awk '{ print $NF }' names.txt
# output: 34
Matching Patterns
A condition or pattern can be placed before the block of commands. Commands are executed only on lines that match the pattern.
awk '/error/ { print }' log.txt # prints lines containing the word error
awk '$3 > 30 { print $1, $3 }' names.txt # lines where column 3 is greater than 30
Changing Field Separator
The default field separator is a space. For processing CSV files we change it with the -F flag.
awk -F ';' '{ print $2 }' data.csv # fields separated by semicolon
Calculations and Sums
awk can also do arithmetic and sum up all lines. The variable NF is the number of fields in a line, NR is the current line number.
awk '{ sum += $3 } END { print sum }' names.txt # sums column 3
awk 'END { print NR }' file.txt # prints the number of lines
These basic commands cover most everyday text processing tasks — printing columns, filtering by pattern and simple calculations. Anyone who wants to dig deeper will discover that awk can do much more.