linux下join 命令

我用的命令:

join -t $'\t' -o 1.1 1.2 1.3 1.4 file1 file2

如果分隔符是tab键,不用指定-t参数的,会被当成空格处理,输出的分隔符自然也是空格了。

如果想输出的分隔符是tab键,那么就使用我上面的方法,-t $'\t' 参数。

 

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.

 

 

 

 

用途说明

Linux下最常用的数据文件格式是文本格式的,多个字段之间通过分隔符来区分,分隔符比如冒号(:)、制表符、空格等。/etc/passwd和 /etc/group就是用:来分隔的,用MySQL的into outfile指令导出的数据通常是以制表符分隔的。这种文本格式既方便人去阅读,也适合程序处理,通常某列类似于数据库中的关键字。join命令就是一 个根据关键字合并数据文件的命令(join lines of two files on a common field),类似于数据库中两张表关联查询。

常用参数

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.)

内连接(inner join) 格式:join <FILE1> <FILE2>

左连接(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





本文转自 vfast_chenxy 51CTO博客,原文链接:http://blog.51cto.com/chenxy/794626,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值