Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 "/SUBSTITUTETHIS/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
Sounds very much like homework..... But in general this looks like you want to replace string REPLACETHIS with something else, which will by default remove REPLACETHIS –  Karlson Sep 28 '12 at 12:29
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

2 Answers 2

up vote 2 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

Assuming your temp.txt file has EXACTLY what you want to replace SUBSTITUTETHIS with and nothing else:

sed -i -e "s/SUBSTITUTETHIS/`cat temp.txt`/" devel.txt
share|improve this answer
Supposing temp.txt contains no /. –  manatwork Sep 28 '12 at 12:36
true, good point –  h3rrmiller Sep 28 '12 at 12:41
sed: -e expression #1, char 21: unterminated `s' command is the output that I get when I litterally execute this command –  Bernhard Sep 28 '12 at 13:21
see my comment to Gilles' answer to know risks of using this code. also, this should be the reason of @Bernhard 's error. –  caesarsol Sep 11 '14 at 12:30

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.