Text and processing

The join command

Joins two files on a common field, like a database query, but on plain text.

Syntax

join archivo1 archivo2Try it in the terminal →

Most useful options

-1 NJoin field of the first file. The default is the first one
-2 NJoin field of the second file. The default is the first one
-j NJoin field in both files. Equivalent to -1 N -2 N
-a 1 o 2Also includes the unpaired lines of the file given
-t CARÁCTERUses that character as field separator, on input and output
-o FORMATOChooses which fields come out and in what order, for example 1.2,2.2 for the second field of each file
-i, --ignore-caseIgnores case differences when comparing the fields

Copy-ready examples

$ printf '1 ana\n' > j1.txt && printf '1 madrid\n' > j2.txt && join j1.txt j2.txtjoins on the first field: key 1 appears in both files.

Example output

1 ana madrid
$ printf '1 ana\n2 beto\n' > j1 && printf '1 madrid\n' > j2 && join -a 1 j1 j2with -a 1 the line from the first file that has no match is printed too.

Example output

1 ana madrid
2 beto
$ printf 'ana,1\nberto,2\n' > a.csv && printf '1,madrid\n2,lugo\n' > b.csv && join -t ',' -1 2 -2 1 a.csv b.csvjoins two CSV files on the second column of the first and the first column of the second.

Example output

1,ana,madrid
2,berto,lugo
$ printf '1 ana\n' > j1 && printf '1 madrid\n' > j2 && join -o 1.2,2.2 j1 j2outputs only the second field of each file, without the key.

Example output

ana madrid

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
join: missing operand after 'socios.txt'You gave only one file. join needs exactly two to join.Add the second file: join socios.txt inscritos.txt.
join: nonexist: No such file or directoryOne of the files does not exist or the path is misspelled.Check the name with ls and run join again.
join: invalid field number: '0'Fields start at 1. There is no field 0 for -1 or -2.Use the field number starting at 1, for example -1 2.

Typical mistakes

  • The files must be sorted by the join field. With sort -k 1b,1 you sort by the first field, ignoring leading blanks.
  • Without -t, leading blanks are ignored and any blank separates fields. For CSV or tab-separated files, set the separator with -t.
  • Without -a, unpaired lines do not appear. If the file is not sorted, join warns that some lines cannot be joined, but it does not fix the order.
  • Comparisons follow the locale (LC_COLLATE). Sort and join with the same one, for example with LC_ALL=C.

Differences from similar commands

  • comm: compares whole lines and groups them into columns. join pairs lines by the value of a field and merges them.
  • paste: glues lines by position, without looking at the content. join pairs them by the value of a field.

Related commands

FAQ

What if there is no match?

By default that line disappears; with -a 1 or -a 2 the line from the file given is kept.

How do I use a field that is not the first?

With -1 N and -2 N you give the field of each file. If it is the same number in both, -j N is enough.

How do I sort it before joining?

With sort -k 1b,1 file, which sorts by the first field ignoring leading blanks. Do it for each file.

More Text and processing commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course