Join Files With an awk Lookup

Map keys to values across two files in a single streaming pass.

LinuxtextscriptingcliLow risk
Use NR==FNR to load the lookup file into an array, then print matches from the data file.

01Command

Command
$ awk 'NR==FNR{a[$1]=$2;next} {print $1, a[$1]}' map.txt data.txt

02What it does

This awk idiom loads the first file into an associative array and joins the second on the fly, so even huge files are processed in one pass with modest memory. It replaces ad-hoc scripts and is a classic reason to reach for the shell.

03Risk

RiskLow risk
RequirementsPreinstalled on Linux (GNU awk)
!Warning: Key order and normalization must match exactly; differences in case or whitespace silently produce blanks.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Developer

06Related solutions