0

I have this command:

salt=123
sed -i '' "s,\/\* Replace with salts \*\/,$salt,g" ./wp-config.php

Which works well if I run it on a file that contains the line:

/* Replace with salts */

But if I assign salt to the following multiline, with single quotes:

define('AUTH_KEY',         'oT0.1IG0ajXBDk.uCy?T@d?L.LgK|D3fz={VCq/IdP0fy.ZrI~WuM-9+5:-/DyPj');
define('SECURE_AUTH_KEY',  '0PO8Zgs:g(CFkwFuIKy/[2[Kb|~BO@#+[WrmHZ dE8&u{LcPA.7P$OrH:% 7Bf=G');
define('LOGGED_IN_KEY',    'xPG6BW$d~~[!lL,e<r z~nlo}(~k^b?G`0.m!54rrRggaDbk%a7c(IRzBTx&MUUy');
define('NONCE_KEY',        'J#+Rn;lgjVzjW&X^hj/wi53Y/!B_[UClk1[H:?h6Je|$?dHSb`<n-f**RtNJl;,d');
define('AUTH_SALT',        '>ho*kxR)1M4t)zni|tpDY.wn!>|Z-sVoULi(bY|0NQu/|G-2gAq=A;$3I%|`}kNb');
define('SECURE_AUTH_SALT', '--d5e0/8%^@Yg+3r UFAB`e-hR,;iMr.X]SOn-b~nU+aH[i5_gy]%hE<8V5LqQv1');
define('LOGGED_IN_SALT',   'k;J.$+/3)k0fU]nJ7Dtbh)a!Nr[/2m<uZNO+(:AGu!(B+cVeiXM,]ePdwRn-$~O-');
define('NONCE_SALT',       ':)M$l+-U,QqCFm>8yPa5rIu9nQ<%i4?[.&PY|X&ta-MTpBp#&z+0i?|#~`%P~7,c');

I get the following error:

