Bash - Comparing 2 xml strings masking certain fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - Comparing 2 xml strings masking certain fields
# 1  
Old 07-12-2016
Bash - Comparing 2 xml strings masking certain fields

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:

Code:
  <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:

Code:
<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

Code:
    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:

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


Moderator's Comments:
Mod Comment Thanks for trying to use tags as required by forum rules, but icode tags are for short sequences within text. For code, data, and output use code tags!

Last edited by RudiC; 07-13-2016 at 03:31 PM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 07-13-2016
How about (untested!)
Code:
sed 's/<DtField1>.*<\/DtField1>//;s/<DtField2>.*<\/DtField2>//;' file1

and then compare the remainders of the two files...?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

2. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

3. Shell Programming and Scripting

Masking Password from within a Bash Shell Script

Is there a way to mask the password inside of a script to minimize the impact of a comprimised server? So ssh -o "PasswordAuthentication no" -o "HostbasedAuthentication yes" -l testuser 192.168.3.1 "mysqldump --opt --all-databases -u root -pPassword| gzip" > $backup_dir/mysqldump.gz a... (2 Replies)
Discussion started by: metallica1973
2 Replies

4. Shell Programming and Scripting

help with comparing fields

I have a file having records like below Field1;Filed2;Filed3 < '393200103052';'H3G';'20081204' < '393200103059';'TIM';'20110111' < '393200103061';'TIM';'20060206' < '393200103064';'OPI';'20110623' > '393200103052';'HKG';'20081204' > '393200103056';'TIM';'20110111' >... (5 Replies)
Discussion started by: shruthi123
5 Replies

5. Shell Programming and Scripting

comparing two strings

hi All i am facing prob in comparing two strings that have two word. below is the code snippet. checkValidates="file validates" file3_name="file" if then echo "file" $file3_name "is validated successfully" fi when i run this i get the error as -bash: [: too many arguments ... (1 Reply)
Discussion started by: infyanurag
1 Replies

6. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies

7. Shell Programming and Scripting

comparing 2 strings

hi i have 2 strings. i want to compare the strings. please help (2 Replies)
Discussion started by: satish@123
2 Replies

8. Shell Programming and Scripting

Comparing Two Strings

Hi All, While I am trying to run below code I Am getting the exception like ./abs.sh: line 102: syntax error near unexpected token `then' ./abs.sh: line 102: ` then' The Code Snippet is: if then cat $file1 | sed -e... (8 Replies)
Discussion started by: Anji
8 Replies

9. UNIX for Advanced & Expert Users

Comparing strings

I have two strings a=Mar22 b=may21 how can I compare them Is this fine if then; . ... else .... fi or if then (2 Replies)
Discussion started by: yakyaj
2 Replies

10. Shell Programming and Scripting

comparing two strings

Hi How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help if then echo success fi I am not getting the desired output if I do this. plz help (24 Replies)
Discussion started by: ragha81
24 Replies
Login or Register to Ask a Question