Awk Processing in Shell

Level 4141 keystrokes

awk processes text line by line and splits each line into fields ($1, $2, ...). These three commands print selected CSV columns, sum a column, and count lines that contain "error".

Shell code

# -F',' splits on commas; print the 1st and 3rd fields of each line
1awk -F',' '{print $1, $3}' data.csv
# Add field 2 of every line to sum; END prints it after the last line
2awk '{sum += $2} END {print "Total:", sum}' numbers.txt
# Count lines matching /error/ and print the count at the end
3awk '/error/ {count++} END {print count}' log.txt

Output

Alice 30
Bob 25
Carol 35
Total: 150
3
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • '× 8
  • {× 5
  • }× 5
  • ,× 3
  • .× 3
  • +× 3
  • $× 3
  • "× 2
  • /× 2
  • -× 1

More Shell examples at level 4

← Shell typing practice