join - Linux


The join command in Linux is used for joining lines of two files on a common field. It can be effectively used for combining data from two different files where one or more common fields exist. This command is especially useful in data analysis and database formatting where relations between different datasets are to be depicted from file-based sources.


The basic syntax of the join command is as follows:

join [OPTION]... FILE1 FILE2
  • FILE1 and FILE2 are the two files to join.

The command can be executed with several options that modify its behavior:

join [-1 FIELD] [-2 FIELD] [-j FIELD] [-o FORMAT] [-t CHAR] [-a FILENUM] [-e EMPTY] [--ignore-case] FILE1 FILE2


  • -1 FIELD: Specifies the join field for the first file.
  • -2 FIELD: Specifies the join field for the second file.
  • -j FIELD: Sets the join field used in both files (assumes -j 1 if not specified).
  • -o FORMAT: Formats the output using a list of comma-separated fields (e.g., 1.1,2.2).
  • -t CHAR: Specifies a character used as a field delimiter.
  • -a FILENUM: Includes unpairable lines from file FILENUM where FILENUM is 1 or 2.
  • -e EMPTY: Replaces missing input fields with EMPTY.
  • --ignore-case: Ignores differences in case when comparing fields.
  • -v FILENUM: Prints only unpairable lines from file FILENUM.


  1. Simple Join on Common Field:

    join file1.txt file2.txt

    Joins file1.txt and file2.txt on the first field of each file.

  2. Specifying Join Field:

    join -1 2 -2 3 file1.txt file2.txt

    Join file1.txt (field 2) and file2.txt (field 3).

  3. Join with Custom Output Format:

    join -o 1.1,2.2 file1.txt file2.txt

    Only display the 1st field of the first file and the 2nd field of the second file in the output.

  4. Include Lines with No Match:

    join -a 1 -a 2 file1.txt file2.txt

    Includes lines from both files even if there is no matching.

Common Issues

  • Field Misalignment: Fields must be sorted on the join key before using join. You can use sort -k [field] to ensure this.
  • Missing Delimiters: Incorrect results if the delimiter isn’t consistent across the files or properly specified with -t.

To fix these, ensure proper sorting and delimiter specification.


join can be combined with other commands like sort or awk for complex data processing tasks:

sort file1.txt > file1_sorted.txt
sort file2.txt > file2_sorted.txt
join file1_sorted.txt file2_sorted.txt | awk '{print $1, $3}'

This script sorts two files before joining them and then processes the output with awk.

  • sort: Sorts files or streams.
  • awk: Pattern scanning and processing language.
  • sed: Stream editor for filtering and transforming text.

For further reading and detailed documentation, refer to the GNU coreutils join manual accessible via man join in the terminal or from GNU Coreutils’ online manuals.