両方のディレクトリで最も一致するファイルを探します。

両方のディレクトリで最も一致するファイルを探します。

次の問題に対する解決策を見つけようとしています。 2つのファイルセットがあります。

  • フォルダAには約400個のテキストファイルが含まれています。
  • フォルダBには、複数のサブフォルダに合計約20,000のテキストファイルが含まれています。

フォルダAのファイルは、フォルダBのファイルの変更されたバージョン、またはフォルダBのファイルの一部です。 「部分的に」という言葉は、フォルダA内のファイルにフォルダB内のファイルのテキストの一部を含めることができますが、すべてが含まれないことを意味します。

つまり、フォルダAIの各ファイルについて、フォルダAのファイルと最も類似したフォルダBのファイルを探したい。

たとえば、次の種類のレポートが必要です。

File ./A/foo123.txt most closely matches file ./B/bar243.txt with 68% of lines identical.
File ./A/bar306.txt most closely matches file ./B/foo85.txt with 30% of lines identical.

この結果を得るためにコマンドラインツールを使用できますか?それとも最良の方法は何ですか?

ベストアンサー1

次のように動作します。

for fa in A/*; do

    highest_pm=0

    for fb in B/*; do

    num_identical_lines=$(diff --unchanged-group-format='%<' --old-group-format='' --new-group-format='' --changed-group-format='' "$fa" "$fb" | wc -l)
    num_lines_file_a=$(wc -l < "$fa")

    # save permille of matching lines
    pm=$((1000*num_identical_lines/num_lines_file_a))

    # compare with highest permille
    if [ $pm -gt $highest_pm ]; then
        highest_pm=$pm
        best_match="$fb"
    fi

    done

    # output
    [ $highest_pm -gt 0 ] \
    && printf "File %s best matches File %s with %d %% of identical lines.\n" "$fa" "$best_match" $((highest_pm/10)) \
    || printf "File %s has no match\n" "$fa"

done

num_identical_lines は次のように評価されます。この回答に基づいて
残りはファイルループ、いくつかの比較、そしていくつかの出力のみです;-)

出力:

File A/file2 has no match
File A/filea best matches File B/fileb with 50 % of identical lines.

おすすめ記事