'How to find and replace text with sed but with special characters

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?



Solution 1:[1]

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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew