Linux join command

Linux 命令大全Linux Command Encyclopedia

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

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

Syntax

join [-i][-a<1或2>][-e<字符串>][-o<格式>][-t<字符>][-v<1或2>][-1<栏位>][-2<栏位>][--help][--version][文件1][文件2]

Parameters:

  • -a<1 or 2> In addition to displaying the original output content, also displays lines in the instruction file that do not have the same field.
  • -e<string> If the specified field cannot be found in [file 1] and [file 2], fill the output with the string in the option.
  • -i or --igore-case Ignores case differences when comparing field content.
  • -o<format> Displays results according to the specified format.
  • -t<character> Uses the field separator character.
  • -v<1 or 2> Same as -a, but only displays lines in the file that do not have the same field.
  • -1<field> Joins the specified field of [file 1].
  • -2<field> Joins the specified field of [file 2].
  • --help Displays help.
  • --version Displays version information.

Examples

Join two files.

To clearly understand the join command, first use the cat command to display the contents of the files testfile_1 and testfile_2.

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

join testfile_1 testfile_2 

First, view the file contents of testfile_1 and testfile_2:

$ cat testfile_1 #testfile_1文件中的内容  
Hello 95 #例如,本例中第一列为姓名,第二列为数额  
Linux 85  
test 30  
cmd@hdd-desktop:~$ cat testfile_2 #testfile_2文件中的内容  
Hello 2005 #例如,本例中第一列为姓名,第二列为年份  
Linux 2009  
test 2006 

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

$ join testfile_1 testfile_2 #连接testfile_1、testfile_2中的内容  
Hello 95 2005 #连接后显示的内容  
Linux 85 2009  
test 30 2006 

The positions of file 1 and file 2 affect the result output to the standard output. For example, swapping the two files in the command, that is, entering the following command:

join testfile_2 testfile_1

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

$ join testfile_2 testfile_1 #改变文件顺序连接两个文件  
Hello 2005 95 #连接后显示的内容  
Linux 2009 85  
test 2006 30 

Linux 命令大全Linux Command Encyclopedia

Other Extensions