Mutli line pattern search & replace in a xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mutli line pattern search & replace in a xml file
# 1  
Old 01-29-2012
Mutli line pattern search & replace in a xml file

Hello guys,

I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern.

For instance, here is the input:
Code:
<RefNickName>abcd</RefNickName>
      <NickName>efgh</NickName>
      <Customize>
        <DefaultBinary>flex/MX.sh</DefaultBinary>
        <DefaultCommands>
        <DefaultCommand>/USER:Paul</DefaultCommand>
<DefaultCommand>/PASSWORD:lalalala007n48</DefaultCommand>
        <DefaultCommand>/GROUP:ALLRIGHT</DefaultCommand>

The output would be:
Code:
<RefNickName>abcd</RefNickName>
      <NickName>efgh</NickName>
      <Customize>
        <DefaultBinary>#DEFAULT_NAME#</DefaultBinary>
        <DefaultCommands>
        <DefaultCommand>/USER:Paul</DefaultCommand>
<DefaultCommand>/PASSWORD:#PAUL_PASSWD#</DefaultCommand>
        <DefaultCommand>/GROUP:ALLRIGHT</DefaultCommand>


I tried the following sed command:

Code:
sed -n '1h;1!H;${;g;s|<DefaultBinary>flex.*n48</DefaultCommand>|<DefaultBinary>#DEFAULT_NAME#</DefaultBinary>\
<DefaultCommands><DefaultCommand>/USER:Paul</DefaultCommand><DefaultCommand>/PASSWORD:#PAUL_PASSWD#</DefaultCommand>|g;p;}'

It's not exactly what I want to do since I am replacing the matching pattern with only one line and not a block line. Besides, that command doesnt even work (I obtain the command garbled error message).

I can't simply search and replace on one hand the DefaultBinary field and the password on the other hand because I have to edit the DefaultBinary and password fields only when they are related to the user Paul. That why I need to use the multi line pattern matching.

Could someone give me some help ?

Thanks a lot and have a good day!

---------- Post updated at 05:03 AM ---------- Previous update was at 04:44 AM ----------

Update Smilie

I managed to make it work, Iwas executing the shell command with "launcher.mxres >>launcher.mres" at the end. There was missing a space.

Hence the following command works
Code:
sed -n '1h;1!H;${;g;s|<DefaultBinary>flex.*n48</DefaultCommand>|<DefaultBinary>#DEFAULT_NAME#</DefaultBinary>\
<DefaultCommands><DefaultCommand>/USER:Paul</DefaultCommand><DefaultCommand>/PASSWORD:#PAUL_PASSWD#</DefaultCommand>|g;p;}'

But still, the command adds the entire line, how can I add a jump line after each xml tag? \n doesnt seem to work

---------- Post updated at 08:44 PM ---------- Previous update was at 05:03 AM ----------

Update!
With this command I managed to get it done:

Code:
sed -n '1h;1!H;${;g;s|<DefaultBinary>flex.*n48</DefaultCommand>|<DefaultBinary>#DEFAULT_NAME#</DefaultBinary>\
<DefaultCommands>\
<DefaultCommand>/USER:Paul</DefaultCommand>\
<DefaultCommand>/PASSWORD:#PAUL_PASSWD#</DefaultCommand>|g;p;}' launcher.mxres >> launcher-edited.mxres

But I have got another problem. I have got another file on which I have to make the same amendment. The problem on this file is that the block that i want to amend occurs several time. I would liek to make the amendment on every block in the file:

Code:
<RefNickName>abcd</RefNickName>
      <NickName>efgh</NickName>
      <Customize>
        <DefaultBinary>flex/MX.sh</DefaultBinary>
        <DefaultCommands>
        <DefaultCommand>/USER:Paul</DefaultCommand>
<DefaultCommand>/PASSWORD:lalalala007n48</DefaultCommand>
        <DefaultCommand>/GROUP:ALLRIGHT</DefaultCommand>
...
 
<RefNickName>abcd</RefNickName>
      <NickName>efgh</NickName>
      <Customize>
        <DefaultBinary>flex/MX.sh</DefaultBinary>
        <DefaultCommands>
        <DefaultCommand>/USER:Paul</DefaultCommand>
<DefaultCommand>/PASSWORD:lalalala007n48</DefaultCommand>
        <DefaultCommand>/GROUP:ALLRIGHT</DefaultCommand>

When I execute my command, there is a problem in the research pattern o this line "flex.*n48". The command takes basically everything after the first "flex" occurrence to the last "n48" occurrence.

It's the last step to get my work done, does someone have an idea please ?

Last edited by vbe; 01-29-2012 at 04:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and Replace a text if the line contains a pattern

My text file looks like below . . . abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt $Bad_abcd_REJ=rcrd_op.txt ghijklm $Bad_abcd_TYHS=rcrd_op.txt abcgd abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt (2 Replies)
Discussion started by: machomaddy
2 Replies

2. Shell Programming and Scripting

search & replace pattern

Hi, My problem is that I have to search a changing pattern and replace it with the wild card char "*" i/p: 99_*_YYYYMMDD_SRC.txt.tar.gz o/p: 99_*_*_SRC.txt.tar.gz The problem is that YYYYMMDD pattern is not static. It could be YYYYMMDDHHMI or could be YYYYMMDDHHMISS. Can... (10 Replies)
Discussion started by: dips_ag
10 Replies

3. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

4. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

5. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

6. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

7. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

8. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

9. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question