how can I diff two sections of the same file?

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

    0 讨论(0)
  • 2021-02-13 17:11

    Any diff tool that lets you manually adjust the alignment will do the job. Diffuse (http://diffuse.sourceforge.net/) is my favourite and it also lets you manually adjust the alignment.

    0 讨论(0)
  • 2021-02-13 17:12

    Emacs has ediff-regions-wordwise for this -- you can take two buffers (or just one) and select a region in each, which ediff will then show for comparison.

    0 讨论(0)
  • 2021-02-13 17:13

    The linediff plugin for Vim works well for me. Visually select one section of your file and type :Linediff. Visually select the other section and type :Linediff. It will put vim in to vimdiff mode, showing only the two sections you highlighted previously. Type:LinediffReset to exit vimdiff mode.

    More info:

    https://unix.stackexchange.com/a/52759/32477

    https://superuser.com/a/414958/199800

    0 讨论(0)
  • 2021-02-13 17:18

    I use Beyond Compare.
    It allows you to select a line on each side and say 'Align Manually'. This should work just fine for you.

    0 讨论(0)
  • 2021-02-13 17:19

    KDiff3 is open source and available on several platforms including Win32 and Linux.

    It has the "manual alignment" feature discussed by Gishu about Beyond Compare (which by the way I haven't been using personally but is considered a great tool by many people I know).

    manual alignment example See this answer for more examples.

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