Manually merge two files using diff

徘徊边缘 提交于 2019-12-03 06:12:05

"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.

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.

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

diff --line-format %L file1 file2

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.

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.

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