在file2中不存在的未分类文件1中显示行



我有2个文本文件, file1 file2 。我需要在 file1 中输出这些行,这些行不存在于 file2 中。

两个文件中的行都不以相同的顺序,并且在 file2 中有其他行,这是无关紧要的。

例如:

$ grep testme file1
chmod -f 644 /root/testme
chown -h root:root /root/testme
$ grep testme file2
chmod -f 777 /root/testme
chown -h dude:dude /root/testme

上面的grep file1 中的2行的一个示例,它们是不同的,或者在 file2 中不存在。这两个文件均为5 MB。

在Linux Bash(RHEL 7(中使用以下步骤

while read -r line; do
 [ "$(grep "^$line$" file2 )" ] || echo $line
done < file1

显示...

chmod -f 644 /root/testme
chown -h root:root /root/testme
grep: Unmatched [ or [^`

...并且不会完成。这也需要很长时间。

我尝试了另一种方法,该方法很快,但是只打印了两行中的1条:

$ fgrep -v -f file2 file1
chmod -f 644 /root/testme
$

为什么只显示1行?

两行在文件的中间,由cr。

终止

有什么想法吗?谢谢!

非常感谢使用通讯的帮助和建议,但同时我使用 grep找到了答案。我更喜欢grep,因为它比comm快得多,也不需要对输入进行排序。

$ fgrep -v -x -f file2 file1
chmod -f 644 /root/testme
chown -h root:root /root/testme

解决方案只是添加-x

-x仅选择与整行完全匹配的那些匹配

combine也可以做到这一点:

combine <(seq 5 10) not <(seq 1 2 20)
6
8
10

相关内容

最新更新