Как я могу получить отношение различия с помощью “разности” или другой команды?

Можно использовать строковые функции замены удара для этого:

for file in /tmp/p/DSC*.JPG; do
  cp "$file" "${file%.JPG}"_orig.JPG
done

Общий формат ${string%substring} который удалит substring от конца string. Например:

$ f=foobar.JPG; echo "${f%.JPG}"
foobar

3
06.10.2014, 05:38
2 ответа

Существует инструмент Diffstat , который звучит так, как вы ищете.

$ diff <file1> <file2> | diffstat

Пример

$ diff afpuri.c afpuri1.c | diffstat
 unknown |   53 ++++++++++++++++++++---------------------------------
 1 file changed, 20 insertions(+), 33 deletions(-)

Это можно использовать для Diff , который также включает несколько файлов в дереве.

Список литературы

7
27.01.2020, 21:10

Моя математика может быть немного выключена, но я считаю, что вы просили соотношение И я верю, что это производит соотношение.

#!/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.
2
27.01.2020, 21:10

Теги

Похожие вопросы