簡體   English   中英

bash刪除文件中的相同內容

[英]bash remove the same in file

我有一個問題,讓數字不同的字符串。

我有兩個文件,例如:

文件1:

aaa1

aaa4

bbb3

ccc2

文件2:

bbb3

ccc2

aaa4

如何從中獲取值1(在這種情況下為aaa1字符串原因)?

我有一個查詢,但它不僅計算不同的字符串,而且還考慮了行的順序。

diff file1 file2 | grep "<" | wc -l

謝謝。

您可以將grep -v -c與其他選項一起使用,如下所示:

grep -cvwFf file2 file1
1

使用的選項有:

-c - get the count of matches
-v - invert matches
-w - full word match (to avoid partial matches)
-F - fixed string match
-f - Use a file for matching patterns

據我了解您的要求,在diff之前對文件進行排序是一種快速的解決方案:

sort file1 > file1.sorted
sort file2 > file2.sorted
diff file1.sorted file2.sorted | egrep "[<>]" | wc -l

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM