join

join 功能说明:join命令针对每一对具有相同内容的输入行,整合为一行输出到标准输出,默认情况下是把输入的第一个字段作为连接字符,字段之间用空格隔开。join命令可以处理具有相关性的文件。
 
参数选项:
-a  文件号   输出文件中不匹配的行,文件号可选值1或2,分别代表文件1和文件2。
-i  比较字段时忽略大小写。
-1 字段   以第1个文件的指定字段为基础进行合并。
-2 字段   以第2个文件的指定字段为基础进行合并。

说明:使用join合并文件的要求是两个文件必须是用sort排序后的。

 
使用范例
[root@testdb ~]# cat a.log
韩海林  21岁
海林韩  23岁
韩林海  22岁
林海韩  24岁
[root@testdb ~]# cat b.log
韩林海  男
海林韩  男
韩海林  男
林海韩  男
[root@testdb ~]# join a.log b.log
join: a.log:2: is not sorted: 海林韩  23岁
join: b.log:4: is not sorted: 林海韩  男
韩海林 21岁 男
[root@testdb ~]# sort a.log > a_sort.log 
[root@testdb ~]# sort b.log > b_sort.log
[root@testdb ~]# join a_sort.log b_sort.log 
林海韩 24岁 男
海林韩 23岁 男
韩林海 22岁 男
韩海林 21岁 男

 
原文地址:https://www.cnblogs.com/l10n/p/9416542.html