日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

求两个Linux文本文件的交集、差集、并集

發布時間:2023/12/31 linux 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求两个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

?

四、相關的解釋

使用sort可以將文件進行排序(sort排序是為了管道交給uniq進行處理,uniq只能處理相鄰的行),可以使用sort后面的參數,例如 -n 按照數字格式排序,例如 -i 忽略大小寫,例如使用-r 為逆序輸出等

uniq為刪除文件中重復的行,得到文件中唯一的行,參數-d 表示的是輸出出現次數大于1的內容;參數-u表示的是輸出出現次數為1的內容;那么對于上述的求交集并集差集的命令做如下的解釋:

sort a.txt b.txt | uniq -d:將兩個文件進行排序,uniq使得兩個文件中的內容為唯一的,使用-d輸出兩個文件中次數大于1的內容,即是得到交集

sort a.txt b.txt | uniq :將兩個文件進行排序,uniq使得兩個文件中的內容為唯一的,即可得到兩個文件的并集

sort a.txt b.txt b.txt | uniq -u:將兩個文件排序,最后輸出a.txt b.txt b.txt文件中只出現過一次的內容,因為有兩個b.txt所以只會輸出只在a.txt出現過一次的內容(b.txt的內容至少出現兩次),即是a.txt-b.txt差集;對于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

[root(0)@thatsit 11:40:48 ~/scripts]# cat b.hosts 10.10.1.101 10.10.1.103 10.10.1.105 [root(0)@thatsit 11:40:49 ~/scripts]#

?

#?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

[root(0)@thatsit 11:41:10 ~/scripts]# sort a.hosts b.hosts | uniq 10.10.1.101 10.10.1.102 10.10.1.103 10.10.1.104 10.10.1.105 [root(0)@thatsit 11:41:19 ~/scripts]#


#?a.hosts - b.hosts

[root(0)@thatsit 11:41:25 ~/scripts]# sort a.hosts b.hosts b.hosts | uniq -u 10.10.1.102 10.10.1.104 [root(0)@thatsit 11:41:45 ~/scripts]#

# 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

?

轉載于:https://www.cnblogs.com/softidea/p/10253896.html

總結

以上是生活随笔為你收集整理的求两个Linux文本文件的交集、差集、并集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。