join

Concatenate lines with the same content in specified fields in two files

Supplementary instructions

join command is used to join lines with the same specified field content in two files. Find the lines with the same content in the specified fields in the two files, merge them, and then output them to the standard output device.

grammar

join(option)(parameter)

Options

-a<1 or 2>: In addition to displaying the original output content, lines in the command file that do not have the same fields are also displayed;
-e<string>: If the specified field cannot be found in [File 1] and [File 2], fill in the string in the option in the output;
-i or --ignore-case: ignore case differences when comparing field contents;
-o<format>: Display the results according to the specified format;
-t<character>: Use the delimiting character of the field;
-v<1 or 2>: Same as -a, but only displays lines that do not have the same fields in the file;
-1<field>: connect to the field specified by [File 1];
-2<field>: Connect the field specified by [File 2].

Parameters

Common usage

Concatenate lines with the same specified field content in two files:

[root@localhost ~]# cat name
1 xiaoming
2 xiaowang
3 xiaoliu
[root@localhost ~]# cat city
1 beijing beijing
2 hubei wuhan
3 hunan changsha

# If the city file is at the end, it will be spliced at the end. If the city file is at the front, the name file will be spliced at the end.
[root@localhost ~]# join name city
1 xiaoming beijing beijing
2 xiaowang hubei wuhan
3 xiaoliu hunan changsha

Concatenate specified columns of two files:

# Splice the 2nd column of the name file and the 3rd column of the city file
[root@localhost ~]# join -o 1.2 2.3 name city
xiaoming beijing
xiaowang wuhan
xiaoliu changsha