Easy Tutorial
❮ Linux Comm Split Linux Comm Pppsetup ❯

Linux join Command

Linux Command Manual

The Linux join command is used to connect lines of two files where the specified field content is the same.

It finds lines with the same specified field content in two files, merges them, and outputs the result to the standard output device.

Syntax

join [-i][-a&lt;1 or 2>][-e<string>][-o<format>][-t<char>][-v&lt;1 or 2>][-1<field>][-2<field>][--help][--version][file1][file2]

Parameters:

Example

Connecting two files.

To better understand the join command, first display the contents of files testfile_1 and testfile_2 using the cat command.

Then compare the two files using the default method, connecting lines where the specified field content is the same, and enter the command in the terminal:

join testfile_1 testfile_2

First, view the contents of testfile_1 and testfile_2:

$ cat testfile_1 # Contents of testfile_1
Hello 95 # For example, the first column is the name, the second column is the amount
Linux 85
test 30
cmd@hdd-desktop:~$ cat testfile_2 # Contents of testfile_2
Hello 2005 # For example, the first column is the name, the second column is the year
Linux 2009
test 2006

Then use the join command to connect the two files, with the result as follows:

$ join testfile_1 testfile_2 # Connect the contents of testfile_1 and testfile_2
Hello 95 2005 # Displayed content after connection
Linux 85 2009
test 30 2006

The order of file1 and file2 affects the output to the standard output. For example, swapping the two files in the command, i.e., entering the following command:

join testfile_2 testfile_1

The final output to the standard output will change, as shown below:

$ join testfile_2 testfile_1 # Changing the file order to connect the two files
Hello 2005 95 # Displayed content after connection
Linux 2009 85
test 2006 30

Linux Command Manual

❮ Linux Comm Split Linux Comm Pppsetup ❯