Skip to content

join Linux Command Guide

The Linux join command is used to merge lines from two sorted files based on a common field. By default, it uses the first field of each line as the join key. This command is useful for combining data from different sources, such as databases or spreadsheets. Joining files can help you analyze and manipulate data more efficiently.

join Syntax:

Terminal window
join [options] file1 file2

Options:

OptionDescription
-tSpecify the field separator character
-1Join on the specified field from file1
-2Join on the specified field from file2
-iIgnore case while comparing fields
-vApply only to non-matching lines
-aPrint all lines

Parameters:

ParameterDescription
file1Path to the first file to be joined
file2Path to the second file to be joined

join Command Samples:

Join Two Files Based on a Common Field

Terminal window
join file1.txt file2.txt

Joins two text files based on a common field.

Join Two Files on a Specific Field

Terminal window
join -t ',' -1 2 -2 1 file1.csv file2.csv

Joins two CSV files using a comma as the delimiter and specifying the fields to join on.

Display Only Unmatched Lines

Terminal window
join -v 1 file1.txt file2.txt

Displays only the lines from file1.txt that do not have a match in file2.txt.

Specify Field Delimiter

Terminal window
join -t '|' file1.txt file2.txt

Joins two files using a vertical bar as the delimiter.

Case-Insensitive Join

Terminal window
join -i file1.txt file2.txt

Performs a case-insensitive join of two text files.

Output Unpaired Lines

Terminal window
join -a 1 file1.txt file2.txt

Outputs unpaired lines from file1.txt along with the joined lines.

Ignore Case and Join on a Specific Field

Terminal window
join -i -1 2 -2 1 file1.txt file2.txt

Performs a case-insensitive join on the second field of file1.txt and the first field of file2.txt.

join FAQ:

How do I use join in Linux?

To use the join command in Linux, execute the following command:

Terminal window
join --option <value>

What is the purpose of the join command in Linux?

The join command in Linux is used to merge lines of two sorted text files based on a common field.

How can I join two files using a specific field in Linux?

To join two files based on a specific field (e.g., field 2) in Linux, you can use the following command:

Terminal window
join -1 2 -2 2 file1.txt file2.txt

Can I specify the separator character for the join command in Linux?

Yes, you can specify a custom separator character for the join command in Linux using the -t option. For example, to use a comma as the separator, you can use the following command:

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

How do I display unmatched lines when using the join command in Linux?

To display the unmatched lines from the first file when using the join command in Linux, you can use the -a 1 option. For example:

Terminal window
join -a 1 file1.txt file2.txt

Is it possible to perform a left outer join with the join command in Linux?

Yes, you can perform a left outer join with the join command in Linux by using the -a 1 option. This will display all lines from the first file, even if there is no match in the second file.

Terminal window
join -a 1 file1.txt file2.txt
## Applications of the join command
1. Merging two files based on a common field
2. Comparing two files and displaying the lines with matching fields
3. Combining fields from two files based on a common field
4. Finding the differences between two sorted files