using Sed to search and replace text in XML file
I have the following text in File.XML:
<!--UpdateAccountGUIDs>UpdateAndExit</UpdateAccountGUIDs-->I would like to replace that with
<UpdateAccountGUIDs>UpdateAndExit</UpdateAccountGUIDs>I tried:
$ sed -i 's/<!--UpdateAccountGUIDs>UpdateAndExit</UpdateAccountGUIDs-->/<UpdateAccountGUIDs>UpdateAndExit</UpdateAccountGUIDs>/' File.XMLBut get this error:
sed: -e expression #1, char 63: unknown option to `s' 1 Answer
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.XMLor
sed -i 's/<!--UpdateAccountGUIDs>UpdateAndExit<\/UpdateAccountGUIDs-->/<UpdateAccountGUIDs>UpdateAndExit<\/UpdateAccountGUIDs>/' File.XMLNote 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.XMLHere, 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.
More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"