1

I have a template file I want to copy and then edit from a script, inserting content at specific template points. For example, my template file might be something like,

...
rm -rf SomeDirectory
make install
#{INSERT-CONTENT-HERE}
do-something-else
...

In another script, I want to add content at "#{INSERT-CONTENT-HERE}" within a loop, i.e.

for i in c; do
  # Write content to the template file copy at the correct point.
done

I think sed is the right tool, but I'm not familiar enough to know the syntax, and the man page isn't helping.

5
  • Thanks for the update, and good luck. Do you also have a question? Commented Mar 19, 2012 at 20:28
  • 1
    For those with IQDCS (Inferred Question Discernment Challenge Syndrome), the question is, "What would be the correct command to do this?" Commented Mar 19, 2012 at 20:32
  • Inferred? Implied? Infrared? :-) Commented Mar 19, 2012 at 20:32
  • what sort of thing are you iterating in the for loop? the content you want to replace? other copies of the templates? Commented Mar 19, 2012 at 20:33
  • @eljunior: Right now, it is a series of RPM spec file commands based on a bunch of files. The loop gets the file names across certain directories meeting certain requirements, applies some transformation logic, and builds the string command. That command needs to be inserted into the template, once for each file. Commented Mar 19, 2012 at 20:36

2 Answers 2

1

An example:

echo "Line #{INSERT-CONTENT-HERE}" | sed 's/#{INSERT-CONTENT-HERE}/---/'

To modify a file:

sed -i 's/#{INSERT-CONTENT-HERE}/---#{INSERT-CONTENT-HERE}/' filename

where -i means in-place edit so be warned

if you do:

sed -i.bak 's/#{INSERT-CONTENT-HERE}/---/' filename

it should back up original as filename.bak

also to make multiple substitutions at each line use the g flag:

sed -i.bak 's/#{INSERT-CONTENT-HERE}/---/g' filename
Sign up to request clarification or add additional context in comments.

3 Comments

Am I mistaken, or would that only get me past the first iteration of the loop? (After which the marker is gone.) Also, I don't want to have to regex-escape the replacement text if possible. Would that be necessary in your solution?
@DaveMateer, this should be enough to do what you want (here filename is the template). You don't need to escape the replacement text (mostly I guess).
look at the second example, you can move the marker as long as you have additions.
1

You can copy the output of all the commands into a temporary file and then copy the contents of that entire file into the template file:

TEMPFILE=`mktemp` && (
  for i in c
    echo "SomeTextBasedOn $i" >> $TEMPFILE
  done
  sed -i '/{INSERT-CONTENT-HERE}/r '$TEMPFILE targetfile
  rm $TEMPFILE
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.