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
add comment

2 Answers

up vote 1 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
add comment

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
4  
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
add comment

Your Answer

 
discard

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.