首先贴一个,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. UVA11137 Ingenuous Cubrency 完全背包 递推式子

    做数论都做傻了,这道题目 有推荐,当时的分类放在了递推里面,然后我就不停的去推啊推啊,后来推出来了,可是小一点的数 输出答案都没问题,大一点的数 输出答案就是错的,实在是不知道为什么,后来又不停的看, ...

  2. IntentService 服务 工作线程 stopself MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. SQL SERVER 2005 数据库置疑修复

    alter database 置疑数据库 set emergency go alter database 置疑数据库 set single_user with rollback immediate g ...

  4. 微软补丁安装工具wusa报错。

    命令行需要msu格式的补丁安装文件的全路径,否则报错.

  5. linux select函数详解

    linux select函数详解 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数的参数会告诉内核: •我们所关心的文件描述符 •对每个描述符,我们所关心的状 ...

  6. DNS-320 B2 语言包

    神一样的NAS啊,这个语言包在这里http://tsd.dlink.com.tw/downloads2008detailgo.asp,选择sc的就可以了. 真是神一样的配置~ 佩服死d-link了

  7. 0x01 Spring Cloud 概述

    Spring Cloud Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,领导选举,分 ...

  8. android 性能优化-工具篇

    一.Traceview 使用TraceView主要有两种方式: 1.直接打开DDMS,选择一个进程,然后按上面的“Start Method Profiling”按钮,等红色小点变成黑色以后就表示Tra ...

  9. 超级NB的防DDOS(小量级)攻击的脚本

    # tree /usr/local/ddos/ /usr/local/ddos/ ├── ddos.conf ├── ddos.sh ├── ignore.ip.list └── LICENSE di ...

  10. Blink

    https://help.aliyun.com/document_detail/66088.html?spm=a2c4g.11186623.6.602.58ff5686FP4Ihh