Merge two sorted files on a common field quickly from the shell. 29.03.2026 | reading time: 2 min Ever needed to merge two files by a common key? Use `join` to combine sorted text files on a matching field and produce compact merged output; the examples below show the exact commands to run and the result you will get. A concrete quick example Scenario: two CSV files with user IDs and values. File 'users.csv':\n```\n1,alice\n2,bob\n3,charlie\n```\nFile 'scores.csv':\n```\n1,85\n3,92\n4,78\n```\nDo this: sort both files by the join field and run `join` with a comma delimiter:\n```\nsort -t, -k1,1 users.csv > users.s\nsort -t, -k1,1 scores.csv > scores.s\njoin -t, -1 1 -2 1 users.s scores.s\n```\nOutput produced:\n```\n1,alice,85\n3,charlie,92\n```\nNote: IDs 2 and 4 are omitted by default because they have no match. Practical knobs and gotchas Sort is mandatory: `join` expects both files sorted on the join field or results will be incorrect; control the delimiter with `-t`, select which fields with `-1` and `-2`, and emit unmatched lines with `-a 1` or `-a 2`; use `-v 1` or `-v 2` to list lines unique to one file, `-o` to format output columns, and `-e` to supply a replacement for empty fields; for ad-hoc work prefer process substitution like `join -t, <(sort -t, -k1,1 a) <(sort -t, -k1,1 b)` and handle headers with `head`/`tail` or a short `awk` filter. When to reach for other tools If you need complex key logic, non-equi joins, or richer CSV handling, move to `awk` or a CSV-aware utility; use `paste` for side-by-side concatenation without keys, and consider Python or specialized CSV libraries for messy real-world data; for very large datasets, preprocess with `sort` and stream with `join` to avoid loading everything into memory. Finish and next steps Try the example on your machine, then experiment with `-a`, `-v` and `-o` to see how unmatched rows and custom column output change the result; deepen your shell skills and consider formal certification paths like CompTIA Linux+ or LPIC-1, and check out bitsandbytes.academy for intensive exam preparation. Join Bits & Bytes Academy First class LINUX exam preparation. utilities scripting filesystem