Using the Merge Command to Update Duplicate Files

I recently came across a duplicate set of files which contained similar information. In order to reduce clutter and clarify the information in one of the files, I used a the diff and merge command to compare and mesh to two file together so none of the data was lost. 

So, if I have file 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>

and file 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 wanted to have an output similar to the following:

<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 following command allowed for the input from both original files and where differences occurred, it denoted it with the following characteristics:

  • #ifndef VERSION1
  • #else /* VERSION1 */

The command I used was as follows:

diff -DVERSION1 file1.xml file2.xml > merged.xml

This provided the addition and notation of the differences in both files so it makes comparison easier. 

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.