我有一系列的文本文件,我想知道它们的共同之处,而不是它们之间的不同之处。命令行Unix或Windows都可以。
文件foo
linux-vdso.so.1 => (0x00007fffccffe000)
libvlc.so.2 => /usr/lib/libvlc.so.2 (0x00007f0dc4b0b000)
libvlccore.so.0 => /usr/lib/libvlccore.so.0 (0x00007f0dc483f000)
libc.so.6 => /lib/libc.so.6 (0x00007f0dc44cd000)
文件栏
libkdeui.so.5 => /usr/lib/libkdeui.so.5 (0x00007f716ae22000)
libkio.so.5 => /usr/lib/libkio.so.5 (0x00007f716a96d000)
linux-vdso.so.1 => (0x00007fffccffe000)
因此,根据上面的两个文件,所需实用程序的输出将类似于file1:line_number, file2:line_number == matching text
(这只是一个建议;我真的不关心语法是什么):
foo:1, bar:3 == linux-vdso.so.1 => (0x00007fffccffe000)
发布于 2009-04-14 05:41:27
在*nix上,您可以使用comm。问题的答案是:
comm -1 -2 file1.sorted file2.sorted
# where file1 and file2 are sorted and piped into *.sorted
下面是comm
的完整用法
comm [-1] [-2] [-3 ] file1 file2
-1 Suppress the output column of lines unique to file1.
-2 Suppress the output column of lines unique to file2.
-3 Suppress the output column of lines duplicated in file1 and file2.
另请注意,在使用comm之前对文件进行排序非常重要,如手册页中所述。
发布于 2009-04-14 06:05:23
之前有人在这里问过:
您也可以尝试使用Perl (credit goes here):
perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' file1 file2
发布于 2014-09-08 08:47:23
我刚从答案中学习了命令,但是我想添加一些额外的东西:如果文件没有排序,并且您不想接触原始文件,那么可以通过管道输出命令。这将使原始文件保持不变。它在Bash中可以工作,但我不能说其他shell。
comm -1 -2 <(sort file1) <(sort file2)
这可以扩展为比较命令输出,而不是文件:
comm -1 -2 <(ls /dir1 | sort) <(ls /dir2 | sort)
https://stackoverflow.com/questions/746458
复制相似问题