← Back to Bash Course | Chapter 10: Text Processing Tools | Lesson 5 of 12

awk Basics

awk automatically splits each line of text into columns and lets you write small programs that act on those columns.

Printing a Field

awk automatically splits each line on whitespace into fields numbered $1, $2, and so on, with $0 referring to the entire line. A simple {print $1} program prints just the first field of every input line.

Example: Printing a Field

bash
#!/bin/bash
printf "Alice 30 Engineer\nBob 25 Designer\n" | awk '{print $1}'

Custom Field Separator

-F sets the character (or pattern) used to split fields, which is essential for structured data like CSV where commas separate values instead of whitespace.

Example: Custom Field Separator

bash
#!/bin/bash
echo "alice,30,engineer" | awk -F, '{print $2}'

Using NF and NR

NF holds the number of fields on the current line, and NR holds the current line number (record number) across the whole input, both of which are useful for filtering or validating rows.

Example: Using NF and NR

bash
#!/bin/bash
printf "a b c\nx y\n" | awk '{print "line", NR, "has", NF, "fields"}'

Simple Conditions and Arithmetic

awk programs can include conditions before the action block, so condition { action } only runs the action on matching lines, and awk supports normal arithmetic directly on field values.

Example: Simple Conditions and Arithmetic

bash
#!/bin/bash
printf "apple 3\nbanana 7\ncherry 1\n" | awk '$2 > 2 { print $1, $2 * 10 }'
Common Mistakes
  1. Forgetting $0 refers to the whole line, while $1, $2, etc. refer to individual fields.
  2. Assuming fields are always split on a single space; the default field separator is any run of whitespace, and -F is needed to change it.
  3. Writing complex logic directly on the command line instead of using -f script.awk for anything beyond a one-liner, making quoting a nightmare.
Chapter Summary
  • awk splits each input line into fields, accessible as $1, $2, ... with $0 meaning the whole line and NF the number of fields.
  • -F sets the field separator, e.g. -F, for comma-separated data.
  • awk '{print $1}' prints just the first field of every line.
  • NR holds the current line (record) number, which is handy for filtering by position.
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.