Replacing a string by a file using Sed
Solution 1
The following command should work for what you want:
sed "s/this is test/$(cat bar)/" foo
If foo
contain more then one line, then you can use:
sed "s/this is test/$(sed -e 's/[\&/]/\\&/g' -e 's/$/\\n/' bar | tr -d '\n')/" foo
or:
sed -e '/this is a test/{r bar' -e 'd}' foo
Source of the last two commands: Substitute pattern within a file with the content of other file
To make the change in foo
file, use sed -i
.
Solution 2
One way:
sed -e '/this is test/r bar' -e '/this is test/d' foo
Sample result:
$ cat bar
12
23
$ cat foo
ab
this is test
cd
this is test
ef
$ sed -e '/this is test/r bar' -e '/this is test/d' foo
ab
12
23
cd
12
23
ef
Solution 3
When you need to replace an entire line with the contents of a file, you can use r
to insert a file and d
to delete the current line:
sed -e "/regex/{r/path/to/file" -e "d}"
Related videos on Youtube
heemayl
Not much active here anymore. Spent substantial time on learning Communication Engineering, now being fascinated by the complexities of Computers. Also passionate about some other stuffs especially Cycling and singing YNWA loudly. If you want to get in touch, mail me: [email protected] Oh... and BTW if you love RegEx, We can be friends ;)
Updated on September 18, 2022Comments
-
heemayl over 1 year
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 ofbar
. 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.
-
Avinash Raj over 9 yearsdoes the contents of the file bar contains newline characters?
-
heemayl over 9 years@AvinashRaj: Yes.
-
heemayl over 9 years@AvinashRaj: please check my edits.
-
David Foerster over 9 yearsDoes it have to be
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. -
heemayl over 9 yearsI was trying to check this using sed. So it would be great if a solution is available in Sed.
-
αғsнιη over 9 yearsIf you mean "this is test" is a separate line then solution is using
sed $'/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нιη over 9 yearsescape it with "\" like
sed $'/\^/ {r bar\n d}' foo
-
heemayl over 9 yearsActually i got my answer but this is just an extension to check if any tweaking can be done further.
-
-
Avinash Raj over 9 yearsit's not working for me..
-
heemayl over 9 years@Radu: Please check my edits. I have tried this already.
-
muru over 9 years@heemayl you used the wrong quotes.
-
Radu Rădeanu over 9 years@heemayl No, you tried using simple quotes, in my answer I used double quotes. See Difference between single and double quotes in bash. Single quotes won't interpolate anything, but double quotes will (for example variables, backticks, certain \ escapes, etc...)
-
heemayl over 9 years@RaduRădeanu: Yes, I got it. please check my edits for problem regarding new lines.
-
heemayl over 9 yearsNot working in my case.
-
Guru over 9 years"Not working" means error or result not as expected or something else?
-
heemayl over 9 yearsSame mentioned error i.e. sed: -e expression #1, char 9: unterminated `s' command
-
Radu Rădeanu over 9 years@AvinashRaj There is a solution for this also...
-
Radu Rădeanu over 9 years@heemayl Check my new edits
-
heemayl over 9 yearsCan you tell me please what /r is doing here?
-
Radu Rădeanu over 9 years@heemayl
r
- append file to stdout after flush. See unix.stackexchange.com/a/26290/37944 -
steeldriver over 9 years@heemayl please check that you have typed @Guru's answer exactly as shown - it does not use an
s
command -
αғsнιη over 9 yearsI don't know if your answer is correct since the OP's question said replace the string "this is test" and not replace the line "this is test". If the line is "this is test in foo file" in foo file, then you deleted "in foo file" string at the end of that line(in your second command). but he/she marked as answer then I think the "this is test" is in a separate line not string into line but again title says that is STRING.
-
αғsнιη over 9 yearsAnd also you have this problem too.
-
heemayl over 9 years@KasiyA: I meant line in this case but what if i want to replace the start of first line of foo with content of file bar?