Skip to content
Latchkey

awk gsub and sub: Find and Replace in Fields

awk gsub(re, repl, target) replaces every regex match in the target and sub replaces only the first.

Stripping units, commas, or color codes out of a field before arithmetic is a gsub job. gsub edits in place and returns how many replacements it made.

What it does

gsub(regex, replacement, target) replaces all matches of regex in target with replacement and returns the count; sub does the same for the first match only. If target is omitted, it operates on $0. Editing a field with gsub also rebuilds $0 using OFS.

Common usage

Terminal
# strip commas from a number before summing
awk '{gsub(/,/, "", $1); sum += $1} END {print sum}' big-nums.txt
# remove an "ms" unit suffix
awk '{gsub(/ms$/, "", $2); print $2}' timings.txt
# replace the first match only
awk '{sub(/^v/, "", $1); print $1}' versions.txt
# strip ANSI color codes from a whole line
awk '{gsub(/\033\[[0-9;]*m/, ""); print}' colored.log

Functions

CallWhat it does
gsub(/re/, "x")Replace all matches in $0, return count
gsub(/re/, "x", $2)Replace all matches in field 2
sub(/re/, "x", $1)Replace the first match in field 1
gsub(/,/, "")Delete all commas in $0
n = gsub(/re/, "x")Capture the number of replacements made

In CI

Clean a metric before arithmetic: gsub(/[^0-9.]/, "", $3) drops everything but digits and a dot so $3 sums correctly. Because gsub on a field rebuilds $0 with OFS, a later print $0 reflects the cleaned value.

Common errors in CI

In the replacement string, & means "the matched text"; to insert a literal ampersand, escape it as \&. A gsub regex with special characters needs escaping: gsub(/./, "x") replaces every character because . is any char. mawk and gawk handle some bracket and backslash cases differently, so test the pattern on the target runner. Forgetting that gsub returns a count, not the string, leads to using it where the modified field was meant.

Related guides

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