首先贴一个,join --help

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 or , 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 `- FIELD - 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
- FIELD join on this FIELD of file
- FIELD join on this FIELD of file
--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 . 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. Report bugs to <bug-coreutils@gnu.org>.

然后来理解下。

join 【命令选项】 文件1 文件2

//命令选项可以很多, 但文件只能是两个

先从重要的开始说,join 的作用是把两个文件对一列求交集,然后输出交集部分。

来先看个基本的例子:

$ cat A.txt

 abc
ccc
sed
4 xxx
$ cat B.txt

 h
x
b
s
$ join A.txt B.txt

 abc  h
ccc x
sed b

为什么得到上面的结果,因为join默认使用空格作为分隔符(可以使用-t设定分割符),使用第一行作为主列(用于求交集的列)。

如果要将所有内容都出来呢,不管有没有配对。可以使用-a命令。

$ join -a1 A.txt B.txt
abc h
ccc x
sed b
xxx

//可以发现,A.txt中没有配对的内容在文件的末尾被输出了。

同样可以把A.txt 和 B.txt都输出来。

$ join -a1 -a2 A.txt B.txt
abc h
ccc x
sed b
xxx
s

但是这时候却发现,排版和我们想的不一样。最后两行根本分不清是来战A.txt还是B.txt。

这时候就要用-o命令和-e命令了。

$ join -a1 -a2 -e"_" -o'1.1 1.2 1.3 2.1 2.2 2.3' A.txt B.txt
abc h
ccc x
sed b
xxx _ _ _
_ _ _ s

其中-e表示如果元素不存在时填充什么, -o 表示以哪种形式输出(1.1 表示文件1中的第一列)。

如何求A.txt中有,而B.txt中没有的呢?

这时候就需要使用-v了

join -v1 A.txt B.txt
xxx

输出了A中有而B中没有的部分。

另外-i 忽略大小写

-j x 相当于同时写了-1x -2x

也就是指定两个文件的x列作为主列。

join内部是怎么实现的呢,我们来看join中的重要要求,每个文件的主列都必须是排好序的!!!

是不是一下就知道了join是怎么实现的了,就是两个有序的数组求交集嘛。是不是对join的复杂度也有了更深的理解。忽略列的大小的情况下,O(n + m)就可以完成了,其中n为文件1的行数,m是文件2的行数。

shell join详解的更多相关文章

  1. adb shell 命令详解,android

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  2. 【Devops】【docker】【CI/CD】关于jenkins构建成功后一步,执行的shell命令详解+jenkins容器运行宿主机shell命令的实现方法

    1.展示这段shell命令 +详解 #================================================================================= ...

  3. Linux主要shell命令详解(上)

    [摘自网络] kill -9 -1即实现用kill命令退出系统 Linux主要shell命令详解 [上篇] shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的 ...

  4. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  5. 二分算法题目训练(一)——Shell Pyramid详解

    HDU2446——Shell Pyramid 详解 Shell Pyramid 题目描述(Google 翻译的) 在17世纪,由于雷鸣般的喧嚣,浓烟和炽热的火焰,海上的战斗与现代战争一样.但那时,大炮 ...

  6. linux shell `符号详解

    linux shell `符号详解 <pre>[root@iZ23uewresmZ arjianghu]# echo `ls`asss.html common guaji.php imag ...

  7. Shell test命令(Shell [])详解,附带所有选项及说明

    test 是 Shell 内置命令,用来检测某个条件是否成立.test 通常和 if 语句一起使用,并且大部分 if 语句都依赖 test. test 命令有很多选项,可以进行数值.字符串和文件三个方 ...

  8. hadoop Shell命令详解

    调用文件系统(FS)Shell命令应使用bin/hadoop fs <args>的形式.所有的的FS shell命令使用URI路径作为参数.URI路径详解点击这里. 1.cat说明:将路径 ...

  9. Hive Shell 命令详解

    Hive服务介绍 Hive默认提供的cli(shell)服务,如果需要启动其他服务,那么需要service参数来启动其他服务,比如thrift服务.metastore服务等.可以通过命令hive -- ...

随机推荐

  1. 【荐】详解 golang 中的 interface 和 nil

    golang 的 nil 在概念上和其它语言的 null.None.nil.NULL一样,都指代零值或空值.nil 是预先说明的标识符,也即通常意义上的关键字.在 golang 中,nil 只能赋值给 ...

  2. JSON序列——根据JSON生成事务性SQL2

    JSON序列——根据JSON生成事务性SQL2 procedure TForm1.Button3Click(Sender: TObject); begin var json:string:=''+ ' ...

  3. Intellij IDEA 通过数据库表逆向生成带注释的实体类文件超级详细步骤,附详细解决方案

    参考:https://blog.csdn.net/qq_34371461/article/details/80571281  https://blog.csdn.net/mqchenrong/arti ...

  4. addEventListener 第三个参数

    addEventListener api target.addEventListener(type, listener[, options]); target.addEventListener(typ ...

  5. MySql之视图的使用

    一:视图是什么 视图相当于一个窗口,一个基于具体数据库表.定义了相关查询规则 的窗口. 建立视图,可以重用一些复杂的查询语句.    建立视图,相当于定义了一系列查询操作:从视图查询数据,相当于在调用 ...

  6. virtualbox centos安装增强工具和Centos与VirtualBox共享文件夹设置

    VirtualBox 大家都习惯性把它简称为 Vbox ,比 VM 的体积小.开源.速 度快.不过在使用 VirtualBox  在虚拟机中安装 CentOS 之后,不能直接运行安装好 Virtual ...

  7. winform的combox下拉框绑定数据源

    List<Site> list = new List<Site>(); foreach (DataRow srcDr in srcDt.Rows) { list.Add(new ...

  8. 【Babble】批量学习与增量学习、稳定性与可塑性矛盾的乱想

    一.开场白 做机器学习的对这几个词应该比较熟悉了. 最好是拿到全部数据,那就模型慢慢选,参数慢慢调,一轮一轮迭代,总能取得不错效果. 但是面对新来数据,怎么能利用已经训练好的模型,把新的信息加进去? ...

  9. 100个MySQL 的调节和优化的提示

    100个MySQL 的调节和优化的提示 MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧. ...

  10. SNF快速开发平台MVC-富文本控件集成了百度开源项目editor

    一.效果如下: 二.在框架当中调用代码如下: 1.在js里配置如下: <script type="text/javascript"> var viewModel =fu ...