Linux Join Command

Linux join command examples

Join Command


The "join" command is used to join to files on a common field. The basic syntax of the join command is as follows:

join [options] File1 File2



Example of joining two files. Two files have been created called file1 and file2. We are going to join the two files together based on the first field. To help explain the example, I have used "cat" display the contents of "file1" and file2":



john@sles01:~/john_split> cat file1
1 Chelsea
2 Liverpool
3 Manchester United
4 Newcastle United
5 West Bromwich Albion

john@sles01:~/john_split> cat file2
1 Stamford Bridge
2 Anfield
3 Old Trafford
4 St James Park
5 The Hawthorns

john@sles01:~/john_split> join file1 file2
1 Chelsea  Stamford Bridge
2 Liverpool  Anfield
3 Manchester United Old Trafford
4 Newcastle United St James Park
5 West Bromwich Albion The Hawthorns

From the above output we can see that these two files have been joined based on the first field.


Verify files have matching fields


To verify that both files have a matching field entry, we can run the "join --checker-order" option:



john@sles01:~/john_split> join --check-order file1 file2
1 Chelsea  Stamford Bridge
2 Liverpool  Anfield
3 Manchester United Old Trafford
4 Newcastle United St James Park
5 West Bromwich Albion The Hawthorns

As both files have a matching first parameter we receive no errors. If we amend the contents of file1 as per below, we should now see an error from our "--check-order" option:



john@sles01:~/john_split> cat file1
1 Chelsea
2 Liverpool
3 Manchester United
5 Newcastle United
4 West Bromwich Albion

john@sles01:~/john_split> cat file2
1 Stamford Bridge
2 Anfield
3 Old Trafford
4 St James Park
5 The Hawthorns

john@sles01:~/john_split> join --check-order file1 file2
1 Chelsea  Stamford Bridge
2 Liverpool  Anfield
3 Manchester United Old Trafford
join: file 1 is not in sorted order

We can also use the "-v" option to check and display only unpaired lines:



john@sles01:~/john_split> join -v1 file1 file2
join: file 1 is not in sorted order
4 West Bromwich Albion

join command options



join options

-a FILENUM
              print unpairable lines coming from file FILENUM,  where  FILENUM
              is 1 or 2, corresponding to FILE1 or FILE2

       -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'

       -o FORMAT
              obey FORMAT while constructing output line

       -t CHAR
              use CHAR as input and output field separator

       -v FILENUM
              like -a FILENUM, but suppress joined output lines

       -1 FIELD
              join on this FIELD of file 1

       -2 FIELD
              join on this FIELD of file 2

       --check-order
              check  that  the  input  is  correctly sorted, even if all input
              lines are pairable

       --nocheck-order
              do not check that the input is correctly sorted

       --header
              treat the first line in each file as field headers,  print  them
              without trying to pair them

       --help display this help and exit

       --version
              output version information and exit