首先贴一个,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. windows命名管道

    命名管道是通过网络来完成进程间的通信,它屏蔽了底层的网络协议细节. 将命名管道作为一种网络编程方案时,它实际上建立了一个C/S通信体系,并在其中可靠的传输数据.命名管道服务器和客户机的区别在于:服务器 ...

  2. Maven学习总结(11)——Maven Tomcat7自动部署

    1.配置tomcat-users.xml文件 在tomcat安装目录下找到tomcat-users.xml文件.该文件路径为[tomcat安装根目录]/conf/ 修改文件内容,增加下列内容: < ...

  3. python测试开发django-47.xadmin上传图片和文件

    前言 xadmin上传图片和上传文件功能 依赖环境 如果没安装Pillow的话,会有报错:practise.Upload.upload_image: (fields.E210) Cannot use ...

  4. oracle的start with connect by prior如何使用

    oracle的start with connect by prior是根据条件递归查询"树",分为四种使用情况: 第一种:start with 子节点ID='...' connec ...

  5. C++ - 定义无双引号的字符串宏

    在某些特殊场合下,我们可能需要定义一个字符串宏,但又不能用双引号 比如像这样 #define HELLO hello world 如果我们只是简单的展开HELLO,肯定会无法编译 std::cout ...

  6. [Java] Hashtable 源码简要分析

    Hashtable /HashMap / LinkedHashMap 概述 * Hashtable比较早,是线程安全的哈希映射表.内部采用Entry[]数组,每个Entry均可作为链表的头,用来解决冲 ...

  7. How to measure IOPS for VMware

    http://blog.synology.com/blog/?p=2225 Executive SummaryThis article, intended towards IT Professiona ...

  8. ORA-00959: tablespace 'TB01' does not exist

    当前的表空间如下: SQL> select name from v$tablespace; NAME ---------------------------------------------- ...

  9. android用TextView实现跑马灯效果

    今天搞啦很久,其实很简单,就加几个属性就可以啦! 图如下 : 有的说要重写TextView方法,有的说要设置固定长度,但是我没重写也没有设置固定长度也弄出来啦!跑在2.3.3的手机上面.就是不知道其他 ...

  10. SNF开发平台WinForm-审核流使用方法样例

    一.效果如下: 二.如何实现 1.程序的数据表设计规范,参考<09.SNF-C#编程规范V1.5.docx>文件. 2.程序操作程序 2.1.在程序页面拖拽控件 2.2.程序的Load事件 ...