Lets say i have two files foo and bar. I want a replace the string "this is test" in foo with the contents of the file bar. How can i do this using a one liner sed?
I have used:
sed -i.bak 's/this is test/$(cat bar)\n/g' foo
but the the string is being replaced by literal $(cat bar)
rather than the contents of bar
. I have tried using quotations but the result remains the same.
Radu's answer is correct as far as the quote is concerned. Now the problem is lets say my bar file contains:
this
is
a
test
file
Now if i run the command it gives an error:
sed: -e expression #1, char 9: unterminated `s' command
18 times.
sed
? To avoid escaping issues, I'd rather use another scripting language that is better suited for file interaction like Awk, Python, Perl, Ruby, or whatnot. – David Foerster Oct 23 '14 at 13:13sed $'/this is test/ {r bar\n d}' foo
.otherwise if "this is test" is a string in a line then solution is usingX=$(echo $(cat bar));sed "s/this is test/$X/" foo
. – αғsнιη Oct 23 '14 at 13:22sed $'/\^/ {r bar\n d}' foo
– αғsнιη Oct 23 '14 at 13:44