how can I diff two sections of the same file?

前端 未结 7 1404
心在旅途
心在旅途 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:10

    i chose to rework @ordnungswidrig's answer into a bash function (i was only interested in the differences from a single file, but this could easily be changed to handle two different files...):

    # find differences within a file giving start and end lines for both sections
    function diff_sections {
      local fname=`basename $1`;
      local tempfile=`mktemp -t $fname`;
      head -$3 $1 | tail +$2 > $tempfile && head -$5 $1 | tail +$4 | diff -u $tempfile - ;
      rm $tempfile;
    }
    

    you call the function like so... diff_sections path/to/file 464 483 485 506

提交回复
热议问题