Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually merge two files using diff

Tags:

linux

diff

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?

like image 493
alexgolec Avatar asked Jun 03 '13 17:06

alexgolec


People also ask

How to merge a diff?

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. I usually then just open the merged file in an editor and resolve the merge conflicts by hand.

Which command is used to merge two files?

Using joinThe join command allows you to merge the content of multiple files based on a common field.

How do I patch a diff file?

The "diff" tool calculates the differences between two text files. That difference is called a patch. You can apply a patch to another file using the "patch" tool. diff and patch are intended to be used on text files.

How do I combine the contents of two files in Linux?

To merge lines of files, we use the paste command in the Linux system. The paste command is used to combine files horizontally by outputting lines consisting of the sequentially corresponding lines from each FILE, separated by TABs to the standard output.


1 Answers

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.

like image 192
dougBTV Avatar answered Sep 28 '22 11:09

dougBTV