English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux join command

Linux Command大全

The Linux join command is used to concatenate lines with the same field content in two files.

Find and merge lines with the same field content in two files, and output them to the standard output device.

Syntax

join [-i][-a<1or2>][-e<string>][-o<format>][-t<character>][-v<1or2>][-1<field>][-2<field>][--help][--version][file1][file2]

Parameter:

  • -a<1or2> In addition to displaying the original output content, it also displays the lines without the same field in the instruction file.

  • -e<string> if [file1] with [file2If the specified field cannot be found in the brackets, the string in the option will be filled in the output.

  • -i or--igore-case

  • -Display the results according to the specified format.

  • -The separator character used for field positions.

  • -v<1or2>  Follow-a is the same, but only display the lines in the file without the same column.

  • -1<Column>  Connect [file1]Specified column.

  • -2<Column>  Connect [file2]Specified column.

  • --help  Display help.

  • --version  Display version information.

Online Example

Concatenate two files.

To clearly understand the join command, first display the file testfile_ through the cat command1and testfile_2 content.

Then compare the two files in the default way, connect the lines with the same specified field content in the two files, enter the command in the terminal:

join testfile_1 testfile_2

First view testfile_1,testfile_2 The content of the file is as follows:

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

Then use the join command to concatenate the two files, the result is as follows:

$ join testfile_1 testfile_2 #Concatenate testfile_1,testfile_2content  
Hello 95 2005 #Content displayed after connection  
Linux 85 2009  
test 30 2006

file1With the file2The position has an impact on the output to standard output. For example, if you swap the two files in the command, enter the following command:

join testfile_2 testfile_1

The final output to standard output will change as follows:

$ join testfile_2 testfile_1 #Change file order to concatenate two files  
Hello 2005 95 #Content displayed after connection  
Linux 2009 85  
test 2006 30

Linux Command大全