Kismet Agbasi Kismet Agbasi - 1 year ago 86
Linux Question

How do I find and replace text using sed using ~ as delimiter


In a Bash shell script I'm putting together, I am trying to find this text: /usr/local/freeswitch/log/freeswitch.log and replace it with: /var/log/freeswitch/freeswitch.log in this file: /etc/fail2ban/jail.local

This is what I have tried so far, both of which result in the error: sed: -e expression #1, char 75: unterminated `s' command

Attempt #1

sed -i 's~usr/local/freeswitch/log/freeswitch.log~var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local

Attempt #2

sed -i 's~usr/local/freeswitch/log/freeswitch.log/var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local

My research shows that since the text I'm searching for includes the "/" character, I should be using a different delimiter "~" to separate the find and replace strings. But looks like I'm doing something wrong, any assistance would be appreciated, thanks.

Answer Source

The structure of a sed substitution command is s/PATTERN/REPLACEMENT/ (note the delimiter at the end of the command).

You're right, you can change the delimiter to a different character, so if you're going to use ~ you need to put one of those at the end of the command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download