使用示例
示例一 内连接(忽略不匹配的行)
不指定任何参数的情况下使用join命令,就相当于数据库中的内连接,关键字不匹配的行不会输出。
[root@rhel55 linux]# cat month_cn.txt
1 一月
2 二月
3 三月
4 四月
5 五月
6 六月
7 七月
8 八月
9 九月
10 十月
11 十一月
12 十二月
13 十三月,故意的
[root@rhel55 linux]# cat month_en.txt
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
14 MonthUnknown
注:注意两个文件的内容,中文版的多了十三月,英文版的多了14月,这纯粹是为了方便演示。
[root@rhel55 linux]# join month_cn.txt month_en.txt
1 一月 January
2 二月 February
3 三月 March
4 四月 April
5 五月 May
6 六月 June
7 七月 July
8 八月 August
9 九月 September
10 十月 October
11 十一月 November
12 十二月 December
[root@rhel55 linux]#
示例二 左连接(又称左外连接,显示左边所有记录)
显示左边文件中的所有记录,右边文件中没有匹配的显示空白。
[root@rhel55 linux]# join -a1 month_cn.txt month_en.txt
1 一月 January
2 二月 February
3 三月 March
4 四月 April
5 五月 May
6 六月 June
7 七月 July
8 八月 August
9 九月 September
10 十月 October
11 十一月 November
12 十二月 December
13 十三月,故意的
[root@rhel55 linux]#
示例三 右连接(又称右外连接,显示右边所有记录)
显示右边文件中的所有记录,左边文件中没有匹配的显示空白。
[root@rhel55 linux]# join -a2 month_cn.txt month_en.txt
1 一月 January
2 二月 February
3 三月 March
4 四月 April
5 五月 May
6 六月 June
7 七月 July
8 八月 August
9 九月 September
10 十月 October
11 十一月 November
12 十二月 December
14 MonthUnknown
[root@rhel55 linux]#
示例四 全连接(又称全外连接,显示左边和右边所有记录)
[root@rhel55 linux]# join -a1 -a2 month_cn.txt month_en.txt
1 一月 January
2 二月 February
3 三月 March
4 四月 April
5 五月 May
6 六月 June
7 七月 July
8 八月 August
9 九月 September
10 十月 October
11 十一月 November
12 十二月 December
13 十三月,故意的
14 MonthUnknown
[root@rhel55 linux]#
示例五 指定输出字段
比如参数 -o 1.1 表示只输出第一个文件的第一个字段。
[root@rhel55 linux]# join -o 1.1 month_cn.txt month_en.txt
1
2
3
4
5
6
7
8
9
10
11
12
[root@rhel55 linux]# join -o 1.1 2.2 month_cn.txt month_en.txt
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
[root@rhel55 linux]# join -o 1.1 2.2 1.2 month_cn.txt month_en.txt
1 January 一月
2 February 二月
3 March 三月
4 April 四月
5 May 五月
6 June 六月
7 July 七月
8 August 八月
9 September 九月
10 October 十月
11 November 十一月
12 December 十二月
[root@rhel55 linux]# join -o 1.1 2.2 1.2 1.3 month_cn.txt month_en.txt <== 字段1.3并不存在
1 January 一月
2 February 二月
3 March 三月
4 April 四月
5 May 五月
6 June 六月
7 July 七月
8 August 八月
9 September 九月
10 October 十月
11 November 十一月
12 December 十二月
[root@rhel55 linux]#
示例六 指定分隔符
用root身份,将/etc/passwd与/etc/shadow 相关的数据整合成一列
[root@www ~]# head -n 3 /etc/passwd /etc/shadow
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
==> /etc/shadow <==
root:$1$K8WSIAfQ$9i1h6a4V1XeIn0lv.CT53/:14833:0:99999:7:::99:7:::
bin:*:14833:0:99999:7:::
daemon:*:14833:0:99999:7:::
#由输出的数据可以发现这两个文件的最左边字段都是账号,且以:分割[root@www ~]# join -t ':' /etc/passwd /etc/shadow
root:x:0:0:root:/root:/bin/bash:$1$K8WSIAfQ$9i1h6a4V1XeIn0lv.CT53/:14833:0:99999:7:::
bin:x:1:1:bin:/bin:/sbin/nologin:*:14833:0:99999:7:::
daemon:x:2:2:daemon:/sbin:/sbin/nologin:*:14833:0:99999:7:::
#通过上面这个操作,我们可以将两个文件的第一字段相同者整合成一行,第二个文件的相同字段并不会显示(因为已经在第一行了)。
示例7:我们知道/etc/passwd的第四个字段是GID,那个GID记录在/etc/group 当中的第三个字段,请问如何将两个文件整合?
root@zhouwj-virtual-machine:~# head -n 3 /etc/passwd /etc/group
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
==> /etc/group <==
root:x:0:
daemon:x:1:
bin:x:2:
#从上面可以看到,确实有相同的部分,赶紧来整合一下!
root@zhouwj-virtual-machine:~# join -t ':' -1 4 /etc/passwd -2 3 /etc/group
0:root:x:0:root:/root:/bin/bash:root:x:
1:daemon:x:1:daemon:/usr/sbin:/usr/sbin/nologin:daemon:x:
2:bin:x:2:bin:/bin:/usr/sbin/nologin:bin:x:
#同样,相同的字段部分被移动到最前面了。所以第二个文件的内容就没再显示。#join在处理两个相关的数据文件时,很有帮助。