# Bash - Comparing 2 xml strings masking certain fields

**URL:** <https://community.unix.com/t/bash-comparing-2-xml-strings-masking-certain-fields/361380>\
**Category:** Shell Programming and Scripting\
**Created:** [July 12, 2016, 7:45pm UTC](https://community.unix.com/t/bash-comparing-2-xml-strings-masking-certain-fields/361380 "2016-07-12T19:45:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sajjadmehdi](https://community.unix.com/letter_avatar/sajjadmehdi/32/6_449c533f586939b6e5f246632a46421f.png) [@Sajjadmehdi](https://community.unix.com/u/Sajjadmehdi)\
**Post date:** [July 12, 2016, 7:45pm UTC](https://community.unix.com/t/bash-comparing-2-xml-strings-masking-certain-fields/361380/1 "2016-07-12T19:45:41Z")

</div>

Would like to compare 2 XML Strings which has certain known fields changed. For example, Date field will always have differences. When comparing both strings, skip/mask all the occurring Date Field's `DtField1` and `DtField2`

Note: these are not formatted xml format.

File1:

```nohighlight
  <Field1><Field2><Field5></Field5><DtField1>Jan 01 2017,00:00:01</DtField1><Code>052</Code><Desc>This is a test</Desc><DtField2>Jan 01 2001,00:00:26</DtField2><Field4>Company</Field4><Field6></Field6></Field3></Field2></Field1>

```

File2:

```nohighlight
<Field1><Field2><Field5></Field5><DtField1>Jan 01 2017,00:00:01</DtField1><Code>052</Code><Desc>This is a test</Desc><DtField2>Jan 01 2001,00:00:39</DtField2><Field4>Company</Field4><Field6></Field6></Field3></Field2></Field1>

```

I have tried to sed the string into blocks using the code

```nohighlight
    Block1=`sed -n 's:.*<Field1>\(.*\)</Field5>.*:\1:p' File`
    Block2=`sed -n 's:.*<Code>\(.*\)</Desc>.*:\1:p' File`
    Block3=`sed -n 's:.*<Field4>\(.*\)</Field1>.*:\1:p' File`

```

and compare the Blocks of File1 with File2 but there is a possibility that Fields may not be in order every time. However, fields will be in same position in String1 and String2.

So, it is easy to somehow mask/remove the occurring DtField's and compare 2 strings.

Expected Output:

```nohighlight
 <Field1><Field2><Field5></Field5><Code>052</Code><Desc>This is a test</Desc><Field4>Company</Field4><Field6></Field6></Field3></Field2></Field1>

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [July 13, 2016, 2:36pm UTC](https://community.unix.com/t/bash-comparing-2-xml-strings-masking-certain-fields/361380/2 "2016-07-13T14:36:19Z")

</div>

How about (untested!)

```nohighlight
sed 's/<DtField1>.*<\/DtField1>//;s/<DtField2>.*<\/DtField2>//;' file1

```

and then compare the remainders of the two files...?
