Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text file (devel.xml).

I added the word REPLACETHIS to it in order to replace this string with the content within a different file (temp.txt).

The closest thing I have is this:

sed -i -e "/REPLACETHIS/r temp.TXT" -e "s///" devel.txt;

This inserts the content after the string, and then deletes the string afterwards.

Is this the best way to do it?

share|improve this question
Does it have to be sed? Depending on how much text is in temp.txt, I might step up to a perl solution. – user17591 Sep 28 '12 at 14:24
up vote 5 down vote accepted

What you've done is to remove SUBSTITUTETHIS wherever it appears in the file (but not the rest of the line where it appears) and insert the content of temp.TXT below that line. If SUBSTITUTETHIS appears multiple times on a line, only the first occurrence is removed, and only one copy of temp.TXT is added.

If you want to replace the whole line when SUBSTITUTETHIS appears, use the d command. Since you need to run both r and d when there's a match, put them in a braced group.

sed -e '/SUBSTITUTETHIS/ {' -e 'r temp.TXT' -e 'd' -e '}' -i devel.txt

Some sed implementations let you use semicolons to separate commands and omit separators altogether around braces, but you still need a newline to terminate the argument to the r command:

sed -e '/SUBSTITUTETHIS/ {r temp.TXT
                          d}' -i devel.txt

If you want to replace SUBSTITUTETHIS by the content of the file, but retain what comes before and after it on the line, it's more complicated. The simplest method is to include the content of the file in the sed command; note that you'll have to properly quote its contents.

sed -e "s/SUBSTITUTETHIS/$(<temp.TXT sed -e 's/[\&/]/\\&/g' -e 's/$/\\n/' | tr -d '\n')/g" -i devel.txt

Or use Perl.

perl -pe 's/SUBSTITUTETHIS/`cat temp.TXT`/ge' -i devel.txt
share|improve this answer
Thanks! That was very helpful!!! – RafaelGP Oct 1 '12 at 8:08
the proposed perl solution has a major problem, the file temp.TXT can't contain any of these characters: `'"/` and any other character that could interfere with perl or even bash commands. this could also result in execution of involuntary commands on the shell! isn't it possible to achieve this with a pure perl solution? – caesarsol Sep 11 '14 at 12:27
@caesarsol The Perl solution works with any character, as far as I can tell. Note that the Perl snippet is between single quotes, not between double quotes, so no shell expansion will occur. In Perl, the backquote operator is parsed as part of parsing the script; the output from the command is treated as a string, not parsed further. What test case fails? – Gilles Sep 11 '14 at 15:18
@Gilles i beg your pardon, missed the single-quote detail and the e flag in the regexp! the problem i was talking about is in @h3rrmiller answer. should really refresh my poor perl skills ;) – caesarsol Sep 12 '14 at 9:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.