Bash Tutorial · Pipes and Text Processing · Lesson 11 of 30
Bash awk: Process Columns
Split lines into fields, filter rows and compute totals with awk.
Fields
awk splits each line into fields: $1, $2, ... and $0 for the whole line. NF is the number of fields and
NR the line number. -F, sets the field separator (here, a comma).
cat > users.csv <<'EOF'
id,name,country,orders
1,Ana,DE,12
2,Ben,US,3
3,Chen,DE,7
EOF
awk -F, 'NR > 1 { print $2 " lives in " $3 }' users.csv
awk -F, 'NR > 1 && $4 > 5 { print $2 }' users.csv
Output
Ana lives in DE Ben lives in US Chen lives in DE Ana Chen
Run this on your computer — see “Get Started” for setup.
Patterns, sums and END
awk 'condition { action }' runs the action for lines where the condition is true. END { ... } runs after the
last line, which is where totals are printed.
cat > sales.csv <<'EOF'
region,amount
north,120
south,80
north,40
west,200
EOF
awk -F, 'NR > 1 { total += $2; by[$1] += $2 } END { printf "total %d\n", total; for (r in by) print r, by[r] }' sales.csv | sort
Output
north 160 south 80 total 440 west 200
Run this on your computer — see “Get Started” for setup.
Test yourself
In awk, what is $3?
The third lineThe third field of the current lineThe number of fieldsA shell variable
Show answer
Answer: The third field of the current line