Linux awk Command Examples

awk is a powerful text-processing language that excels at field-based data extraction and reporting. It reads input line by line, splitting each line into fields, making it ideal for processing CSV files, log files, and tabular data directly from the command line without writing a full script.

LinuxawkCLIText Processing
Field Extraction
Print the first column
awk '{print $1}' filename.txt
By default awk splits each line on whitespace. $1 is the first field, $2 the second, and $0 is the entire line. Extracting specific columns from command output like ps aux or ls -l is one of the most common daily uses.
Print multiple specific columns
awk '{print $1, $3}' filename.txt
Comma-separated field numbers print multiple columns with a space between them. Set OFS="," to produce CSV output, or OFS="\t" for tab-separated values.
Set a custom input delimiter
awk -F',' '{print $2}' data.csv
The -F flag sets the input field separator. This processes CSV files by splitting on commas. Use -F':' for /etc/passwd, -F'\t' for TSV, or any regex pattern as the delimiter.
Print the last field of each line
awk '{print $NF}' filename.txt
NF is the built-in variable holding the count of fields on the current line. Using $NF as a field index gives the last field regardless of how many columns the line has.
Filtering
Print lines matching a pattern
awk '/ERROR/ {print}' logfile.txt
A pattern before the action block acts as a filter. This prints all lines containing "ERROR", similar to grep but with the added power to simultaneously process field values on matched lines.
Filter by numeric field value
awk '$3 > 100 {print $1, $3}' data.txt
Compare field values numerically or as strings in the condition. This prints columns 1 and 3 only where column 3 exceeds 100 — useful for filtering logs by response time, file size, or any numeric metric.
Print with line numbers
awk '{print NR, $0}' filename.txt
NR (Number of Records) is the current line counter starting at 1. Prepending it adds line numbers to output, useful for cross-referencing results with source files or debugging scripts.
Aggregation
Sum a numeric column
awk '{sum += $1} END {print "Total:", sum}' numbers.txt
The END block runs after all lines are processed, printing the accumulated total. This one-liner sums an entire column of numbers in a single pass — much faster than importing into a spreadsheet.
Count lines matching a pattern
awk '/ERROR/ {count++} END {print count}' logfile.txt
Increment a counter for each match, then print the total in END. This is faster than grep pattern | wc -l for counting occurrences because it does the work in a single pass through the file.
Calculate column average
awk '{sum += $1; count++} END {print "Avg:", sum/count}' data.txt
Track both sum and count to compute an average in a single pass. The END block performs the division after all lines are read. This pattern extends easily to min, max, or any statistical calculation on a column.

How to Use

  1. Open your terminal — awk is pre-installed on Linux and macOS.
  2. Use awk '{print $1}' file to extract the first column of any whitespace-separated output.
  3. Set a custom delimiter with -F',' for CSV or -F':' for /etc/passwd-style files.
  4. Add a pattern before the action block, e.g. /ERROR/ {print}, to filter lines.
  5. Use BEGIN{} for setup and END{} for summary output like totals and averages.

Frequently Asked Questions

When should I use awk instead of sed?

Use awk when you need to work with specific columns or perform calculations on field values. Use sed for simple line-by-line substitutions and deletions. awk is better for structured tabular data while sed excels at regex-based text transformations.

What is the difference between NR and NF?

NR is the current line number (Number of Records). NF is the count of fields on the current line (Number of Fields). Using $NF as a field index accesses the last field on a line regardless of its position.

Can awk process multiple files at once?

Yes, awk accepts multiple filenames and processes them sequentially. NR keeps incrementing across files. Use FNR instead if you need the line number to reset at the start of each new input file.

Related Tools