Why Use Awk?
- Processes structured data by rows (records) and columns (fields)
- Handles irregular whitespace automatically
- Integrates seamlessly into Unix pipelines
- Offers concise one-liners or full-fledged scripts
Fields and Records
Imagine a seating chart stored inminimovies.txt, where “Y” means a seat is taken and “N” means it’s available. Awk treats each line as a record and each whitespace-separated item as a field.

- Columns ➔ Fields (
$1,$2, …) - Rows ➔ Records (
NRis the built-in record counter)
Extracting a Specific Seat
Step 1: Select the third column ($3) for every record.Step 2: Filter for record number 2 using
NR.

NR is a built-in Awk variable representing the current record (line) number.Fields are referenced as
$1, $2, etc.Awk as a Domain-Specific Language
Awk is more than a simple filter—it’s a small programming language tailored for text. It provides:- Pattern-action statements
- Built-in variables (
NR,NF,FS,OFS) - Control structures (
if,while,for)

Awk treats any sequence of spaces and tabs as the default field separator (
FS = [ \t]+).Handling Irregular Spacing
Even if your data has inconsistent spacing, Awk splits fields correctly:
Integrating Awk with Unix Pipelines
Combine Awk with other commands to filter and format on the fly:Common Use Cases
| Command | Purpose | Example |
|---|---|---|
| top | System runtime stats | top | awk '{ print $2 }' | head -5 |
| ps | Process listing | ps aux | awk '$3 > 50 {print $1, $3}' |
| df -h | Disk usage report | df -h | awk '$5 > "80%"' |
Writing Full Awk Scripts
Instead of one-liners, you can write complete Awk programs:Ensure the shebang path (
/usr/bin/awk) matches your system’s Awk installation.