Hi all,
I was reading the Sed question to the list in the archives (sorry, still
not re-subscribed :) and Padraig's answer, which is no good...
John A. Kinsella wrote:
> I'd like to do a simple conversion
> s/Maple 8/Maple 9/g
> in a bunch of Maple *.mws files using sed.
>
> Is there a way to tell sed to do the substitution while preserving the
> line breaks (which Maple seems to need).
There is, but it's a mess. The problem is that sed's buffer only stores
lines, usually. You need to use G and H to add the current line to the
stored pattern buffer, and retrieve it. It's all a big mess...
But here goes a try...
sed -e '
N
s/Maple\([\n ]\{1,\}\)8/Maple\19/g
P
D' testfile
The D is the trick - since there'll be stuff left in the pattern buffer
every time until the last, it restarts without automatically dumpling
the pattern buffer, and without reading a new line. We dump and read
explicitly with P and N respectively. Then when N fails, sed does what
it always does, prints the pattern space (the last line) and quits.
I thought \19 might cause a problem, but apparrently sed does the right
thing.
Cheers,
Dave.
--
David Neary
Phenix Engineering
110 ave Jean Jaures, 69007 Lyon
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!