join - macOS


The join command on macOS is used to combine the lines of two sorted text files based on a common field. This is similar to the SQL JOIN operation. It is most effective in situations where you need to merge tabular data stored in text format, commonly used in data analysis and scripting to streamline information collection.


The basic syntax of the join command is as follows:

join [options] file1 file2

file1 and file2 are the input files. These files must be sorted on the join fields. Specific fields can be selected, and various options can adjust the command’s behavior.


  • -a FILENUM: Print unpairable lines coming from file FILENUM, where FILENUM is either 1 or 2.
  • -e EMPTY: Replace missing input fields with EMPTY.
  • -i, --ignore-case: Ignore differences in case when comparing fields.
  • -j FIELD: Equivalent to -1 FIELD -2 FIELD, join on this field number.
  • -o FORMAT: Specify output format, where FORMAT is a sequence of FIELD numbers.
  • -t CHAR: Use CHAR as input and output field separator.
  • -1 FIELD: Join on this FIELD number of file1.
  • -2 FIELD: Join on this FIELD number of file2.
  • -v FILENUM: Output only the lines that are not paired from FILENUM (either 1 or 2).


Simple Join:

join file1.txt file2.txt

This command joins the two files on the first field of each line.

Specify a Separator:

join -t ',' file1.txt file2.txt

Uses a comma as the field delimiter instead of the default whitespace.

Join on Different Fields:

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

Join file1 on its second field and file2 on its third field.

Output Specific Fields:

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

Here, the output will only have the first field from file1 and the second field from file2.

Common Issues

  • Sorting: join requires that the input files be sorted on the join fields. Misalignment or errors often occur when this is overlooked.
  • Delimiter mismatch: Ensuring both files use the same field delimiter is crucial; unexpected results can occur otherwise.

Solution: Check the sorting order and delimiters before using join.


join command can be combined effectively with other commands like sort, awk, or sed for more complex data manipulations. Here’s an example of how join can be used in a script:

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 uses awk to select specific fields from the joined output.

  • sort: Pre-sort files for joining.
  • awk: Useful for manipulating and analyzing joined data.
  • sed: Can be used to format data before or after the join operation.

For more information on the join command, see the man page by typing man join in your terminal.