Use the join command of linux to connect two files
This article mainly introduces the use of linux join command to connect two files, has a certain reference value, friends in need can refer to. Let's take a look at it with me.
In short, the join operation of join is to pick out two records with the same fields and put all the fields of those records on one line.
Note: when join connects two files, both files must be sorted by connection domain, and sorting by other domain is invalid.
Syntax format: join [parameters] [File 1] [File 2]
Common parameters:
-A1 or-a2 in addition to showing the record of the common domain,-A1 shows that the first file has no record of the common domain,-a2 shows that there is no record of the common domain in the second file,-I ignores case-o sets the format displayed by the result-t changes the delimiter of the domain-v1 or-v2 does not show the record of the common domain,-v1 shows that the first file has no record of the common domain -v2 shows the record of no common domain in the second file-1 or-2-1 is used to set the domain to which file 1 is connected, and-2 is used to set the domain to which file 2 is connected.
Reference example
Connect the records of two files with a common domain:
[root@linuxcool ~] # cat file.db
A li:20:men:anhui
B wang:21:women:jiangsu
C zhang:22:men:anhui
D liu:23:women:Shanghai
E chen:23:women:Hefei
[root@linuxcool ~] # cat file_hobby.db
A li:Song
B wang:shopping
C zhang:pingpong
D liu:chess
E Wang:reading
[root@linuxcool ~] # join-t: file.db file_hobby.db
A li:20:men:anhui:Song
B wang:21:women:jiangsu:shopping
C zhang:22:men:anhui:pingpong
D liu:23:women:Shanghai:chess
-A1 also shows that there is no record of a common domain in the first file, and-a2 shows the second:
[root@linuxcool] # join-t:-A1 file.db file_hobby.db
A li:20:men:anhui:Song
B wang:21:women:jiangsu:shopping
C zhang:22:men:anhui:pingpong
D liu:23:women:Shanghai:chess
E chen:23:women:Hefei
[root@linuxcool] # join-t:-a2 file.db file_hobby.db
A li:20:men:anhui:Song
B wang:21:women:jiangsu:shopping
C zhang:22:men:anhui:pingpong
D liu:23:women:Shanghai:chess
E Wang:reading
Set the fields of the specified format to display (display the fields with common records in the format of name + gender + preference):
[root@linuxcool] # join-t:-o 1.1 1.3 2.2 file.db file_hobby.db
A li:men:Song
B wang:women:shopping
C zhang:men:pingpong
D liu:women:chess above is the use of linux's join command to connect the details of the two files, after reading it, do you have anything to gain? If you want to know more about it, welcome to the industry information!