如何在不同和uniq中找到两个文件内容

时间:2014-10-09 12:29:46

标签: shell awk sed grep cut

我需要通过两个不同的文件获得差异和uniq输出。

此时就把one.txt存盘

The lightning and thunder
They go and come;
But the stars and the stillness
Are always at home.

two.txt

The lightning and thunder
They go and come;
Tyger! Tyger! burning bright
In the forests of the night,
What immortal hand or eye
But the stars and the stillness
Are always at home.

我希望以两种不同的方式获得输出。

same.txt

The lightning and thunder
They go and come;
But the stars and the stillness
Are always at home.

diff.txt

Tyger! Tyger! burning bright
In the forests of the night,
What immortal hand or eye
But the stars and the stillness

3 个答案:

答案 0 :(得分:2)

假设您的文件one.txt包含two.txt,您可以将grep-f一起使用。然后,-v反转输出。

匹配的内容:

grep -f f1 f2 > same.txt

见输出:

$ cat same.txt
The lightning and thunder
They go and come;
But the stars and the stillness
Are always at home.

有什么不同:

grep -vf f1 f2 > diff.txt

见输出:

$ cat diff.txt
Tyger! Tyger! burning bright
In the forests of the night,
What immortal hand or eye

来自man grep

  

-f FILE, - file = FILE

     

从FILE获取模式,每行一个。空文件包含零   模式,因此没有匹配。 (-f由...指定   POSIX)

     

-v , - 反向匹配

     

反转匹配感,选择不匹配的线条。 (-v是   由POSIX指定。)

答案 1 :(得分:0)

巧合:

grep -f one.txt two.txt > same.txt

对于差异:

cat one.txt two.txt| grep -f same.txt

答案 2 :(得分:0)

如果文字顺序无关紧要。你可以排序并做一个通讯。

排序和存储

 sort one.txt > one_sorted.txt
 sort two.txt > two_sorted.txt

之后做通讯

 comm -3 one_sorted.txt two_sorted.txt > diff.txt 
 comm -12 one_sorted.txt two_sorted.txt > same.txt