awk Command in Linux: Syntax, Patterns & Practical Examples

awk scans input line by line, splits fields, and runs actions when patterns match. Use it to print columns, filter rows, sum numbers, and build quick reports from logs or CSV-style text.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

awk Command in Linux: Syntax, Patterns & Practical Examples
About awk scans input line by line, splits fields, and runs actions when patterns match. Use it to print columns, filter rows, sum numbers, and build quick reports from logs or CSV-style text.
Tested on Ubuntu 25.04 (Plucky Puffin); mawk 1.3.4; kernel 7.0.0-27-generic
Package mawk (apt/deb) · gawk (dnf/rpm)
Man page awk(1)
Privilege user
Distros

Ubuntu and Debian default to mawk at /usr/bin/awk. Many distros ship gawk instead.

Simple substitutions: sed. One field only: cut.

awk — quick reference

Fields and printing

Split each line on whitespace (or a custom separator) and print selected columns.

When to use Command
Print the first field of each line awk '{print $1}' file
Print multiple fields (reorder columns) awk '{print $2, $1}' file
Print the last field on the line awk '{print $NF}' file
Set comma (or other) field separator awk -F',' '{print $1}' file.csv
Join fields with a custom output separator awk 'BEGIN{OFS=","} {print $1,$3}' file

Patterns and conditions

Run the action only when a pattern or field test matches.

When to use Command
Print lines matching a regex awk '/error/' log
Print when a field equals a string awk '$3 == "admin" {print $1}' file
Numeric comparison on a field awk '$2 >= 100 {print $1, $2}' file
Print a specific line number awk 'NR==2' file
Skip the header row in CSV awk -F',' 'NR>1 {print $1}' file.csv

BEGIN, END, and variables

When to use Command
Sum a numeric column awk '{sum+=$2} END {print sum}' file
Pass a shell variable into awk awk -v thresh=100 '$2 >= thresh' file
Print line number with each record awk '{print NR, $0}' file
Run a program stored in a file awk -f script.awk file

Help and version

When to use Command
Show mawk version (Ubuntu default) awk --version
mawk-specific help awk -W help

awk — command syntax

awk runs a program against each input line. Synopsis from awk --help on Ubuntu 25.04 (mawk 1.3.4):

text
mawk [Options] [Program] [file ...]

Program:
    The -f option value is the name of a file containing program text.
    If no -f option is given, a "--" ends option processing; the following
    parameters are the program text.

Options:
    -f program-file  Program text is read from file instead of from the
                     command-line.  Multiple -f options are accepted.
    -F value         sets the field separator, FS, to value.
    -v var=value     assigns value to program variable var.

Default field splitting is whitespace. $0 is the whole line; $1, $2, … are fields; NF is the field count; NR is the record (line) number.


awk — command examples

Essential Print the first column

The most common awk one-liner: treat each line as fields and emit one column.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk '{print $1}' data.txt

Sample output:

text
alice
bob
charlie

Field numbering starts at 1, not 0.

Essential Swap column order

Print fields in any order — handy for log reformatting before sort.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk '{print $2, $1}' data.txt

Sample output:

text
100 alice
200 bob
50 charlie

Separate printed fields with a space by default; set OFS in BEGIN for commas.

Essential Filter rows by field value

Add a condition before the action block to keep matching lines only.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk '$3 == "admin" {print $1}' data.txt

Sample output:

text
alice

Use == for string equality; quote literal values.

Common Sum a numeric column with END

BEGIN / END blocks run once before and after the main loop — classic for totals.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk 'BEGIN {sum=0} {sum+=$2} END {print sum}' data.txt

Sample output:

text
350

Empty lines or non-numeric fields can skew totals — filter first if needed.

Common Parse CSV with -F

Comma-separated input needs -F',' so $1 is not the entire line.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > csv.txt <<'EOF'
name,dept,salary
alice,eng,95000
bob,sales,72000
EOF
awk -F',' 'NR>1 {print $1, $3}' csv.txt

Sample output:

text
alice 95000
bob 72000

NR>1 skips the header row.

Common Print one line by record number

NR is the current line number across all input files.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk 'NR==2' data.txt

Sample output:

text
bob 200 user

