在区域开头的块中(skip=1),在那里添加打印语句。逻辑是:
if this is the beginning of the special block:
set a flag
print my replacement
if this is the end of the special block:
unset a flag
else if the flag is not set:
print the current line
解决方案如下:
if [ -n "$1" ] && [ $1 == "slave" ]
then
rm solrconfig2.xml
echo "setting up slave"
cat solrconfig.xml | awk '
/^<!--############ BEGIN replication/ {
skip = 1
print "<!--############ BEGIN replication settings DO NOT EDIT ################################-->"
print "<requestHandler name=\"/replication\" class=\"solr.ReplicationHandler\" >"
print "<lst name=\"slave\">"
print "<str name=\"masterUrl\">http://solr-master:8983/solr/replication</str>"
print "<str name=\"pollInterval\">00:00:60</str>"
print "</lst>"
print "</requestHandler>"
print "<!--############ END replication settings DO NOT EDIT ################################-->"
}
/^<!--############ END replication/ { skip = 0; next; }
{ if (skip == 0) print $0; }
' > solrconfig2.xml
fi
然而,更好的解决方案可能是使用更好的脚本语言和XML支持(例如,python、ruby或tcl),并利用操纵DOM的能力。