sed: 1: "s,\/\* Replace with sal ...": bad flag in substitute command: '''

Is there a way I can totally escape the contents of the salt variable, and just do a flat out replace?

Others recommend using double quotes, but I think I'm already doing that?

Djave
  • 8,595
  • 8
  • 70
  • 124
  • Oh so you mean that you want these many contents to a single variable `salt` here? Then it means we need to replace a string with these multiple lines? – RavinderSingh13 Oct 02 '19 at 12:55
  • @RavinderSingh13 yes that is exactly correct – Djave Oct 02 '19 at 13:17
  • The problem isn't single quotes, but commas in the replacement string. – Benjamin W. Oct 02 '19 at 13:20
  • Maybe I shouldn't be using `sed` at all... – Djave Oct 02 '19 at 13:22
  • And `&` in the replacement string. – Benjamin W. Oct 02 '19 at 13:23
  • `following multiline` sed doesn't handle multiline replacement strings because `sed` commands are delimetered by a newline. – KamilCuk Oct 02 '19 at 13:24
  • I'm a +1 on the "shouldn't be using `sed` at all" side of the world. See the `awk`-based `gsub_literal` function in [BashFAQ #21](https://mywiki.wooledge.org/BashFAQ/021), which is a much better fit-to-task. – Charles Duffy Oct 02 '19 at 13:49
  • Incidentally, we already have SO questions here about doing arbitrary replacements, in which `gsub_literal` is already given as an answer; see f/e https://stackoverflow.com/questions/31110862/how-to-safely-handle-n-with-sed-on-automated-scripts-where-n-can-be-in-the -- @KamilCuk, as someone who's taken a closer look at this question than I have, can you opine on whether/why this is or is not duplicative? – Charles Duffy Oct 02 '19 at 13:50
  • Thanks @CharlesDuffy this is my first bash script so I might be trying to fit a square peg in a round hole... – Djave Oct 02 '19 at 13:54

1 Answers1

0

Write the replacement string into a temporary file and then use r sed command.

With bash command redirection this is just:

sed '/\/\* Replace with salts \*\/{ s/.*//; r'"<(cat <<<"$salt")"$'\n''}' ./wp-config.php

Remember that sed command are delimetered with a newline, you need a newline after r command (the ; will not work).

The command above matches the regex \/\* Replace with salts \*\ and if matched, substitutes current line for nothing and then appends the output of $salt variable to the output. Because it "appends", you will see an empty line prepended to $salt output.

Tested with:

salt=$(cat <<'EOF'                                                                                  
define('AUTH_KEY',         'oT0.1IG0ajXBDk.uCy?T@d?L.LgK|D3fz={VCq/IdP0fy.ZrI~WuM-9+5:-/DyPj');
define('SECURE_AUTH_KEY',  '0PO8Zgs:g(CFkwFuIKy/[2[Kb|~BO@#+[WrmHZ dE8&u{LcPA.7P$OrH:% 7Bf=G');
define('LOGGED_IN_KEY',    'xPG6BW$d~~[!lL,e<r z~nlo}(~k^b?G`0.m!54rrRggaDbk%a7c(IRzBTx&MUUy');
define('NONCE_KEY',        'J#+Rn;lgjVzjW&X^hj/wi53Y/!B_[UClk1[H:?h6Je|$?dHSb`<n-f**RtNJl;,d');
define('AUTH_SALT',        '>ho*kxR)1M4t)zni|tpDY.wn!>|Z-sVoULi(bY|0NQu/|G-2gAq=A;$3I%|`}kNb');
define('SECURE_AUTH_SALT', '--d5e0/8%^@Yg+3r UFAB`e-hR,;iMr.X]SOn-b~nU+aH[i5_gy]%hE<8V5LqQv1');
define('LOGGED_IN_SALT',   'k;J.$+/3)k0fU]nJ7Dtbh)a!Nr[/2m<uZNO+(:AGu!(B+cVeiXM,]ePdwRn-$~O-');
define('NONCE_SALT',       ':)M$l+-U,QqCFm>8yPa5rIu9nQ<%i4?[.&PY|X&ta-MTpBp#&z+0i?|#~`%P~7,c');
EOF
)

cat >wp-config.php  <<'EOF'
dfasfsda
/* Replace with salts */
fdasdfsa
EOF

the command will output:

dfasfsda

define('AUTH_KEY',         'oT0.1IG0ajXBDk.uCy?T@d?L.LgK|D3fz={VCq/IdP0fy.ZrI~WuM-9+5:-/DyPj');
define('SECURE_AUTH_KEY',  '0PO8Zgs:g(CFkwFuIKy/[2[Kb|~BO@#+[WrmHZ dE8&u{LcPA.7P$OrH:% 7Bf=G');
define('LOGGED_IN_KEY',    'xPG6BW$d~~[!lL,e<r z~nlo}(~k^b?G`0.m!54rrRggaDbk%a7c(IRzBTx&MUUy');
define('NONCE_KEY',        'J#+Rn;lgjVzjW&X^hj/wi53Y/!B_[UClk1[H:?h6Je|$?dHSb`<n-f**RtNJl;,d');
define('AUTH_SALT',        '>ho*kxR)1M4t)zni|tpDY.wn!>|Z-sVoULi(bY|0NQu/|G-2gAq=A;$3I%|`}kNb');
define('SECURE_AUTH_SALT', '--d5e0/8%^@Yg+3r UFAB`e-hR,;iMr.X]SOn-b~nU+aH[i5_gy]%hE<8V5LqQv1');
define('LOGGED_IN_SALT',   'k;J.$+/3)k0fU]nJ7Dtbh)a!Nr[/2m<uZNO+(:AGu!(B+cVeiXM,]ePdwRn-$~O-');
define('NONCE_SALT',       ':)M$l+-U,QqCFm>8yPa5rIu9nQ<%i4?[.&PY|X&ta-MTpBp#&z+0i?|#~`%P~7,c');
fdasdfsa
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 2
    If you don't want the empy line, you can do `r` first, then `d`, something like `sed -e '\#pattern#{r salts' -e 'd}'` (where `salts` is just a file containing the replacement strings). – Benjamin W. Oct 02 '19 at 13:34