Same idea as sed -n '2p' but embedded in larger awk programs easily.

Advanced Prefix each line with NR

Useful when merging outputs and you need stable line references.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk '{print NR, $0}' data.txt

Sample output:

text
1 alice 100 admin
2 bob 200 user
3 charlie 50 guest

$0 is the full original line including spaces.

Advanced Pass a threshold with -v

-v sets awk variables before the program runs — safer than string interpolation in the shell.

Run the command:

bash
WORKDIR=/tmp/awklab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
cat > data.txt <<'EOF'
alice 100 admin
bob 200 user
charlie 50 guest
EOF
awk -v thresh=100 '$2 >= thresh {print $1, $2}' data.txt

Sample output:

text
alice 100
bob 200

Change thresh on the command line without editing the awk program.


awk — when to use / when not

Use awk when Use something else when
You need columns, math, or conditions on structured lines You only substitute text in place — sed
You are summing, counting, or reporting from logs or CSV You need a single fixed fieldcut is simpler
One short program beats a shell loop You only search for a string — grep
Input is line-oriented text, not binary Input is JSON or XML — use jq or a proper parser

awk vs sed vs cut

awk sed cut
Strength Fields, conditions, arithmetic Stream edits and substitutions Extract one field or byte range
Typical input Logs, ps, CSV Config templates /etc/passwd columns
Learning curve Small programs Regex-heavy Lowest for one column

On Ubuntu 25.04, /usr/bin/awk is mawk — most POSIX one-liners match gawk, but advanced gawk-only features may differ.


Text tools often chained before or after awk.

Command One line
awk Column logic and reports (this page)
sed Search-and-replace and line editing
sort command Sort awk output

Browse the full index in our Linux commands reference.


awk — interview corner

What is awk used for in Linux?

awk is a pattern-action language: read input record by record (usually one line), split into fields ($1, $2, …), and run { actions } when a pattern matches.

Admins use it for log parsing, df/ps reports, CSV column pulls, and quick totals — without writing a full Python script.

A strong answer is:

"awk scans lines, splits fields, and runs actions on matches — my go-to for columns, filters, and sums on text data."

What are $0, $1, and NF in awk?
Symbol Meaning
$0 Entire current line
$1, $2, … First field, second field, … (default split on whitespace)
NF Number of fields on the current line
NR Current record (line) number

A strong answer is:

"$0 is the full line, $1 onward are fields, NF is how many fields exist, and NR is the line number — defaults assume whitespace-separated input."

What do BEGIN and END blocks do?

BEGIN runs once before any input line is read — initialize variables or print headers. END runs once after all input — print totals or summaries.

Classic idiom:

bash
awk '{sum+=$2} END {print sum}' file

A strong answer is:

"BEGIN sets up before the first line; END reports after the last — I use END for totals and BEGIN for headers or OFS."

When would you pick awk over sed?

Choose awk when the task is field-aware: print column 3, sum column 2, filter where $NF > 100. sed excels at in-stream edits — replace text, delete lines matching a regex, line numbers without field math.

A strong answer is:

"awk when I need columns, numbers, or conditions; sed for substitutions and line edits. Many pipelines use both."

What awk runs by default on Ubuntu?

On Ubuntu 25.04, /usr/bin/awk is mawk (lightweight, fast). Other distros may default to gawk. Portable one-liners stick to POSIX features; gawk-only extensions (PROCINFO, match with arrays) need an explicit gawk call.

Check with:

Resolve the real path behind a PATH alias with readlink -f "$(which cmd)"; see the which command for symlink chains.

bash
awk --version
readlink -f "$(which awk)"

A strong answer is:

"Ubuntu defaults to mawk at /usr/bin/awk; I verify with awk --version and avoid gawk-only syntax unless gawk is installed."


Troubleshooting

Symptom Likely cause Fix
Whole line in $1 on CSV Default FS is whitespace awk -F',' '{...}'
Empty output Pattern too strict or wrong field index Print $0 first; check NF
syntax error Unbalanced quotes in shell Wrap program in single quotes
Numbers treated as strings Locale or leading spaces Strip fields or force numeric context
gawk feature fails on Ubuntu mawk is default Install gawk or rewrite portably

References

  • sed(1) man page — stream edits without field math
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …