Skip to content
Latchkey

awk print $N: Extract Columns from Output

awk splits each line into fields and lets you print any of them by number with $1, $2, and so on.

Pulling the second column out of ps, df, or a tool listing is the most common awk job in CI. $0 is the whole line; $1 is the first field.

What it does

awk reads input line by line, splits each line on runs of whitespace into fields $1, $2, ... and prints whatever you ask. $0 is the entire line and $NF is the last field. The default action when a pattern matches is to print $0.

Common usage

Terminal
# second column of every line
 kubectl get pods | awk '{print $1}'
# first and third fields together
df -h | awk '{print $1, $3}'
# the whole line (explicit)
cat build.log | awk '{print $0}'

Fields and references

ReferenceWhat it prints
$0The entire current line
$1, $2, ...The first, second, ... field
$NFThe last field on the line
$(NF-1)The second-to-last field
print $1, $2Two fields joined by OFS (a space by default)
print $1 $2Two fields concatenated with no separator

In CI

To capture a single field into a step output, extract it with awk and append to $GITHUB_OUTPUT: VERSION=$(node -v | awk '{print $1}'); echo "version=$VERSION" >> "$GITHUB_OUTPUT". A later step reads it as ${{ steps.<id>.outputs.version }}.

Common errors in CI

If $2 prints nothing, the line has fewer fields than you think, often because the separator is a tab or comma, not a space; set it with -F. "awk: syntax error at source line 1" usually means the program was not single-quoted and the shell expanded $1. Forgetting the comma in print $1 $2 concatenates the fields instead of separating them.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →