如何区分同一文件的两个部分?

时间:2022-11-01 11:37:19

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 differences, but I need to be sure I'm aware of them all so I don't miss any.
What I usually do in such cases is copy each of the sections to a separate file and then use tkdiff or vimdiff to highlight the differences. Is there any way to skip the intermediate files and just diff two parts of the same file?

我有一个源文件,有两个相似但略有不同的部分。我想将这两个部分合并为一个子程序,其中一个参数可以处理微妙的差异,但我需要确定我已经完全了解它们,所以我不会错过任何一个。在这种情况下我通常做的是将每个部分复制到一个单独的文件,然后使用tkdiff或vimdiff突出显示差异。有没有办法跳过中间文件,只是区分同一个文件的两个部分?

6 个解决方案

#1


4  

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.

Vim的有线插件适用于我。以可视方式选择文件的一个部分,然后键入:Linediff。目视选择其他部分并输入:Linediff。它会将vim置于vimdiff模式,仅显示您之前突出显示的两个部分。键入:LinediffReset退出vimdiff模式。

More info:

更多信息:

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

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

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

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

#2


5  

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

KDiff3是开源的,可在多个平台上使用,包括Win32和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).

它具有Gishu讨论的关于Beyond Compare的“手动对齐”功能(顺便说一下,我没有亲自使用它,但被我认识的许多人认为是一个很好的工具)。

如何区分同一文件的两个部分? See this answer for more examples.

有关更多示例,请参阅此答案。

#3


4  

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.

我使用Beyond Compare。它允许您在每一侧选择一条线并说“手动对齐”。这应该对你有用。

#4


3  

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...):

我选择将@ ordnungswidrig的答案重写为bash函数(我只对单个文件的差异感兴趣,但这可以轻松更改为处理两个不同的文件......):

# 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

你可以像这样调用函数... diff_sections path / to / file 464 483 485 506

#5


1  

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.

任何可以手动调整对齐的diff工具都可以完成这项工作。 Diffuse(http://diffuse.sourceforge.net/)是我最喜欢的,它也可以让你手动调整对齐。

#6


0  

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.

4 10 2 8是从第一个文件和第二个文件中考虑的部分的起始和结束行号。

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

您可以将snippets保存为shell脚本或别名。

#1


4  

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.

Vim的有线插件适用于我。以可视方式选择文件的一个部分,然后键入:Linediff。目视选择其他部分并输入:Linediff。它会将vim置于vimdiff模式,仅显示您之前突出显示的两个部分。键入:LinediffReset退出vimdiff模式。

More info:

更多信息:

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

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

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

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

#2


5  

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

KDiff3是开源的,可在多个平台上使用,包括Win32和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).

它具有Gishu讨论的关于Beyond Compare的“手动对齐”功能(顺便说一下,我没有亲自使用它,但被我认识的许多人认为是一个很好的工具)。

如何区分同一文件的两个部分? See this answer for more examples.

有关更多示例,请参阅此答案。

#3


4  

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.

我使用Beyond Compare。它允许您在每一侧选择一条线并说“手动对齐”。这应该对你有用。

#4


3  

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...):

我选择将@ ordnungswidrig的答案重写为bash函数(我只对单个文件的差异感兴趣,但这可以轻松更改为处理两个不同的文件......):

# 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

你可以像这样调用函数... diff_sections path / to / file 464 483 485 506

#5


1  

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.

任何可以手动调整对齐的diff工具都可以完成这项工作。 Diffuse(http://diffuse.sourceforge.net/)是我最喜欢的,它也可以让你手动调整对齐。

#6


0  

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.

4 10 2 8是从第一个文件和第二个文件中考虑的部分的起始和结束行号。

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

您可以将snippets保存为shell脚本或别名。