title: Replacing a *specific* term in an XML file
body: <p>I want to replace the text <code>PATHTOEXPORT</code> with the contents of a variable passed on the command-line that would have a folder path in it (for example, <code>/Dev_Content/AIX/Apache</code>)</p>
  
  <p>I came across this <a href="http://www.dotkam.com/2007/04/04/sed-to-parse-and-modify-xml-element-nodes/" rel="nofollow">article</a> that discusses how to use <code>sed</code> to replace a value in an XML file with another.</p>
  
  <p>However, not having used <code>sed</code> before, I'm unsure how to read the directions.</p>
  
  <p>The script is as follows:</p>
  
  <pre><code># Check that exactly 3 values were passed in
  if [ $# -ne 3 ]; then
  echo 1&gt;&amp;2 “This script replaces xml element’s value with the one provided as a command parameter \n\n\tUsage: $0 &lt;xml filename&gt; &lt;element name&gt; &lt;new value&gt;”
  exit 127
  fi
  
  echo "DEBUG: Starting... [Ok]\n"
  echo "DEBUG: searching $1 for tagname &lt;$2&gt; and replacing its value with '$3'"
  
  # Creating a temporary file for sed to write the changes to
  temp_file="repl.temp"
  
  # Elegance is the key -&gt; adding an empty last line for Mr. “sed” to pick up
  echo ” ” &gt;&gt; $1
  
  # Extracting the value from the &lt;$2&gt; element
  el_value=`grep “&lt;$2&gt;.*&lt;.$2&gt;” $1 | sed -e “s/^.*&lt;$2/&lt;$2/” | cut -f2 -d”&gt;”| cut -f1 -d”&lt;”`
  
  echo "DEBUG: Found the current value for the element &lt;$2&gt; - '$el_value'"
  
  # Replacing elemen’s value with $3
  sed -e “s/&lt;$2&gt;$el_value&lt;\/$2&gt;/&lt;$2&gt;$3&lt;\/$2&gt;/g” $1 &gt; $temp_file
  
  # Writing our changes back to the original file ($1)
  chmod 666 $1
  mv $temp_file $1
  </code></pre>
  
  <p>Is there a better way to do what I need to do? Can I do it <em>in situ</em> rather than using an intermediate file?</p>