join类似 db里面的join方法,同样有left join right join inner join等
指定参数-a 可以指定join的方式。 -a1表示 显示第一个文件中不匹配的行,即为left join
-a2 表示 显示第二个文件中不匹配的行,即为right join
-o 1.1 2.1 -o是输出参数,1.1表示输出第一个文件的第一列;2.1表示输出第二个文件的第一列
-t "|" 是分隔符参数, 表示指定|作为域分隔符
-j 1.1 2.1 -j是表示连接的字段参数, 这里表示第一个文件的第一列和第二个文件的第一列关联
join - join lines of two files on a common field
-a FILENUM
print unpairable lines coming from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or
FILE2
例子一:
[root@SOR_SYS hahah]# cat aa
chris 300
herman 150
jane 200
joe 100
[root@SOR_SYS hahah]# cat bb
chris 95
herman 80
jane 75
joe 50
[root@SOR_SYS hahah]# join -o 1.2 2.2 aa bb
300 95
150 80
200 75
100 50
例子二:
[root@SOR_SYS hahah]# cat file1
11111
22222
11111
22222
33333
44444
55555
[root@SOR_SYS hahah]# cat file2
11111 bj
22222 hb
33333 hn
44444 nm
55555 xm
66666 mk
[root@SOR_SYS hahah]# join -1 2 -2 1 <(sort file1|uniq -c) <(sort file2)
11111 2 bj
22222 2 hb
33333 1 hn
44444 1 nm
55555 1 xm