views:

208

answers:

1

Hi,

Im have a xml config file called solrconfig.xml, it has this section in the middle of it:

<!--############ BEGIN replication settings DO NOT EDIT ################################-->
<requestHandler name="/replication" class="solr.ReplicationHandler" >
<lst name="master">
  <str name="replicateAfter">commit</str>
  <str name="replicateAfter">startup</str>
  <str name="confFiles">schema.xml,stopwords.txt</str>
</lst>
</requestHandler>
<!--############ END replication settings DO NOT EDIT ################################-->

I have a shell script that I want to use to replace this section in the case I am configuring the server as a slave. I have it working, except it puts the new section at the end of the file instead of the same place as the old one, can you help me tweak this to replace it in the same spot.

if [ -n "$1" ] && [ $1 == "slave" ] 
then
    rm solrconfig2.xml
    echo "setting up slave"
    cat solrconfig.xml | awk '
/^<!--############ BEGIN replication/ { skip = 1 }
/^<!--############ END replication/   { skip = 0; next; }
{ if (skip == 0) print $0; }
END {
   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&lt;/str&gt;"
   print "<str name=\"pollInterval\">00:00:60</str>"
   print "</lst>"
   print "</requestHandler>"
   print "<!--############ END replication settings DO NOT EDIT ################################-->"
}
' > solrconfig2.xml
fi
+1  A: 

In your block for the beginning of the region ({ skip = 1}), add your print statements there. The logic is:

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

The solution looks something like this:

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&lt;/str&gt;"
   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

However, a better solution might be to use a better scripting language with XML support (for example, Python, Ruby or Tcl) and take advantage of the ability to manipulate the DOM.

Bryan Oakley
thanks, and agree!
Joelio