Find and replace variable for xml tags

,

Hello All,

I have some tagged variables in xml which i want to modify from source and target using sed or awk.
source:

      <cdb_name oa_var="s_cdb_name">EBS2SIT</cdb_name>
      <cdb_unique_name oa_var="s_cdb_unique_name"/>
      <global_db_name oa_var="s_dbSid">PEBS2SIT</global_db_name>
      <db_name_lower oa_var="s_dbSidLower">pebs2sit</db_name_lower>

Should be like below in target: ( copied the same source file to target and modified)

      <cdb_name oa_var="s_cdb_name">EBSPAT2</cdb_name>
      <global_db_name oa_var="s_dbSid">PEBSPAT</global_db_name>
      <db_name_lower oa_var="s_dbSidLower">pebspat</db_name_lower>

LIKE WISE HAVE MANY WITH IN TAG. I CAPTURED USING AWK the above values in variable. SOURCE:

S_PDBNAME_C= PEBS2SIT,
S_PDBNAME_S= pebs2sit,
S_UNIQ=EBS2SIT_12345,
S_CDBINST= EBS2SIT2,
S_INSTNAME= EBS2SIT

TARGET:

T_PDBNAME_C= PEBSPAT,
T_PDBNAME_S= pebspat,
T_UNIQ= EBSPAT_12345,
T_CDBINST= EBSPAT2,
T_INSTNAME= EBSPAT2

I used below to modify the source and target.

sed -i 's/$S_PDBNAME_S/$T_PDBNAME_S/g' $S_FILE

Challenge is although command is successful but the value in target file is just the same i need to directly modify the tagged variable with out creating temp variable or create temp variable and modify the file with source and target variable.

sed -i 's/$S_PDBNAME_S/$T_PDBNAME_S/g' $S_FILE

outputs

<global_db_name oa_var="s_dbSid">PEBS2SIT</global_db_name>

Please share some solution I’m really exhausted with all combinations avail on google.Thanks in advance. Any input from your end is really valuable for me…

regards
Rahman Syed

Try double quote:

sed -i "s/$S_PDBNAME_S/$T_PDBNAME_S/g" $S_FILE

Hello tomboi,

that that as well but no change.

even tried
sed -i ‘|${var1}|${var2}|g’ $S_FILE didnt work.

please share your views.

regards
Rahman Syed

Are you getting any errors? Post it.

Procedure

Set it as follows:

file="input.txt"

Here is what $file:

cat $file

Your original input file:

     <cdb_name oa_var="s_cdb_name">EBS2SIT</cdb_name>
      <cdb_unique_name oa_var="s_cdb_unique_name"/>
      <global_db_name oa_var="s_dbSid">PEBS2SIT</global_db_name>
      <db_name_lower oa_var="s_dbSidLower">xxx</db_name_lower>

Now set variables (avoid spaces and ‘,’ for variable and double quote them):

S_PDBNAME_S="pebs2sit"
T_PDBNAME_S="pebspat"

Then run (the -i'.bak' creates a backup file):

sed -i'.bak' "s/$S_PDBNAME_S/$T_PDBNAME_S/g" $file

Verify it:

cat $file

See it is now updated:

      <cdb_name oa_var="s_cdb_name">EBS2SIT</cdb_name>
      <cdb_unique_name oa_var="s_cdb_unique_name"/>
      <global_db_name oa_var="s_dbSid">PEBS2SIT</global_db_name>
      <db_name_lower oa_var="s_dbSidLower">pebspat</db_name_lower>

Hello Nixcraft,

we have executed the code you shared but its giving following error.

  • sed -i.bak s///g s.xml
    sed: -e expression #1, char 0: no previous regular expression
  • sed -i.bak s///g s.xml
    sed: -e expression #1, char 0: no previous regular expression
  • sed -i.bak s///g s.xml
    sed: -e expression #1, char 0: no previous regular expression

regards
Rahman Syed

Hello All,

Please help fix the issue.

regards
Rahman Syed

Make sure your variables such as S_PDBNAME_S and others don’t have any special characters like ,.