Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Say I have a text file like this:

# custom content section
a
b

### BEGIN GENERATED CONTENT
c
d
### END GENERATED CONTENT

I'd like to replace the portion between the GENERATED CONTENT tags with the contents of another file.

What's the simplest way to do this?

share|improve this question

4 Answers 4

lead='^### BEGIN GENERATED CONTENT$'
tail='^### END GENERATED CONTENT$'
sed -e "/$lead/,/$tail/{ /$lead/{p; r insert_file
        }; /$tail/p; d }"  existing_file
share|improve this answer
    
Excellent. sed can do so much more than just s/.../...! –  DevSolar Jun 22 '12 at 8:28
    
Hmmm does not work for me, should I put the sed command on one line? –  lzap Jul 11 at 11:15
    
The r insert_file command must be the last thing on its line. Note that neither whitespace nor comments are allowed after it. The code was tested using GNU sed with the --posix option enabled, and it worked as expected, so it should work with any posix compliant sed. –  Peter.O Jul 12 at 1:58
up vote 2 down vote accepted
newContent=`cat new_file`
perl -0777 -i -pe "s/(### BEGIN GENERATED CONTENT\\n).*(\\n### END GENERATED CONTENT)/\$1$newContent\$2/s" existing_file
share|improve this answer
    
Nice job. Much simpler than mine. :) –  Dr Kitty Jun 22 '12 at 6:12

Warning: This is definitely not the simplest way to do it. (EDIT: bash works; POSIX grep is fine too)

If the main text is in file "main" and the generated content is in file "gen", you could do the following:

#!/bin/bash
BEGIN_GEN=$(cat main | grep -n '### BEGIN GENERATED CONTENT' | sed 's/\(.*\):.*/\1/g')
END_GEN=$(cat main | grep -n '### END GENERATED CONTENT' | sed 's/\(.*\):.*/\1/g')
cat <(head -n $(expr $BEGIN_GEN - 1) main) gen <(tail -n +$(expr $END_GEN + 1) main) >temp
mv temp main
share|improve this answer
    
Does this work? I think your last line will open main for writing, clearing it, before it is read by cat. –  chepner Jun 22 '12 at 14:05
    
@chepner Crap, you're right. The rest works, though. I'll fix it. –  Dr Kitty Jun 22 '12 at 18:39
ed -s FILE1 <<EOF
/### BEGIN GENERATED/+,/### END GENERATED/-d
/### BEGIN GENERATED/ r FILE2
w
q
EOF
share|improve this answer
    
Using a heredoc and the ed line editor. The first line inside the heredoc is to deleted 'd' the line after '+' the '### BEGIN gENERATED...' and the line before '-' the '### END GENERATED...' the second line is to insert FILE2 after the line ### END GENERATED...' –  Jetchisel Sep 3 at 23:27
    
sorry what i meant was insert FILE2 after the line '### BEGIN GENERATED..' –  Jetchisel Sep 3 at 23:47
    
Take it easy on me folks, it is after all my first time :-). Also i might have used the word the same but the other solution does not use a heredocs but a printf and a pipe. Any way apologies if i have made a mistake :-) –  Jetchisel Sep 8 at 0:41

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.