Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 带有GNU差异的百分比值_Linux_Unix_Diff - Fatal编程技术网

Linux 带有GNU差异的百分比值

Linux 带有GNU差异的百分比值,linux,unix,diff,Linux,Unix,Diff,使用diff显示两个文件之间的百分比差异的好方法是什么 例如,如果一个文件有100行,而一个副本有15行已更改,则差异百分比将为15%。可能是这样的吗 两个文件,A1和A2 $sdiff-B-B-sa1a2 | wc将给出不同的行数。wc给出总数,只是除以 -b和-b将忽略空格和空行,-s表示抑制公共行。下面是一个脚本,它将比较所有.txt文件,并显示重复率超过15%的文件: #!/bin/bash # walk through all files in the current dir (an

使用diff显示两个文件之间的百分比差异的好方法是什么


例如,如果一个文件有100行,而一个副本有15行已更改,则差异百分比将为15%。

可能是这样的吗

两个文件,A1和A2

$sdiff-B-B-sa1a2 | wc
将给出不同的行数。wc给出总数,只是除以


-b和-b将忽略空格和空行,-s表示抑制公共行。

下面是一个脚本,它将比较所有.txt文件,并显示重复率超过15%的文件:

#!/bin/bash

# walk through all files in the current dir (and subdirs)
# and compare them with other files, showing percentage
# of duplication.

# which type files to compare?
# (wouldn't make sense to compare binary formats)
ext="txt"

# support filenames with spaces:
IFS=$(echo -en "\n\b")

working_dir="$PWD"
working_dir_name=$(echo $working_dir | sed 's|.*/||')
all_files="$working_dir/../$working_dir_name-filelist.txt"
remaining_files="$working_dir/../$working_dir_name-remaining.txt"

# get information about files:
find -type f -print0 | xargs -0 stat -c "%s %n" | grep -v "/\." | \
     grep "\.$ext" | sort -nr > $all_files

cp $all_files $remaining_files

while read string; do
    fileA=$(echo $string | sed 's/.[^.]*\./\./')
    tail -n +2 "$remaining_files" > $remaining_files.temp
    mv $remaining_files.temp $remaining_files
    # remove empty lines since they produce false positives
    sed '/^$/d' $fileA > tempA

    echo Comparing $fileA with other files...

    while read string; do
        fileB=$(echo $string | sed 's/.[^.]*\./\./')
        sed '/^$/d' $fileB > tempB
        A_len=$(cat tempA | wc -l)
        B_len=$(cat tempB | wc -l)

        differences=$(sdiff -B -s tempA tempB | wc -l)
        common=$(expr $A_len - $differences)

        percentage=$(echo "100 * $common / $B_len" | bc)
        if [[ $percentage -gt 15 ]]; then
            echo "  $percentage% duplication in" \
                 "$(echo $fileB | sed 's|\./||')"
        fi
    done < "$remaining_files"
    echo " "
done < "$all_files"

rm tempA
rm tempB
rm $all_files
rm $remaining_files
#/bin/bash
#浏览当前目录(和子目录)中的所有文件
#并将其与其他文件进行比较,显示百分比
#重复的数量。
#要比较哪种类型的文件?
#(比较二进制格式没有意义)
ext=“txt”
#支持带空格的文件名:
IFS=$(echo-en“\n\b”)
工作目录=“$PWD”
working_dir_name=$(echo$working_dir|sed's |.*/| |')
all_files=“$working_dir/。/$working_dir_name-filelist.txt”
剩余的\u文件=“$working\u dir/./$working\u dir\u name-remaining.txt”
#获取有关文件的信息:
查找-键入f-print0 | xargs-0 stat-c“%s%n”| grep-v”/\。|\
grep“\.$ext”|排序-nr>$all\u文件
cp$所有\u文件$剩余\u文件
读取字符串时;做
fileA=$(echo$string | sed's/[^.]*\./\./')
尾部-n+2“$剩余文件”>$剩余文件.temp
mv$剩余\u文件。temp$剩余\u文件
#删除空行,因为它们会产生误报
sed'/^$/d'$fileA>tempA
echo正在将$fileA与其他文件进行比较。。。
读取字符串时;做
fileB=$(echo$string | sed's/[^.]*\./\./')
sed'/^$/d'$fileB>tempB
A_len=$(cat tempA | wc-l)
B|len=$(cat tempB | wc-l)
差异=$(sdiff-B-s tempA tempB | wc-l)
通用=$(expr$A_len-$差异)
百分比=$(回音“100*$common/$B|len”| bc)
如果[$percentage-gt 15]];然后
回显“$percentage%重复出现在”\
“$(echo$fileB | sed's | \./| |')”
fi
完成<“$剩余的\u文件”
回声“”
完成<“$all_文件”
坦帕酒店
rm tempB
rm$所有文件
rm$剩余的\u文件
对此有一个简洁的解决方案

wdiff -s file1.txt file2.txt

更多选项请参见。

您可以使用sdiff并计算分隔符,然后除以行数。diff fileA fileB | wc-l除以wc-l fileA//似乎是一种有趣的手动方式。但问题是,如果存在差异,则会得到3行——orig、new和description。所以,您可能会高估wc的man文件中的:换行符、单词和字节。将输出中的第一个数字除以要比较的文件中的行数。wc-l,只提供行数,可以添加到上面的命令。-回应AlligatorJack@cdated:谢谢你的澄清。当然,在你发表评论之前,我没有看到问题/回答。我想这只适用于文本,而不是像媒体一样的视频,我一直在接近重复的视频(由于youtube dl的变化)。