差异哪里线大多是相同的,但乱序?


23

我想比较两组mod_rewrite规则。这组线大约有90%相同,但是顺序如此不同,以至于diff基本上表示它们完全不同。

如何查看两个文件中的哪几行真正不同,而不管它们的行号是多少?


3
将它们都通过sort拳头。
肖恩·高夫

@Shawn是否可以执行此操作而无需创建(然后删除)两个一次性文件?
user394 2011年

Answers:


36

sort可以用来使文件具有相同的顺序,因此diff可以比较它们并找出差异。如果您有进程替换,则可以使用它,并避免创建新的排序文件。

diff <(sort file1) <(sort file2)

8

为此编写了一个脚本,该脚本可以使行顺序保持完整。这是重要行的带注释的版本:

# 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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.