0

I'm trying to replace some text with other text in a configuration file. I created a script with the sed command but I'm getting an error.

Original Line:

<property name="encryptData">false</property>

New Line:

<property name="encryptData">true</property>

Command:

sed -i -e 's/<property name="encryptData">false</property>/<property name="encryptData">true</property>/g' /Users/User/Desktop/mirth.cfg

Error:

sed: 1: "s/<property name="encry ...": bad flag in substitute command: '<'
1

2 Answers 2

5

The command is almost perfect. Your problem is that you need to precede the slash in </property> with a backslash\ like this.

sed -i -e 's/<property name="encryptData">false<\/property>/<property name="encryptData">true<\/property>/g' /Users/User/Desktop/mirth.cfg

Observed I did it for both occurrences.

In this way sed will know that this slash is part of the stream and not part of the command 's'.

Hope this fixes your issue.

5
  • That did it! I was tearing my hair out over what to escape. I tried it out for everything except the /. Thanks! Jan 11, 2016 at 5:21
  • 2
    @user491764: Don't thank Torrien! ;-) If this answer did help, just click the little grey under the number now turning it into beautiful green. That's the best thank you you can give him as it means which means Yes, this answer is valid! ;-)
    – Fabby
    Jan 11, 2016 at 20:50
  • I tried earlier. I don't have enough reputation yet for it to show my positive vote :( Jan 12, 2016 at 0:55
  • 1
    Not positive. Choose it as the right answer. Is your question, you choose the answer.
    – Torrien
    Jan 12, 2016 at 1:18
  • Oh! I found it! Jan 13, 2016 at 2:09
1

As the pattern you are looking for contains /, change the delimited for sed substitution command syntax. For example you can use _ and also use pattern grouping so that you can use back-referencing in substitution:

sed -r 's_(<property name="encryptData">)false(</property>)_\1true\2_' /Users/User/Desktop/mirth.cfg

Also it is always a good idea to take a backup while using the -i option:

sed -ri.bak 's_(<property name="encryptData">)false(</property>)_\1true\2_' /Users/User/Desktop/mirth.cfg

Now the original file will be kept as /Users/User/Desktop/mirth.cfg.bak and the modified file will be /Users/User/Desktop/mirth.cfg.

1
  • Thanks for the advice! I normally make a backup, but it's usually done manually. Jan 11, 2016 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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