awk: The Linux Text Processing Tool Explained

The awk command splits each line of input into fields and lets you print or calculate with them. Its most common use is pulling out a column: awk '{print $1}' file prints the first word of every line. awk is the tool to reach for whenever a line of text has columns.

Print a column

Fields are numbered from 1: $1 is the first, $2 the second, and $0 is the whole line. The default separator is any run of spaces or tabs.

$ awk '{print $1}' names.txt
$ awk '{print $1, $3}' scores.txt

Change the field separator with -F

For comma- or colon-separated data, tell awk the separator with -F. Parsing /etc/passwd is the classic example.

$ awk -F: '{print $1}' /etc/passwd

Filter lines with a condition

Place a condition before the action. This prints the third field for every line whose second field is greater than 10.

$ awk '$2 > 10 {print $3}' data.txt

Count lines and sum numbers

awk does arithmetic as it reads. END runs after the last line, so you can total a column. This sums every line of a file:

$ awk '{sum += $2} END {print sum}' amounts.txt

Use awk with pipes

awk shines on command output. The classic combo with ps prints just the PID and command columns. For more text editing, see sed.

$ ps aux | awk '{print $2, $11}'

The awk reference page covers the full language.

Keep learning

Related guides: How to Use the sed Command for Text Editing · How to Search Files for Text with the Linux grep Command · Linux Redirection and Pipes: A Practical Guide. Read all tutorials on the Learn Linux blog.