我有一个名为alphabet.xml的xml文件,如下所示: -
<?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>
现在我想用<!DOCTYPE models SYSTEM "mp.dtd">
<!DOCTYPE models SYSTEM "/opt/nms_cif_mp/dat/mp.dtd">
字符串
我正在使用的代码在下面。但是使用此代码不会发生上述特定字符串的替换。
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
注意:如果我将<!DOCTYPE models SYSTEM "mp.dtd">
替换为任何单词(尝试使用hello word),而不是<!DOCTYPE models SYSTEM "/opt/nms_cif_mp/dat/mp.dtd">
这里是否需要特殊处理?
答案 0 :(得分:1)
问题在于Awk命令中的引用。
如果您的Awk支持$to_dtd
选项,则可以通过将-v
的值传递给Awk变量来简化此操作:
awk -vdtd="$to_dtd" 'NR == 2 { print dtd } NR != 2 { print }' /home/abinash/Desktop/MyFolder /$file_name > /home/abinash/Desktop/MyFolder/tmp.xml