수천 줄이 포함된 두 개의 파일이 있습니다. 다음을 사용하여 라인/바이트 차이 비율을 얻고 싶습니다.차이점,Wimdiv또는 구체적인 차이점에 관계없이 다른 명령을 사용할 수 있습니다.
답변1
diffstat
당신이 찾고있는 것과 비슷한 도구가 있습니다 .
$ diff <file1> <file2> | diffstat
예
$ diff afpuri.c afpuri1.c | diffstat
unknown | 53 ++++++++++++++++++++---------------------------------
1 file changed, 20 insertions(+), 33 deletions(-)
diff
이는 트리에 여러 파일이 포함된 출력에도 사용할 수 있습니다.
인용하다
답변2
내 계산이 약간 틀렸을 수도 있지만 귀하가 비율을 요구하고 있다고 믿으며 이것이 비율을 산출한다고 믿습니다.
#!/usr/bin/env bash
# File 1 contains 1,2,3,4,5 on new lines
# File 2 contains 1,2,3,4,5,6,7,8,9,10 on new lines.
# Compare differentials side-by-side
diff -y 1 2 > diff
# Print lines to file which do not contain ">" prefix.
sed 's/[ ]/d' diff > MATCHES
# Print lines to file which do contain ">" prefix.
sed '/[>]/!d' diff > DIFFS
# Count lines in file that contains MATCHES between Versions of Files 1,2.
MATCHES=$(wc -l MATCHES | sed 's/[^0-9]*//g')
# Count lines in file that DID NOT MATCH between Version of Files 1,2.
DIFFS=$(wc -l DIFFS | sed 's/[^0-9]*//g')
# Sed here is stripping all but the number of lines in file.
RATIO=$(echo "$MATCHES / $DIFFS" | bc -l)
# To get the ratio, we are echoing the #of_matches and the #of_diffs to
# the bc -l command which will give us a float, if we need it.
echo "You've got:" $RATIO "differential."
# Bytes...
# stat -c%s prints bytes to variable
MATCHES=$(stat -c%s MATCHES)
DIFFS=$(stat -c%s DIFFS)
RATIO_BYTE=$(echo "$MATCHES / $DIFFS" | bc -l)
echo "Let Ratio in Bytes be" $RATIO_BYTE
# Again, we divide the matches by the diffs to reach the "ratio" of
# differences between the files.