두 디렉터리에서 가장 일치하는 파일을 찾습니다.

두 디렉터리에서 가장 일치하는 파일을 찾습니다.

다음 문제에 대한 해결책을 찾으려고 노력하고 있습니다. 두 가지 파일 세트가 있습니다.

  • 폴더 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.

관련 정보