Script Valley
Linux & Bash for Developers
Text Processing & SearchingLesson 2.5

awk command tutorial for text processing

awk syntax, field separator, NR NF built-ins, print fields, conditionals in awk, BEGIN END blocks, sum and aggregate

awk: Process Structured Text Column by Column

awk treats each line of input as a record and splits it into fields by whitespace (or a delimiter you specify). It is the right tool when you need to extract specific columns, filter by field value, or compute aggregates from structured output.

Fields, NR, and NF

$1 is the first field, $2 the second, $0 is the entire line. NR is the current line number. NF is the number of fields on the current line.

# Print the second column of a file
awk '{print $2}' data.txt

# Use comma as field separator (CSV-like)
awk -F',' '{print $1, $3}' data.csv

# Print lines where third field is greater than 100
awk '$3 > 100' data.txt

# Print line numbers with content
awk '{print NR": "$0}' file.txt

# Sum values in column 2
awk '{sum += $2} END {print "Total:", sum}' sales.txt

# Print only lines 3 to 7
awk 'NR>=3 && NR<=7' file.txt

awk vs grep vs sed

Use grep when you need to find lines. Use sed when you need to replace text. Use awk when you need to work with columns or compute values. They are often combined in pipelines: grep ERROR app.log | awk '{print $1, $2, $NF}' extracts the timestamp and last field from error lines.