sed xml file script help

I have about 1200 xml files I need to deploy from a 6.4 XML version to 9.0 version of a DTD. So I need a sed command to do lots of of replacements of random variables.

The keyword line is <AUTOEDIT EXP then is will have a variable followed by an equals sign. From that point on I need to break the line again.

Example

<AUTOEDIT EXP="%%S_APP_HOME=APP_HOME=%%G_APP_HOME_PREFIX.pmt/apb"/>  

SED to

<VARIABLE NAME="%%S_APP_HOME" VALUE="APP_HOME=%%G_APP_HOME_PREFIX.pmt/apb" />  

I know the last part but I'm stuck on how to handle the unknown variable name part. So its carried over on the replace.

#!/bin/ksh
inXML=$1
outXML=${inXML:?}.v9.xml

sed -e \
"s/AUTOEDIT EXP=\"[a-z]*[0-9]*=/AUTOEDIT EXP=\"[a-z]*[0-9]*\" VALUE=\"/g" -e \
"s/AUTOEDIT EXP=/VARIABLE NAME=/g" < ${inXML} > ${outXML}

\1 holds the value of the first capture group on the lhs of a substitution

google sed capture groups for many detailed discussions

1 Like

Thanks, that helped a lot with that issue.

I also messed up the first grab though

AUTOEDIT EXP=\"[a-z]*[0-9]*=

Is not finding

AUTOEDIT EXP="%%S_APP_HOME=

For the random variable names what is the correct syntax?

Try

sed '/AUTOEDIT EXP/{s//VARIABLE NAME/; s/=/" VALUE="/2}' file4
<VARIABLE NAME="%%S_APP_HOME" VALUE="APP_HOME=%%G_APP_HOME_PREFIX.pmt/apb"/>  

[a-z][0-9] won't match "%", "_", nor any upper case char.

1 Like

I'm running on AIX, but get an error message "cannot be parsed"

AIX an05074 1 6 00F61FD34C00
$ sed '/AUTOEDIT EXP/{s//VARIABLE NAME/; s/=/" VALUE="/2}' test.xml
sed: 0602-404 Function /AUTOEDIT EXP/{s//VARIABLE NAME/; s/=/" VALUE="/2} cannot be parsed.

---------- Post updated at 05:43 PM ---------- Previous update was at 05:28 PM ----------

Thank you both, got it working.

#!/bin/ksh
inXML=$1
outXML=${inXML:?}.v9.xml

sed -e \
"s/AUTOEDIT EXP/VARIABLE NAME/g" -e \
"/VARIABLE NAME/s/=/\" VALUE=\"/2" < ${inXML} > ${outXML}

Please be aware that this will substitute EVERY occurrence of VARIABLE NAME in your file regardless of AUTOEDIT found in same line or not.