求两个Linux文本文件的交集、差集、并集
一、交集
sort a.txt b.txt | uniq -d二、并集
sort a.txt b.txt | uniq三、差集
a.txt-b.txt:sort a.txt b.txt b.txt | uniq -ub.txt-a.txt:sort b.txt a.txt a.txt | uniq -u?
四、相關(guān)的解釋
使用sort可以將文件進(jìn)行排序(sort排序是為了管道交給uniq進(jìn)行處理,uniq只能處理相鄰的行),可以使用sort后面的參數(shù),例如 -n 按照數(shù)字格式排序,例如 -i 忽略大小寫,例如使用-r 為逆序輸出等
uniq為刪除文件中重復(fù)的行,得到文件中唯一的行,參數(shù)-d 表示的是輸出出現(xiàn)次數(shù)大于1的內(nèi)容;參數(shù)-u表示的是輸出出現(xiàn)次數(shù)為1的內(nèi)容;那么對(duì)于上述的求交集并集差集的命令做如下的解釋:
sort a.txt b.txt | uniq -d:將兩個(gè)文件進(jìn)行排序,uniq使得兩個(gè)文件中的內(nèi)容為唯一的,使用-d輸出兩個(gè)文件中次數(shù)大于1的內(nèi)容,即是得到交集
sort a.txt b.txt | uniq :將兩個(gè)文件進(jìn)行排序,uniq使得兩個(gè)文件中的內(nèi)容為唯一的,即可得到兩個(gè)文件的并集
sort a.txt b.txt b.txt | uniq -u:將兩個(gè)文件排序,最后輸出a.txt b.txt b.txt文件中只出現(xiàn)過一次的內(nèi)容,因?yàn)橛袃蓚€(gè)b.txt所以只會(huì)輸出只在a.txt出現(xiàn)過一次的內(nèi)容(b.txt的內(nèi)容至少出現(xiàn)兩次),即是a.txt-b.txt差集;對(duì)于b.txt-a.txt同理。
樣例
#?a.hosts
[root(0)@thatsit 11:40:46 ~/scripts]# cat a.hosts 10.10.1.101 10.10.1.102 10.10.1.103 10.10.1.104 [root(0)@thatsit 11:40:47 ~/scripts]#
# b.hosts
?
#?a.hosts ∩ b.hosts
[root(0)@thatsit 11:40:49 ~/scripts]# sort a.hosts b.hosts | uniq -d 10.10.1.101 10.10.1.103 [root(0)@thatsit 11:41:08 ~/scripts]#?
#?a.hosts ∪ b.hosts
#?a.hosts - b.hosts
# b.hosts -?a.hosts?
[root(0)@thatsit 11:41:47 ~/scripts]# sort a.hosts a.hosts b.hosts | uniq -u 10.10.1.105 [root(0)@thatsit 11:41:55 ~/scripts]#?
參考鏈接:http://www.cnblogs.com/molong1208/p/5358509.html
?
轉(zhuǎn)載于:https://www.cnblogs.com/softidea/p/10253896.html
總結(jié)
以上是生活随笔為你收集整理的求两个Linux文本文件的交集、差集、并集的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王者荣耀怎么邀请好友加入战队
- 下一篇: 搜索小猿搜题app(搜狗搜索引擎)