diff 其中行大部分相同但顺序混乱?

diff 其中行大部分相同但顺序混乱?

我想区分两组 mod_rewrite 规则。这组行大约有 90% 相同,但顺序如此不同,以至于 diff 基本上表明它们完全不同。

如何查看两个文件之间哪些行真正不同,无论它们的行号如何?

答案1

sort可用于将文件按相同顺序排列,以便diff比较它们并识别差异。如果您有进程替换,则可以使用它并避免创建新的排序文件。

diff <(sort file1) <(sort file2)

答案2

我制造了一个脚本为此,保持线路顺序完整。以下是重要行的带注释版本:

# Strip all context lines
diff_lines="$(grep '^[><+-] ' | sed 's/^+/>/;s/^-/</')" || exit 0

# For each line, count the number of lines with the same content in the
# "left" and "right" diffs. If the numbers are not the same, then the line
# was either not moved or it's not obvious where it was moved, so the line
# is printed.
while IFS= read -r line
do
    contents="${line:2}"
    count_removes="$(grep -cFxe "< $contents" <<< "$diff_lines" || true)"
    count_adds="$(grep -cFxe "> $contents" <<< "$diff_lines" || true)"
    if [[ "$count_removes" -eq "$count_adds" ]]
    then
        # Line has been moved; skip it.
        continue
    fi
    
    echo "$line"
done <<< "$diff_lines"

if [ "${line+defined}" = defined ]
then
    printf "$line"
fi

答案3

我的开源 Linux 工具“dif”比较文件,同时忽略各种差异。

它有很多选项用于排序、忽略时间戳、空格或注释、执行搜索/替换、忽略与正则表达式匹配的行等。

预处理输入文件后,它会对这些中间文件运行 Linux 工具 meld、gvimdiff、tkdiff、diff 或 kompare。

不需要安装,只需从以下位置下载并运行“dif”可执行文件即可https://github.com/koknat/dif

对于您的用例,请尝试“排序”选项:

dif file1 file2 -sort

相关内容