Home > Software design >  How to find and replace text with sed but with special characters
How to find and replace text with sed but with special characters

Time:05-16

I am writing some bash script from wchich I need to replace some text in another file I need to find and replace the following text in myfile

$conf['extra_login_security'] = true;

with:

$conf['extra_login_security'] = false;

so I tried the following:

sed -i 's_extra_login_security'] = true_extra_login_security'] = false_g' myfile.php

but it did not work I am getting the following error:

sed: -e expression # 1, character 15: unknown option for the `s' command

can you help me and tell what I am doing wrong?

CodePudding user response:

You can use

sed "s/\(extra_login_security'] = \)true/\1false/" myfile.php

Details:

  • \(extra_login_security'] = \) - Capturing group 1: extra_login_security'] = string
  • true - a fixed string
  • \1false - replacement: Group 1 value false substring.

See the online demo:

#!/bin/bash
s="\$conf['extra_login_security'] = true;"
sed "s/\(extra_login_security'] = \)true/\1false/" <<< "$s"
# => $conf['extra_login_security'] = false;
  • Related