CmdKeys LogoCommand Quiz

Awk Command Practice

awk splits each record into fields ($1…$NF) and runs a pattern-action program. This track drills printing columns, /pattern/ and NR filters, -F separators, and BEGIN/END sums — the one-liners you type in a pipeline, not a spreadsheet. Grep selects lines; sed edits them; awk computes on fields.

Command Reference

separator

CommandDescription
awk -F: '{print $1}' <file>Split each record on colon and print the first field.
awk -F: '{print $1, $NF}' <file>Split each record on colon and print the first and last fields.
awk -F ',' '{print $NF}' <file>Split each record on comma and print the last field.
awk 'BEGIN {FS=":"; OFS=","} {print $1,$2,$3}' <file>Read colon fields and print the first three fields separated by commas.
awk -v FS=':' '{print $1}' <file>Set FS with -v and print the first field (not the -F spelling).

print

CommandDescription
awk '{print $1, $NF}' <file>Print the first field and the last field of every record.
awk '{print NR, $0}' <file>Print the record number and the whole line for every record.
awk '{print $1}' <file>Print the first field of every record.
awk '{print $(NF-1)}' <file>Print the second-to-last field of every record.
awk '{print $1 "=" $2}' <file>Print the first two fields joined by an equals sign, with no extra OFS space.
awk '{$1 = NR; print}' <file>Replace the first field with the record number and reprint the record.

select

CommandDescription
awk 'NR > <num>' <file>Print records after a given record number.
awk '$NF > <num>' <file>Print records whose last field is greater than a number.
awk 'NR==<num>,NR==<num>' <file>Print an inclusive range of record numbers.
awk 'NR%<num>==1' <file>Print every Nth record, starting with the first.

script

CommandDescription
awk -f <script> <file>Run commands from an awk script file against an input file.
awk -F: -f <script> <file>Run a script file with colon as the field separator.
awk -v <name>=<text> '{print <name>}' <file>Pass a variable into the program and print it on every record.
awk -v OFS=',' '{print $1,$2}' <file>Set OFS with -v and print the first two fields comma-separated.

blocks

CommandDescription
awk 'BEGIN {print "<text>"}'Print a string once before reading any input.
awk 'BEGIN {print "<text>"} {print $1}' <file>Print a header once, then the first field of every record.
awk '{print $1} END {print "<text>"}' <file>Print the first field of every record, then a trailer once.
awk '{s+=$1} END {print s}' <file>Add the first field of every record and print the total once.

filter

CommandDescription
awk '/<pattern>/' <file>Print records whose whole line matches a pattern.
awk '/<pattern>/ {print $2}' <file>Print the second field of records whose line matches a pattern.
awk '!/<pattern>/' <file>Print records whose whole line does not match a pattern.
awk '$1 ~ /<pattern>/' <file>Print records whose first field matches a regex.
awk '$1 !~ /<pattern>/' <file>Print records whose first field does not match a regex.
awk '$1 == "<name>"' <file>Print records whose first field equals an exact string.

Key Use Cases

  • Print the first and last colon-separated fields from a passwd-style file
  • Keep only lines whose first field matches a regex
  • Number every record and print the whole line
  • Sum the first column and print the total in an END block
  • Set FS and OFS so comma output matches colon input
  • Run a saved .awk script file against an input file

Frequently Asked Questions

How is awk different from sed?

sed applies an address plus an edit (s///, d, i). awk splits the line into fields and runs a program. print $1 and END {print s} are awk; they are not sed scripts.

What are $0, $1, and $NF?

$0 is the whole record. $1 is the first field. $NF is the last field; NF is the field count, so $(NF-1) is the second-to-last field.

When do I use -F versus BEGIN {FS=…}?

-F (or -v FS=) sets the input field separator before records are read. BEGIN {FS=":"; OFS=","} is the same idea when you also need OFS or other setup. This track does not treat -F and -v FS= as fill-blank aliases of each other.

What do BEGIN and END run on?

BEGIN runs once before any input. END runs once after the last record. A missing pattern (just {…}) runs on every record.

Ready to master Awk commands?

Test your muscle memory with our spaced-repetition quiz system. Free forever.

Start Practice Now →