Replacing a string by a file using Sed

sed
19,833

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}"
Share:
19,833

Related videos on Youtube

heemayl
Author by

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, 2022

Comments

  • heemayl
    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 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.

    • Avinash Raj
      Avinash Raj over 9 years
      does the contents of the file bar contains newline characters?
    • heemayl
      heemayl over 9 years
      @AvinashRaj: Yes.
    • heemayl
      heemayl over 9 years
      @AvinashRaj: please check my edits.
    • David Foerster
      David Foerster over 9 years
      Does 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
      heemayl over 9 years
      I was trying to check this using sed. So it would be great if a solution is available in Sed.
    • αғsнιη
      αғsнιη over 9 years
      If 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 using X=$(echo $(cat bar));sed "s/this is test/$X/" foo.
    • αғsнιη
      αғsнιη over 9 years
      escape it with "\" like sed $'/\^/ {r bar\n d}' foo
    • heemayl
      heemayl over 9 years
      Actually i got my answer but this is just an extension to check if any tweaking can be done further.
  • Avinash Raj
    Avinash Raj over 9 years
    it's not working for me..
  • heemayl
    heemayl over 9 years
    @Radu: Please check my edits. I have tried this already.
  • muru
    muru over 9 years
    @heemayl you used the wrong quotes.
  • Radu Rădeanu
    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
    heemayl over 9 years
    @RaduRădeanu: Yes, I got it. please check my edits for problem regarding new lines.
  • heemayl
    heemayl over 9 years
    Not working in my case.
  • Guru
    Guru over 9 years
    "Not working" means error or result not as expected or something else?
  • heemayl
    heemayl over 9 years
    Same mentioned error i.e. sed: -e expression #1, char 9: unterminated `s' command
  • Radu Rădeanu
    Radu Rădeanu over 9 years
    @AvinashRaj There is a solution for this also...
  • Radu Rădeanu
    Radu Rădeanu over 9 years
    @heemayl Check my new edits
  • heemayl
    heemayl over 9 years
    Can you tell me please what /r is doing here?
  • Radu Rădeanu
    Radu Rădeanu over 9 years
    @heemayl r - append file to stdout after flush. See unix.stackexchange.com/a/26290/37944
  • steeldriver
    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нιη
    αғsнιη over 9 years
    I 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нιη
    αғsнιη over 9 years
    And also you have this problem too.
  • heemayl
    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?