How to ignore escape sequences in sed

Hello guys,

I have a file that contains this string
$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

and I would like to add the blowfish string insde single quotes
$cfg['blowfish_secret'] = 'H=ichbddbE[h[UqgOT.IBS6lOEAZtpH7';

1st try

  1. Find the line number contains that string
    blowfishLine=$(grep -F -n "$cfg['blowfish_secret']" /usr/share/secret_interfaceDB/config.inc.php | awk -F ":" '{print $1}' /path-file
  2. Put the blowfish data in a variable
    blowfishData="\$cfg['blowfish_secret'] = 'H=ichbddbE[h[UqgOT.IBS6lOEAZtpH7';"
  3. Using sed to change entire line
    sed "${blowfishLine}s/.*/${blowfishData}" /path-file
    And the terminal returns
    sed: -e expression #1, char 7: unterminated `s' command
    → It seems when I using double quote, it will eliminate the s function

2nd try
My ideal is using backslash to escape those special characters

sed 's/\$cfg\[\'blowfish_secret\'\] = \'\'/test/g' /path-file
Terminal returns
sed: can't read /usr/share/secrets/$cfg['blowfish_secret']: No such file or directory
sed: can't read =: No such file or directory

So how to walk through these issues?

Thanks!

hi, try the following .... (there's probably other/better, but this may do the job for you ....)
as always, test,check,test again ... repeat :slight_smile:

$
$ cat testfile
$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
$
$ sed  -i 's/\(blowfish_secret..\)\(.*;\)/\1 = \'\'H=ichbddbE[h[UqgOT.IBS6lOEAZtpH7\'\;/g'' testfile
$
$ cat testfile 
$cfg['blowfish_secret'] = 'H=ichbddbE[h[UqgOT.IBS6lOEAZtpH7'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
$ 

Hi,
The thing is that I have not known as well as understand your syntax
I can copy in Google to fix my issues, but it will make me like a copy cat
So can you break down for me to easy understand?
Thanks

Hi @bucminhdo,

try the following:

sed "s#\$cfg\['blowfish_secret'\] =.*#\$cfg['blowfish_secret'] = 'your-secret';#" infile

You can use any character as a separator, it's determined by the character right after the s command (I've used # here). $ and [ (and ]) have special meanings (not only) in a regex, therefore they have to be escaped by \ in order to handle them as literal chars (btw, escaping ] isn't really needed, try to find out why).

In the replacement string, escaping [ and ] isn't needed, cause it's not a regex. But since the complete string is enclosed by double/soft and not single/hard quotes, the shell would interpret $cfg as a variable, so the $ has to be escaped again. I've used double quotes, cause the regex and the replacement string contain several single quotes already, otherwise all the inner single quotes had to be escaped.

.* matches the rest of the string, it means any character any times. As an exercise, try to find out why it's there.

As always, there's not only one way:

sed "s/.cfg..blowfish_secret.*/\$cfg['blowfish_secret'] = 'your-secret';/" infile

This is a bit shorter, but less accurate: E.g. it would match #cfg("blowfish_secret_long")=..., too. Sometimes it's a matter of taste and/or readability and/or robustness etc. etc.

For changing the file in place, use sed -i, this will overwrite the original file. Without -i, the complete text will be printed to stdout resp. to the console. See man sed also.