Shell: Find Matching Lines Across Many Files

前端 未结 4 1104
-上瘾入骨i
-上瘾入骨i 2021-01-03 02:43

I am trying to use a shell script (well a \"one liner\") to find any common lines between around 50 files. Edit: Note I am looking for a line (lines) that a

4条回答
  •  盖世英雄少女心
    2021-01-03 03:23

    Combining this two answers (ans1 and ans2) I think you can get the result you are needing without sorting the files:

    #!/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
    

    Simply save it, give it execution rights (chmod +x compareFiles.sh) and run it. It will take all the files present in the current working directory and will make an all-vs-all comparison leaving in the "matching_lines" file the result.

    Things to be improved:

    • Skip directories
    • Avoid comparing all the files two times (file1 vs file2 and file2 vs file1).
    • Maybe add the line number next to the matching string

    Hope this helps.

    Best,

    Alan Karpovsky

提交回复
热议问题