Join Files With an awk Lookup
Map keys to values across two files in a single streaming pass.
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.txt02What 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
!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
- gawk.1.en.html manual page (Debian bookworm)Evidence: Documents NR, FNR and associative arrays used for two-file joins.
Last verified: 2026-10-13 · Category: Developer