首先贴一个,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. AngularJS中介者模式实例

    在任何应用程序中,中介者模式随处可见. → 有一个事件源,触发事件,传递参数→ 中介者记下这个事件,向外界广播,并带上参赛→ 有一个地方侦听中介者事件,一旦事件源触发事件,就从中介者手里获取事件相关参 ...

  2. android:应用性能优化SparseArray

    HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果.最近在做一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performanc ...

  3. angularjs drag and drop

    angular-dragula Drag and drop so simple it hurts 480 live demo angular-drag-and-drop-lists Angular d ...

  4. 6种常见的Git错误以及解决的办法

    我们都会犯错误,尤其是在使用像Git这样复杂的东西时.如果你是Git的新手,可以学习如何在命令行上开始使用Git.下面介绍如何解决六个最常见的Git错误. Photo by Pawel Janiak  ...

  5. linux 重启和关机

    重启命令有:reboot,  shutdown -ry 0关机命令有:poweroff, shutdown -hy 0

  6. pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. 4. OpenAI GPT算法原理解析

    1. 语言模型 2. Attention Is All You Need(Transformer)算法原理解析 3. ELMo算法原理解析 4. OpenAI GPT算法原理解析 5. BERT算法原 ...

  8. [转]《RabbitMQ官方指南》安装指南

    原文链接   翻译:xiezc 目录(其中的文章后续翻译): Windows下安装 Debian / Ubuntu下安装 基于RPM的Linux下安装 Mac OS X下安装 Homebrew安装 W ...

  9. Spring Security 使用数据库用户进行认证

    本文参考或摘录自:http://haohaoxuexi.iteye.com/blog/2157769 本文使用Spring Security自带的方式连接数据库对用户进行认证. 1.Spring Se ...

  10. 在Android 5.0中使用JobScheduler(转载)

    翻译见:http://blog.csdn.net/bboyfeiyu/article/details/44809395 In this tutorial, you will learn how to ...