Manually merge two files using diff

久未见 提交于 2019-12-03 17:26:30

问题


I'd like to merge two files by doing the following:

  1. Output the diff of the two files into a temp file and
  2. Manually select the lines I want to copy/save.

The problem here is that diff -u only gives me a file lines of context, while I want to output the entire file in a unified format.

Is there any way diff can do this?


回答1:


"I want to output the entire file in a unified format. Is there any way diff can do this?"

Yes.

diff -U 9999999 file1.txt file2.txt > diff.txt

This should work, provided your files are less than 10 million lines long.




回答2:


One option that might fit the bill for you,

sdiff : side-by-side diff of files.

sdiff -o merged.file left.file right.file

Once there, it will prompt you with what lines you want to keep from which file. Hit ? and then enter for a little help. Also man sdiff with the detailed goods.

(In my distro, these come packaged in the "diffutils" package [fedora,centos])

If you need to automate the process, you might want to try the util merge, which will mark conflicts in the files. However, that might put you back at square one.




回答3:


You can merge/combine the two files with diff using --

diff --line-format %L file1 file2



回答4:


The easy answer is to use the -D flag to merge the files and surround the differences with C style #ifdef statements.

From the documentation:

-D NAME  --ifdef=NAME
          Output merged file to show `#ifdef NAME' diffs.

You can use it as follows:

$ diff -D NEWSTUFF file1 file2 > merged_file

I usually then just open the merged file in an editor and resolve the merge conflicts by hand.

You also can use options to output an ed script, etc.




回答5:


If you are an emacs user, you can do this directly in emacs using the "emerge" tool:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Emerge.html

Issuing M-x emerge-files will open an interactive prompt with a view of files A, B, and the merged file to allow choosing text that differs between files A & B, inserting part of A into B, and more.



来源:https://stackoverflow.com/questions/16902001/manually-merge-two-files-using-diff

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!