Thanks for the feedback guys. In the end I discovered the following REALLY 
helpful web page "Sed - An Introduction" at 
http://www.grymoire.com/Unix/Sed.html. As it says in th introduction, the 
documentation for sed is poor, but it's a really powerful tool if you can 
find out how to use it.

This helped me to produce the following script which does exactly what I want, 
and does it very quickly. Although my requirement is very specific, I hope 
the technique for dealing with multiline search and replace might be useful 
for others.

#!/bin/bash
sed '
# Look for 'Type=0x21'
/Type=0x21/ {
# append the next line to the pattern space
N
# Look for 'Label=...00'
/Label=...00/ {
# Replace 'Type=0x21' with 'Type=0x22'
s/Type=0x21/Type=0x22/
# print the modified data
P
# Append the extra line
a\
EndLevel=1
# then delete the first line
D
}
}' $1 > /tmp/tempfile

mv /tmp/tempfile $1



Tom

On Tue, 25 Oct 2005 18:02, Volker Kuhlmann wrote:
> > Type=0x21           Type=0x22       # Same as previous except
> > Label= 2200         Label= 2200     # different value for Label
> > blah, blah          EndLevel=1
> >                     blah, blah
> >
> > The bit that's got me stumped is how to handle the Label line. Can
> > someone please give me hint how to do this?
>
> The problem I have with sed is that it's line oriented, not record
> oriented, and your job is record oriented. (Personally I'd use gawk.) It
> can be a pain to match across lines in sed, but it's possible in GNU
> sed.
>
> The match condition I'd try for the label line is whether the number
> ends in 2 zeros. You'd have to match all from the type= to the label= in
> one expression, and if matched, perform one or more search/replaces, and
> write out the result with any additional stuff you want appended after
> label= . You can ignore any text following label= in the same record.
>
> Does that help?
>
> Volker

Reply via email to