Skip to content
Latchkey

awk $0: Work with the Whole Line

awk $0 holds the whole current line, so printing it, prefixing it, or reassigning it rewrites the record.

Most awk actions touch individual fields, but $0 is the full line. Printing it filters; assigning to it replaces the record and re-splits into fields.

What it does

awk $0 is the entire current record. print with no argument prints $0, so a bare pattern acts like grep. Assigning a new value to $0 re-splits it into fields on FS, updating NF and $1..$NF. Assigning to any field rebuilds $0 using OFS.

Common usage

Terminal
# grep-like: print matching lines whole
awk '/ERROR/ {print}' build.log        # print and print $0 are equivalent
# prefix every line with the record number
awk '{print NR ": " $0}' app.log
# replace the whole line, then fields re-split on FS
awk '{$0 = "x " $0; print $1, $2}' data.txt
# uppercase the entire line
awk '{print toupper($0)}' names.txt

Behavior

ExpressionWhat it does
$0The entire current line
printPrint $0 (the default action)
$0 = "..."Replace the line and re-split into fields on FS
$1 = xModify a field, which rebuilds $0 using OFS
length($0)Length of the whole line
gsub(/re/, "x")Operate on $0 when the target is omitted

In CI

To pass a whole filtered line into a step output, capture it: LINE=$(awk '/version:/{print; exit}' info.txt); echo "line=$LINE" >> "$GITHUB_OUTPUT". Reassigning $0 is the trick when you need awk to re-tokenize a record after you have rewritten it.

Common errors in CI

Modifying a field changes $0 using OFS, so a later print $0 may show different separators than the input; that surprises people converting delimiters. Assigning to $0 re-splits on the current FS, so a mid-program FS change does not retroactively re-split earlier lines. print $0 with a stray comma (print $0,) appends an OFS and can add a trailing separator.

Related guides

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