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