我用的命令:linux
join -t $'\t' -o 1.1 1.2 1.3 1.4 file1 file2数据库
若是分隔符是tab键,不用指定-t参数的,会被当成空格处理,输出的分隔符天然也是空格了。less
若是想输出的分隔符是tab键,那么就使用我上面的方法,-t $'\t' 参数。ide
Usage: join [OPTION]... FILE1 FILE2
For each pair of input lines with identical join fields, write a line to
standard output. The default join field is the first, delimited
by whitespace. When FILE1 or FILE2 (not both) is -, read standard input.
-a FILENUM print unpairable lines coming from file FILENUM, where
FILENUM is 1 or 2, corresponding to FILE1 or FILE2
-e EMPTY replace missing input fields with EMPTY
-i, --ignore-case ignore differences in case when comparing fields
-j FIELD equivalent to `-1 FIELD -2 FIELD'
-o FORMAT obey FORMAT while constructing output line
-t CHAR use CHAR as input and output field separator
-v FILENUM like -a FILENUM, but suppress joined output lines
-1 FIELD join on this FIELD of file 1
-2 FIELD join on this FIELD of file 2
--help display this help and exit
--version output version information and exit
Unless -t CHAR is given, leading blanks separate fields and are ignored,
else fields are separated by CHAR. Any FIELD is a field number counted
from 1. FORMAT is one or more comma or blank separated specifications,
each being `FILENUM.FIELD' or `0'. Default FORMAT outputs the join field,
the remaining fields from FILE1, the remaining fields from FILE2, all
separated by CHAR.
Important: FILE1 and FILE2 must be sorted on the join fields.ui
用途说明this
Linux下最经常使用的数据文件格式是文本格式的,多个字段之间经过分隔符来区分,分隔符好比冒号(:)、制表符、空格等。/etc/passwd和 /etc/group就是用:来分隔的,用MySQL的into outfile指令导出的数据一般是以制表符分隔的。这种文本格式既方便人去阅读,也适合程序处理,一般某列相似于数据库中的关键字。join命令就是一 个根据关键字合并数据文件的命令(join lines of two files on a common field),相似于数据库中两张表关联查询。spa
经常使用参数orm
join命令根据公共字段(关键字)来合并两个文件的数据行。所以最简单的使用方式就是指定两个数据文件名,这两个文件的第一列就是公共字段,字段 之间以空白分隔。(For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by whitespace. When FILE1 or FILE2 (not both) is -, read standard input.)ci
内链接(inner join) 格式:join <FILE1> <FILE2>rem
左链接(left join, 左外链接, left outer join) 格式:join -a1 <FILE1> <FILE2>
右链接(right join, 右外链接,right outer join) 格式:join -a2 <FILE1> <FILE2>
全链接(full join, 全外链接, full outer join) 格式:join -a1 -a2 <FILE1> <FILE2>
指定分隔符:
-t <CHAR>
好比:-t ':' 使用冒号做为分隔符。默认的分隔符是空白。
指定输出字段:
-o <FILENO.FIELDNO> ...
其中FILENO=1表示第一个文件,FILENO=2表示第二个文件,FIELDNO表示字段序号,从1开始编号。默认会所有输出,但关键字列只输出一次。
好比:-o 1.1 1.2 2.2 表示输出第一个文件的第一个字段、第二个字段,第二个文件的第二个字段。
使用示例示例一 内链接(忽略不匹配的行)
不指定任何参数的状况下使用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@rhel55 linux]# join -t ':' /etc/passwd /etc/shadow