I have a xml file with name alphabet.xml like below : –
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE models SYSTEM "mp.dtd">
<models>
<class name="ABC">
<attribute name="a"/>
<attribute name="b"/>
<attribute name="c"/>
</class>
<class name="DEF">
<attribute name="d"/>
<attribute name="e"/>
<attribute name="f"/>
</class>
<class name="GHI">
<attribute name="g"/>
<attribute name="h"/>
<attribute name="i"/>
</class>
<class name="JKL">
<attribute name="j"/>
<attribute name="k"/>
<attribute name="l"/>
</class>
</models>
Now I want to replace <!DOCTYPE models SYSTEM "mp.dtd"> String of alphabet.xml with <!DOCTYPE models SYSTEM "/opt/nms_cif_mp/dat/mp.dtd">
code I am using is in below . But with this code replacement of the above specific string is not happening .
cd /home/abinash/Desktop/MyFolder/
file_name="alphabet.xml"
from_dtd='<!DOCTYPE models SYSTEM "mp.dtd">'
to_dtd='<!DOCTYPE models SYSTEM "/opt/nms_cif_mp/dat/mp.dtd">'
#Basically using awk I am trying to replace the string and then write it into tmp.xml .
#After that I am copying the content of tmp.xml into alphabet.xml .
awk '{ if (NR == 2) print "<!DOCTYPE models SYSTEM "/opt/ericsson /nms_cif_mp/dat/mp.dtd""; else print $0}' /home/abinash/Desktop/MyFolder /$file_name > /home/abinash/Desktop/MyFolder/tmp.xml
cp /home/abinash/Desktop/MyFolder/tmp.xml /home/abinash/Desktop/MyFolder /$file_name
NOTE : The above code logic works if I will replace <!DOCTYPE models SYSTEM "mp.dtd"> with any word(tried with hello word) , but not with <!DOCTYPE models SYSTEM "/opt/nms_cif_mp/dat/mp.dtd">
Is there any special handling required here ?
Advertisement
Answer
The problem is with the quoting inside the Awk command.
You can simplify this a lot by passing the value of $to_dtd to an Awk variable, if the -v option is supported with your Awk:
awk -vdtd="$to_dtd" 'NR == 2 { print dtd } NR != 2 { print }' /home/abinash/Desktop/MyFolder /$file_name > /home/abinash/Desktop/MyFolder/tmp.xml