[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Sheflug] Replacing a block of text in a file with text from another file



Hello

I've tried posting this to the Manchester Linux User Group, but haven't heard anything back, so I thought I'd try here - apologies to anyone who is on both groups and has seen this before!

I'm trying to write a script that will go through a bunch of input files I have and replace all the text between the lines 'zmatrix angstrom' and 'end' with text the from a file (the input file is test.in and the text to be inserted from basis.txt in the example below).

I think that sed should be able to do the actual insertion bit, but I'm having no luck trying to work out exactly what I need to do. I've got as far as:

sed -e '/zmatrix angstrom/,/end/{
r basis.txt
d}' < test.in

which inserts the text from the file as many times as there are lines in the pattern between 'zmatrix angstrom' and 'end', whereas I just want the file inserted once for the entire block. I've tried several ways of doing this, but each time the action of inserting the file is repeated for each line and I can't work out how to specify the entire pattern as the entity to be replaced.

Any hints on how to go about this would be greatly apreciated (I know this would be easy to do with something like Python or Perl, but I'd really like to try and do this with a bash script if at all possible).

Many thanks in advance,

Jens
___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

GNU the choice of a complete generation.