
How to Use awk to Join Two Files on a Key Without Surprises
Join two files on a key with awk: inner and left joins, duplicate keys, composite keys, output separators and when the join command is the better choice.
Tag
13 posts

Join two files on a key with awk: inner and left joins, duplicate keys, composite keys, output separators and when the join command is the better choice.

Read two files in one awk program with NR == FNR: load a lookup table, annotate lines, filter by a list and avoid the empty file trap.

Understand awk pattern action pairs: regex and comparison patterns, range patterns, BEGIN and END blocks, next and exit, and overlapping rules.

Count, group and deduplicate with awk associative arrays: count[$1]++, loop over keys, sort the output, test membership and use composite keys.

Use regex in awk: the tilde operator, patterns between slashes, dynamic regex strings, the match function with RSTART and RLENGTH, and gsub with ampersand.

Sum, average, count, minimum and maximum a column with awk: skip headers, guard against non-numeric rows and empty input, and group the totals by a key.

Learn awk fields and printf: $1 and NF, set the field separator with -F or FS, control the output separator, align columns and rebuild a line.

Write awk user functions: parameters and local variables, recursion, libraries, numeric precision, rounding and the string versus number pitfalls.

Gawk features beyond POSIX awk: arrays of arrays, asort, gensub, strftime, FPAT, networking, include and the portability costs of using them.

Pass shell variables into awk safely with -v and ENVIRON instead of splicing them into the program, and avoid quoting, escape-sequence and injection bugs.

Process CSV with awk when fields hold quoted commas: why -F, fails, gawk FPAT, the --csv flag in gawk 5.3, headers and when to use csvkit or Python.

Use awk to parse logs and build reports: field splitting, counting by key, sums and averages, time buckets, sorted summaries and END blocks.