join Command: Tutorial & Examples
Join lines of two files on a common field
The join
command is a utility in Linux that allows you to join the lines of two files based on a common field. It reads two input files and combines them based on the fields specified with the -1
and -2
options, which specify the field in each input file to be used for joining. By default, the join command outputs a line for each pair of input lines that have identical values in the specified fields.
Here is the basic syntax for using join:
join [options] file1 file2
Some common options for join
include:
-1 field
: Specifies the field infile1
to use for joining.-2 field
: Specifies the field infile2
to use for joining.-t char
: Specifies the field separator character. The default is the tab character.-a file
: Prints all lines from file, regardless of whether they match a line in the other file.-e string
: Specifies a string to be printed in place of empty fields.-o format
: Specifies the output format.
Here is an example of using join
to join the lines of two files based on the first field:
join -t , -1 1 -2 1 file1.csv file2.csv
This command will join the lines of file1.csv
and file2.csv
based on the first field (which is separated by a comma).