Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.XML

But get this error:

sed: -e expression #1, char 63: unknown option to `s'
share|improve this question

1 Answer 1

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.

share|improve this answer
    
@c0rp thanks, fixed. –  terdon Apr 1 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.