[comp.editors] sed address arithmetic

reberhar@iiic.ethz.ch (Rolf Georg Eberhardt) (09/20/90)

Hello Netlanders

Is it possible to do address arithmetic in sed? I would like to do something like

/searchpattern/+3 s/^/<AA>/

ie. inserting the tag <AA> three lines under all the lines containing 
searchpattern.


Thanks.

-- rolf

tarsa@abyss.zk3.dec.com (Greg Tarsa) (09/21/90)

In article <8738@neptune.inf.ethz.ch>, reberhar@iiic.ethz.ch (Rolf Georg
Eberhardt) writes:
|> Hello Netlanders
|> 
|> Is it possible to do address arithmetic in sed? I would like to do
something like
|> 
|> /searchpattern/+3 s/^/<AA>/
|> 
|> ie. inserting the tag <AA> three lines under all the lines containing 
|> searchpattern.

Yes, but not directly, as you have here.  Try the following:

	/searchpattern/{n;n;n;i\
<AA>
}

Greg
 
Tarsa Software Consulting
--------------------------------
		33 Seabee Street
		Bedford, NH 03102	tarsa@elijah.mv.com
		(603)668-9226		{decuac,decvax}!elijah!tarsa

meyering@cs.utexas.edu (Jim Meyering) (09/22/90)

In article <8738@neptune.inf.ethz.ch> reberhar@iiic.ethz.ch
(Rolf Georg Eberhardt) writes:

>Is it possible to do address arithmetic in sed?
> I would like to do something like
>
>/searchpattern/+3 s/^/<AA>/

Here's another solution (when reasonable, I prefer
to avoid sed's multiline commands: i,a,c...):
This is a bit ugly, though...

    % sed '/searchpattern/{N;N;p;g;N;s/^/<AA>/;}'

With at least SunOS4.0.3's /bin/sed, (due to a bug)
there must be a semicolon following the `s' command
at the end of a {} group.
-- 
Jim Meyering          meyering@cs.utexas.edu