awk
15 commands
15 shown
Print specific column
Print the 3rd whitespace-delimited field
awk '{print $3}' file.txt
Custom field separator
Use colon as field separator, print user and UID
awk -F: '{print $1, $3}' /etc/passwd
Filter rows by pattern
Print line number and content for lines matching ERROR
awk '/ERROR/ {print NR": "$0}' app.log
Sum a column
Accumulate a numeric column and print total
awk '{sum += $4} END {print sum}' access.log
Print lines between two patterns
Range pattern: include lines from START to END inclusive
awk '/START/,/END/' file.txt
Skip header line
Process all lines except the first (header)
awk 'NR>1 {print $1, $2}' data.csv
Count occurrences per key
Frequency count of the first field (e.g. IP addresses)
awk '{count[$1]++} END {for (k in count) print count[k], k}' access.log | sort -rn
Print last field
NF holds the number of fields; $NF is the last field
awk '{print $NF}' file.txt
Multi-condition filter
Print lines where field 2 > 500 AND field 5 is POST
awk '$2 > 500 && $5 == "POST"' access.log
Reformat output
Aligned printf formatting with fixed column widths
awk '{printf "%-20s %5d\n", $1, $2}' data.txt
Run awk script from file
Load awk program from an external script file
awk -f transform.awk input.txt
In-place-style transform with awk
Global substitution and write back to same file
awk '{gsub(/foo/, "bar"); print}' input.txt > tmp && mv tmp input.txt
Process CSV with quotes (gawk)
Parse quoted CSV fields correctly with FPAT in gawk
gawk -v FPAT='([^,]+)|("[^"]+")' '{print $2}' data.csv
BEGIN/END blocks
Run code before and after processing all lines
awk 'BEGIN{print "Start"} {lines++} END{print lines" lines processed"}' file.txt
Two-file join on key
Left-join data.txt with lookup.txt on first field
awk 'NR==FNR{a[$1]=$2; next} $1 in a {print $0, a[$1]}' lookup.txt data.txt
no commands match