I have read about diff and patch but I can't figure out how to apply what I need.
I guess its pretty simple, so to show my problem take these two files:
a.xml
<resources>
<color name="same_in_b">#AAABBB</color>
<color name="not_in_b">#AAAAAA</color>
<color name="in_b_but_different_val">#AAAAAA</color>
<color name="not_in_b_too">#AAAAAA</color>
</resources>
b.xml
<resources>
<color name="same_in_b">#AAABBB</color>
<color name="in_b_but_different_val">#BBBBBB</color>
<color name="not_in_a">#AAAAAA</color>
</resources>
I want to have an output, which looks like this (order doesn't matter):
<resources>
<color name="same_in_b">#AAABBB</color>
<color name="not_in_b">#AAAAAA</color>
<color name="in_b_but_different_val">#BBBBBB</color>
<color name="not_in_b_too">#AAAAAA</color>
<color name="not_in_a">#AAAAAA</color>
</resources>
The merge should contain all lines along this simple rules:
- any line which is only in one of the files
- if a line has the same name tag but a different value, take the value from the second
I want to apply this task inside a bash script, so it must not nessesarily need to get done with diff and patch, if another programm is a better fit
Best Answer
You don't need
patch
for this; it's for extracting changes and sending them on without the unchanged part of the file.The tool for merging two versions of a file is
merge
, but as@vonbrand
wrote, you need the "base" file from which your two versions diverged. To do a merge without it, usediff
like this:It will enclose each set of changes in C-style
#ifdef
/#ifndef
"preprocessor" commands, like this:If a line or region differs between the two files, you'll get a "conflict", which looks like this:
So save the output in a file, and open it in an editor. Search for any places where
#else
comes up, and resolve them manually. Then save the file and run it throughgrep -v
to get rid of the remaining#if(n)def
and#endif
lines:In the future, save the original version of the file.
merge
can give you much better results with the help of the extra information. (But be careful:merge
edits one of the files in-place, unless you use-p
. Read the manual).