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

Re: [Sheflug] Replacing a block of text in a file with text fromanother file



Thanks for the tips David and Chris - I really appreciate you taking the time to give me your suggestions.

I fiddled around a bit more and have managed to come up with a work-around, although it looks pretty horrible, and I'm sure there's a neater way to do it .

#!/bin/bash
sed -e '/zmatrix angstrom/,/end/cREPLACEME' <$1 >$1.tmp
sed '/REPLACEME/ {
r basis.txt
d
}' <$1.tmp >$1._in
rm $1.tmp

The link to the sed faq that David fowarded was really useful and I also found the following pretty good too, in case anyone else wants to delve into sed (something I wouldn't recommend unless you are pretty bloody-minded or seriously averse to Perl or Python).

http://www.grymoire.com/Unix/Sed.html
http://www-h.eng.cam.ac.uk/help/tpl/unix/sed.html

Thanks again,

Jens

Chris Johnson wrote:

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'd use (and have used) perl to do this. I had a template html file that
needed stats inserting at a certain point.

Not sure how good your perl is (hopefully better than mine) but the
structure should look a bit like this.

Read inputfile line
if inputfile line = starttag then {
open insertfile
read insertfile line by line
write insert file lines to outputfile

read input file until inputfile line = endtag #(this will skip to the end
of the bit to miss out)
}
if inputfile line <> start tag and inputfile line <> end tag write inputfile
line to outputfile.

Hope this isn't too crypic. I don't have time at the mo to put it in real
code and without testing I'd probably mix my php and perl together and leave
you with a load of useless code.

It can probably be done in a lot less code. You have to get perl/sed etc to
match over multiple lines which I was never any good at and it took me less
time to code it in long hand than try to be clever and do it in a one liner.

HTH

Chris Johnson
(No not that one, the other one!)
(running SUSE 9, 512MB PIII 600 (ish))

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.734 / Virus Database: 488 - Release Date: 04/08/2004


___________________________________________________________________

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

GNU the choice of a complete generation.


___________________________________________________________________

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

 GNU the choice of a complete generation.