awk
Quick, line-oriented text processing. Awk reads records (lines), splits into fields, and runs actions on matches.
Basics
- Default field separator: whitespace. Fields:
$1,$2, …,$NF; whole line:$0 - Change field separator:
-F','orFS=","; output FS:OFS - Blocks:
BEGIN { init }pattern { action }END { finalize }
Common one-liners
- Print column 1 and 3, CSV input
awk -F, '{ print $1, $3 }' file.csv
- Filter rows matching regex and print 2nd field
awk '/ERROR/ { print $2 }' app.log
- Sum a numeric column and print total
awk '{ sum += $3 } END { print sum }' data.txt
- Set output delimiter
awk -F, 'BEGIN{OFS=","} { print $1, $2, $5 }' file.csv
- Replace value in a column (in-memory)
awk '$2=="staging" {$2="prod"} {print}' OFS='\t' input.tsv
- Header + computed column
awk 'BEGIN{OFS=","; print "name,size_kb"} {print $1, $2/1024}' sizes.txt
Patterns and conditions
- Range match (between markers)
awk '/BEGIN/,/END/' file
- Numeric condition
awk '$5 > 100' data.txt
- Multiple conditions
awk '$2=="ok" && $5>=200 {print $1,$5}' OFS="," file
Editing files in-place (GNU awk via shell)
- With temp file
awk '{gsub(/foo/,"bar"); print}' input > tmp && mv tmp input
Tips
- Use
-vto pass variables
awk -v thr=10 '$3>thr' file
- Use
-F'\t'for TSV; for multi-char FS:-F"[|]+" - For complex scripts, put in a
.awkfile and run:awk -f script.awk input