我确信我曾经发现过一个shell命令,它可以从两个或多个文件中打印公共行。它叫什么名字?

它比diff简单多了。


当前回答

如果这两个文件还没有排序,你可以使用:

comm -12 <(sort a.txt) <(sort b.txt)

它将工作,避免错误消息comm: file 2不是有序的 当执行comm -12 a.t xxb .txt时。

其他回答

有关如何为多个文件执行此操作,请参阅跨多个文件查找匹配行的链接答案。


结合这两个答案(答案1和答案2),我认为你可以得到你需要的结果,而不需要对文件进行排序:

#!/bin/bash
ans="matching_lines"

for file1 in *
do 
    for file2 in *
        do 
            if  [ "$file1" != "$ans" ] && [ "$file2" != "$ans" ] && [ "$file1" != "$file2" ] ; then
                echo "Comparing: $file1 $file2 ..." >> $ans
                perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' $file1 $file2 >> $ans
            fi
         done 
done

只需保存它,赋予它执行权限(chmod +x compareFiles.sh)并运行它。它将获取当前工作目录中的所有文件,并进行all-vs-all比较,将结果留在“matching_lines”文件中。

需要改进的地方:

跳过目录 避免将所有文件进行两次比较(file1 vs file2和file2 vs file1)。 可以在匹配的字符串旁边添加行号

rm file3.txt

cat file1.out | while read line1
do
        cat file2.out | while read line2
        do
                if [[ $line1 == $line2 ]]; then
                        echo $line1 >>file3.out
                fi
        done
done

这个应该可以了。

awk 'NR==FNR{a[$1]++;next} a[$1] ' file1 file2

在受限版本的Linux上(比如我正在开发的QNAP (NAS)):

Comm并不存在 正如@ChristopherSchultz所说,grep -f -f file1 file2可能会导致一些问题,使用grep -f -f file1 file2真的很慢(超过5分钟-没有完成-在超过20mb的文件上使用下面的方法超过2-3秒)

这就是我所做的:

sort file1 > file1.sorted
sort file2 > file2.sorted

diff file1.sorted file2.sorted | grep "<" | sed 's/^< *//' > files.diff
diff file1.sorted files.diff | grep "<" | sed 's/^< *//' > files.same.sorted

如果files.same.sorted应该与原始文件的顺序相同,那么添加这一行与file1的顺序相同:

awk 'FNR==NR {a[$0]=$0; next}; $0 in a {print a[$0]}' files.same.sorted file1 > files.same

或者,对于与file2相同的顺序:

awk 'FNR==NR {a[$0]=$0; next}; $0 in a {print a[$0]}' files.same.sorted file2 > files.same

你要找的命令是通讯

comm -12 1.sorted.txt 2.sorted.txt

在这里:

-1:删除列1 (1.sort .txt中唯一的行)

-2:删除列2 (2.sort .txt中唯一的行)