how can I diff two sections of the same file?

前端 未结 7 1406
心在旅途
心在旅途 2021-02-13 16:53

I have a source file with two similar yet subtly different sections. I\'d like to merge the two sections into one subroutine with a parameter that handles the subtle difference

相关标签:
7条回答
  • 2021-02-13 17:23

    If you can describe the start and end of the sections to compore with a regex you can use the following:

    sh -c 't=`mktemp`; cat "$0" | grep -e "$2" -A10000 | grep -e "$3" -B 10000 > $t; cat "$1" | grep -e "$2" -A10000 | grep -e "$3" -B 10000 | diff -u $t - ; rm $t' firstfile secondfile "section start" "section end"
    

    As an alternative you if you want to describe the section by line number you can do:

    sh -c 't=`mktemp`; cat "$0" | head -$3 |tail +$2 > $t; cat "$1" | head -$5 | tail +$4 | diff -u $t - ; rm $t' first second 4 10 2 8
    

    4 10 2 8 is the start and end line number for the section to consider from the first file and the second file.

    You can either save the snippets a shell scripts or as aliases.

    0 讨论(0)
提交回复
热议问题