A(1,2,3)和B(3,4,5),A和B的交集是3,A对B的差集是1和2,B对A的差集是4和5,A和B求差的结果是1、2、4、5。

在Linux中可以使用comm命令求出这些集。

[root@xuexi tmp]# cat <<eof>set1.txt
> orange
> gold
> apple
> sliver
> steel
> iron
> eof
[root@xuexi tmp]# cat <<eof>set2.txt
> orange
> gold
> cookiee
> carrot
> eof

使用comm命令。

[root@xuexi tmp]# comm set1.txt set2.txt
apple
orange
comm: file is not in sorted order
comm: file is not in sorted order
gold
cookiee
carrot
silver
steel
iron

提示没有排序,所以comm必须要保证比较的文件是有序的。

[root@xuexi tmp]# sort set1.txt -o set1.txt;sort set2.txt -o set2.txt
[root@xuexi tmp]# comm set1.txt set2.txt
apple
carrot
cookiee
gold
iron
orange
silver
steel

结果中输出了3列,每一列使用制表符\t隔开。第一列是set1.txt中有而set2.txt中没有的,第二列则是set2.txt中有而set1.txt中没有的,第三列是set1.txt和set2.txt中都有的。

根据这三列就可以求出交集、差集和求差。

交集就是第三列。使用-1和-2分别删除第一第二列就是第三列的结果。

[root@xuexi tmp]# comm set1.txt set2.txt - -
gold
orange

A对B的差集就是第一列,B对A的差集就是第二列。

[root@xuexi tmp]# comm set1.txt set2.txt  - -  # A对B的差集
apple
iron
silver
steel
[root@xuexi tmp]# comm set1.txt set2.txt  - -   # B对A的差集
carrot
cookiee

A和B的求差就是第一列和第二列的组合。

[root@xuexi tmp]# comm set1.txt set2.txt  -
apple
carrot
cookiee
iron
silver
steel

但是这样分两列的结果不方便查看,应该进行处理使它们显示在同一列上。

[root@xuexi tmp]# comm set1.txt set2.txt  - | tr "\t" "\0"
apple
carrot
cookiee
iron
silver
steel

Linux comm命令求出文件的交集、差集的更多相关文章

  1. Linux ln命令:在文件之间建立链接(硬链接和软链接)详解版1

    Linux ln命令:在文件之间建立链接(硬链接和软链接)详解版 < Linux创建文件及修改文件时间戳(touch命令)Linux复制文件和目录(cp命令) > <Linux就该这 ...

  2. 如何将Linux rm命令删除的文件放入垃圾箱

    因为rm命令删除的文件是不会放入垃圾箱的,所以无法恢复,下面小编就给大家介绍一种方法,通过替换Linux rm命令的方法,从而将rm命令删除的文件放入垃圾箱. 方法: 1. 在/home/userna ...

  3. Linux— file命令 用于辨识文件类型

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  4. 原!linux comm命令文件 比较 输出交集,差集。

    文件内容大致如下: 112805|300011222483|OL海12卓|47397c33e36cdbed26637c50dd305973|2019-08-06 10:50:13|登B录123|suc ...

  5. Linux常用命令01(文件和目录)

    目标 查看目录内容 ls 切换目录 cd 创建和删除文件 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 > 和 &g ...

  6. linux常用命令之查阅文件

    CAT cat – concatenate print files 连续的输出文件内容 用法 cat [-nbA] file 选项 -n line number 输出行号 -b line number ...

  7. Linux常用命令_(文件权限)

    Linux权限管理: 二.权限管理1.3种基本权限在Linux中,将使用系统资源的人员分为4类:超级用户.文件或目录的属主.属主的同组人和其他人员.超级用户拥有对Linux系统一切操作权限,对 于其他 ...

  8. Linux常用命令学习2---(文件搜索命令locate find、命令搜索命令whereis which、字符串搜索命令grep、帮助命令man)

     1.文件搜索命令:locate [文件名]    在后台数据库中按文件名搜索,搜索速度比find快,耗费资源更少    例子:locate test.txt,就会显示文件名包含 test.txt的所 ...

  9. linux常用命令:创建文件和文件夹

    1.首先说一下touch 创建文件的命令,touch可以用于创建二进制文件,用法非常简单.用法:touch+文件名,touch与文件名之间一定要有空格.图中先用之前分享过的命令来查看一下/目录下面有多 ...

随机推荐

  1. html文件上传保存-(.html and string translate into .html )

    //上传h5编辑器编辑的html内容 uploadHtml(newsId?: any) { const news = newsId !== undefined ? newsId : 'new'; le ...

  2. Normalize.css & Reset

    Normalize.css: try to keep the style consistent in every browser. Reset: clear style in every browse ...

  3. Atomic in Redis

    Since Redis is single-threaded, everything is atomic.

  4. 了解一下Ubuntu系统

    百度百科: ubuntu系统基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一个最新的.同时又相当稳定的主要由自由软件构建而成的操作系统,它可免费使用,并带有社团及专业 ...

  5. 一、Java和JavaScript

    JavaScript诞生于1995年,所以他得叫我一声姐姐,(*^__^*) .当时它的主要任务就是表单验证,在还没JavaScript的时候,进行表单验证的时候必须要把数据提交到服务器,才能进行表单 ...

  6. maven项目(多模块)

    在eclipse下构建maven项目,该项目由多个子模块组成. 1.创建一个父项目 NEW -->project-->maven-->maven Project,点击下一步,进入ne ...

  7. 工具安装(mac)

    1. iterm 2. nvm 3. node 4. git 5. VScode 6. postman 1.安装iterm2安装路径 https://www.iterm2.com/使用技巧https: ...

  8. display: table-cell的实用应用

    概述 之前工作中碰到了一个垂直居中问题,最后通过查资料利用table-cell解决.于是打算总结一下有关table-cell的应用,记录下来,供以后开发时参考,相信对其他人也有用. 参考资料:我所知道 ...

  9. MySQL数据库的锁机制

    在并发访问情况下,很有可能出现不可重复读等等读现象.为了更好的应对高并发,封锁.时间戳.乐观并发控制(乐观锁).悲观并发控制(悲观锁)都是并发控制采用的主要技术方式. 锁分类 ①.按操作划分:DML锁 ...

  10. 使用PageHepler分页

    首先需要引入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>p ...