Replace string with multiline file content
Solution 1
Another perl
way:
perl -pe 's/SALT/`cat salt.txt`/e' wp-config.php > result.txt
The key here is the /e
regexp option allowing us to use a perl
command result as a substitution string.
Solution 2
You can do this:
sed -e "/SALT/{r salt.txt" -e "d}" wp-config.php > result.txt
Where salt.txt
is the salt, wp-config.php
is the input file and SALT
is the string to replace
Solution 3
If you want to stick to bash
, choose a character that doesn't appear in neither your string nor in your file, let's say @
; then:
SALT=`< salt.txt tr '\n' '@'`
sed "s/SALT/$SALT/" wp-config.php | tr '@' '\n' > result.txt
This way before the replacement the newline
characters in your string are changed to @
and after the replacement the @
characters are changed back to newlines
, so that SALT
is not treated as an array anymore but just as a variable containing a long string.
Related videos on Youtube
Emetrop
Updated on September 18, 2022Comments
-
Emetrop over 1 year
I need to replace string
SALT
in a file with content of another file. Problem is that the input file has multilines. I tried something like this in my bash script:SALT=`cat salt.txt`; sed "s/SALT/$SALT/" wp-config.php > result.txt
It work's fine when the salt.txt is single line, but if there are more lines it fails. I've read that it could do PERL. But I don't know how. Could you help me?
-
αғsнιη about 9 yearsThis will replace whole line with content of file salt.txt if containing a SALT in it not SALT word itself!!
-
Tuan Anh Tran about 4 yearsis there anyway to fix this? instead of replacing the whole line, just the keyword