Manually merge two files using diff

I would like to merge the two files by doing the following:

  • Output the difference between two files to temp file and
  • Manually select the rows that I want to copy / save.

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

Is there a diff way to do this?

+8
linux diff
source share
4 answers

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

Yes.

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

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

+6
source share

One option that can set you up,

sdiff : side by side diff files.

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

After that, it will tell you which lines you want to save from which file. Click ? and then enter for a little help. Also man sdiff with detailed items.

(In my distribution, they come in the diffutils package [fedora, centos])

If you need to automate the process, you can try the merge utility, which will flag conflicts in files. However, this may return you to the square.

+10
source share

The simple answer is to use the -D flag to combine files and surrounding differences using instructions like Cif #ifdef.

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 

Usually I just open the merged file in the editor and resolve the merge conflicts manually.

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

0
source share

You can merge / merge two files with diff -

 diff --line-format %L file1 file2 
0
source share

All Articles