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.

Create a free account to save this page, take notes and track your progress. Sign in

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).

Fields and conditions
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.

Totals and grouping
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?

  1. The third line
  2. The third field of the current line
  3. The number of fields
  4. A shell variable
Show answer

Answer: The third field of the current line

Was this page helpful?