[comp.unix.shell] sed question

wmark@wb3ffv.ampr.org (Mark Winsor) (05/16/91)

I know that 

sed -n '/PATTERN1/,/PATTERN2/p' filename

will print everything between pattern 1 & 2, but what if I only want the
first occurence of these patterns in the file? I know I can pipe the above
statement to sed '/PATTERN2/q' but there has got to be a way to do this
in one sed process. Thanks.

Mark S. Winsor
ProVAR, Inc.

rouben@math16.math.umbc.edu (Rouben Rostamian) (05/16/91)

In article <3880@wb3ffv.ampr.org> wmark@wb3ffv.ampr.org (Mark Winsor) writes:
>I know that 
>
>sed -n '/PATTERN1/,/PATTERN2/p' filename
>
>will print everything between pattern 1 & 2, but what if I only want the
>first occurence of these patterns in the file? I know I can pipe the above
>statement to sed '/PATTERN2/q' but there has got to be a way to do this
>in one sed process. Thanks.

Here it is -- one sed process:

sed -n -e '
/PATTERN1/,/PATTERN2/{
p
:loop
n
/PATTERN2/!{
p
bloop
}
p
q
}' <filename

--
Rouben Rostamian                          Telephone: (301) 455-2458
Department of Mathematics and Statistics  e-mail:
University of Maryland Baltimore County   bitnet: rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.              internet: rouben@math9.math.umbc.edu

dattier@vpnet.chi.il.us (David W. Tamkin) (05/17/91)

rouben@math16.math.umbc.edu (Rouben Rostamian) wrote in
<1991May16.043510.16184@umbc3.umbc.edu>:

| In article <3880@wb3ffv.ampr.org> wmark@wb3ffv.ampr.org (Mark Winsor) writes:
| >I know that 
| >
| >sed -n '/PATTERN1/,/PATTERN2/p' filename
| >
| >will print everything between pattern 1 & 2, but what if I only want the
| >first occurence of these patterns in the file? I know I can pipe the above
| >statement to sed '/PATTERN2/q' but there has got to be a way to do this
| >in one sed process. Thanks.

| Here it is -- one sed process:
| 
| sed -n -e '
| /PATTERN1/,/PATTERN2/{
| p
| :loop
| n
| /PATTERN2/!{
| p
| bloop
| }
| p
| q
| }' <filename

sed -n '/PATTERN1/,/PATTERN2/p
/PATTERN2/q' filename

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier@vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591

peterc@suite.sw.oz.au.sw.oz.au (Peter Chubb,-x27,6982322,3982735) (05/17/91)

From article <1991May16.043510.16184@umbc3.umbc.edu>, by rouben@math16.math.umbc.edu (Rouben Rostamian):
> In article <3880@wb3ffv.ampr.org> wmark@wb3ffv.ampr.org (Mark Winsor) writes:
>>I know that 
>>
>>sed -n '/PATTERN1/,/PATTERN2/p' filename
>>
>>will print everything between pattern 1 & 2, but what if I only want the
>>first occurence of these patterns in the file? I know I can pipe the above
>>statement to sed '/PATTERN2/q' but there has got to be a way to do this
>>in one sed process. Thanks.
> 
> Here it is -- one sed process:
	(sed script using branches elided)

Why not use multiple -e options?  That's a little simpler than using
branches and labels. 

Thus:
	sed -n -e '/PATTERN1/,/PATTERN2/p' -e '/PATTERN2/,$d' filename


			Regards,

				- Peter Chubb

Softway Pty Ltd, P.O. Box 305, Strawberry Hills, NSW 2012, AUSTRALIA
Phone: +61 2 698 2322;       Fax: +61 2 699 9174;     Telex: AA27987
Internet: peterc@softway.oz.au	   UUCP: ...!uunet!softway.oz!peterc

anw@maths.nott.ac.uk (Dr A. N. Walker) (05/21/91)

In article <1991May17.002433.15615@vpnet.chi.il.us> dattier@vpnet.chi.il.us
(David W. Tamkin) writes:

> sed -n '/PATTERN1/,/PATTERN2/p
> /PATTERN2/q' filename

Try

	sed '/PATTERN1/,$ !d; /PATTERN2/ q' filename

which works even if PATTERN2 happens to occur before PATTERN1, and
which mentions PATTERN2 only once [less to maintain!].

-- 
Andy Walker, Maths Dept., Nott'm Univ., UK.
anw@maths.nott.ac.uk