using Sed to search and replace text in XML file
The issue is that your search pattern contains /
which you are using as the replacement delimiter, you need to use another character for that or escape the /
:
sed -i 's#<!--UpdateAccountGUIDs>UpdateAndExit</UpdateAccountGUIDs-->#<UpdateAccountGUIDs>UpdateAndExit</UpdateAccountGUIDs>#' File.XML
or
sed -i 's/<!--UpdateAccountGUIDs>UpdateAndExit<\/UpdateAccountGUIDs-->/<UpdateAccountGUIDs>UpdateAndExit<\/UpdateAccountGUIDs>/' File.XML
Note that you should never use regular expressions to parse [X]HTML.
Finally, as a general rule, when working with regular expressions, less is more. You should try to specify the simplest possible exclusive pattern rather than repeat all text. This not only makes your code much easier to read, it also avoids problems like the one you were facing. For example:
sed -i -r 's/<!--(UpdateAccountGUIDs.+?)-->/<\1>/' File.XML
Here, the -r
enables extended regular expression syntax so we can use ()
to capture a group (without needing to escape the parentheses) and then refer to the captured text as \1
. So, the command above simply looks for a comment that is adjacent to UpdateAccountGUIDs
, extends till the first end of comment statement and then does the replacement.