Cheatsheet: awk
Last updated 2026-06-21
Printing fields
Print the first column of a whitespace-separated file.
Print the second and third columns separated by the output field separator.
Print the entire current record.
Print the last field on each line using NF.
Print line number and line text using NR.
Fields and separators
Use a comma as the input field separator for CSV-like data.
Use a regular expression field separator such as colon or comma.
Set output field separator to a comma in a BEGIN block.
Print the number of fields in each line using NF.
Skip a header row by starting after NR greater than 1.
Patterns and filters
Print all lines that contain a specific regular expression.
Print rows where the first field is greater than 50.
Print rows matching multiple conditions with logical AND.
Print rows matching either condition with logical OR.
Print lines between two matching patterns, inclusive.
Use next to skip comments and blank lines before other actions.
Summaries and formatting
Print the sum of the first column.
Print the average of the first column.
Print the maximum value of the first column.
Print the minimum value of the first column.
Count records by a field value and print totals at the end.
Use printf for aligned columns and numeric precision.
Run setup and final reporting with BEGIN and END blocks.
See also: