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.
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.
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.
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:
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.
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.