1

Hollo, i have researched a lot but never find a solution that fits what i need.

I have to replace the original xml line:

<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>

with a modificated xml line:

<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T %{i,Proxy-Remote-User}" prefix="access_${jboss.server.name}" suffix=".log"/>

I tried making 2 variables. here is my script:

#!/bin/bash
MOD=`more /aplicaciones/users/t682213/alvaro/mod`
XML=`more /aplicaciones/users/t682213/alvaro/xml`
sed "s#$XML#$MOD#g" test2

But i get something duplicated like this:

/aplicaciones/users/t682213/alvaro$ sed "s#$XML#$MOD#g" test2

<access-log pattern="%h %l %u %t <access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>quot;%r<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>quot; %s %b <access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>quot;%{i,Referer}<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>quot; <access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>quot;%{i,User-Agent}<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>quot; %T %{i,Proxy-Remote-User}" prefix="access_${jboss.server.name}" suffix=".log"/>

Please i need help, i want to replace a very complex string with another very complex string.

facyssys
  • 11
  • 2
  • No. Don't use Regexes on XML. Use a XML parser then replace the node attribute as needed. Or you could use XSLT which let's one write tree transducers. – Dan D. Feb 02 '19 at 19:16

2 Answers2

1

Dan D. is right. Manipulating XML using regular expressions is an evil hack that works in some cases, but it can quickly become a maintance nightmare. Nevertheless, if you are really confident that for some reasons (like having no XML parser availabel) you just have to use sed for that task, I provide an answer to your question (referencing another SO question).

You need to escape the strings from the file. In this case, the trouble arises from the character & in the replacement string which means put the whole match in here. See Escape a string for a sed replace pattern for suggestions on how to escape the search and the replace pattern.

Michael Karcher
  • 3,803
  • 1
  • 14
  • 25
0

I GOT THE SOLUTION

As Michael mention, what was the problem was the &, so i escaped the & in the two text variables, (the MOD and the XML)

here is my result, (if you find problems with regex in sed, like /, "", &, try this)

first of all i create two texts one for the xml string that i want to replace (xml), and the other text with the xml string that i want to replace with (mod)

here are the two texts:

xml (with & escape: \&)

<access-log pattern="%h %l %u %t \&quot;%r\&quot; %s %b \&quot;%{i,Referer}\&quot; \&quot;%{i,User-Agent}\&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>

mod (with & escape: \&)

<access-log pattern="%h %l %u %t \&quot;%r\&quot; %s %b \&quot;%{i,Referer}\&quot; \&quot;%{i,User-Agent}\&quot; %T %{i,Proxy-Remote-User}" prefix="access_${jboss.server.name}" suffix=".log"/>

i created a test txt to simulate de complete xml where i want to work after, called test2

<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T " prefix="access_${jboss.server.name}" suffix=".log"/>
HOLA
ADIOS

And finally create a shell script that makes the change with sed: (its very importand to use # instead / and " " instead ' ' )

#!/bin/bash
MOD=`more /aplicaciones/users/t682213/alvaro/mod`
XML=`more /aplicaciones/users/t682213/alvaro/xml`
sed "s#$XML#$MOD#" test2 >> test3 

finally the script generate a test3 text that is modificated correctly:

<access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; %T %{i,Proxy-Remote-User}" prefix="access_${jboss.server.name}" suffix=".log"/>
HOLA
ADIOS 
facyssys
  • 11
  • 2