[comp.unix.questions] A little help with SED please

dave@lsuc.uucp (David Sherman) (04/26/88)

murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
>I need an sed line (or some gory pipeline) to extract the data between
>BEGIN and END.

awk '/^BEGIN$/ { wantthis = 1; next }
/^END$/ { wantthis = 0; next }
{ if(wantthis) print $0 }'

The test for BEGIN can be augmented to spit out a dividing line of
some sort, if that's desired. Note that the line beginning with '{'
matches ALL lines.

I must confess that I programmed in C on UNIX systems for 11 years
before finally learning awk.  Should have gotten into it far sooner.
Awk is a tremendous tool, once you figure out how to use it.  (It's
a bit like pointers in C: one day it suddenly clicks.)

David Sherman
The Law Society of Upper Canada
Toronto
-- 
{ uunet!mnetor  pyramid!utai  decvax!utcsri  ihnp4!utzoo } !lsuc!dave

robert@pttesac.UUCP (Robert Rodriguez) (05/03/88)

In article <1988Apr26.011145.27914@lsuc.uucp> dave@lsuc.UUCP (David Sherman) writes:
>murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
>>I need an sed line (or some gory pipeline) to extract the data between
>>BEGIN and END.
>[more stuff on "awk" deleted]

I'm surprised nobody mentioned csplit.  It's perfect for this stuff,
as the man page says, csplit is a context splitter.

csplit -s -k foo /BEGIN/+1 /END/-1

should work nicely.

/*
 * .signature.... just say no.
 */

dg@lakart.UUCP (David Goodenough) (05/04/88)

From article <762@pttesac.UUCP>, by robert@pttesac.UUCP (Robert Rodriguez):
> In article <1988Apr26.011145.27914@lsuc.uucp> dave@lsuc.UUCP (David Sherman) writes:
>>murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
>>>I need an sed line (or some gory pipeline) to extract the data between
>>>BEGIN and END.
>>[more stuff on "awk" deleted]
> 
> I'm surprised nobody mentioned csplit.  It's perfect for this stuff,
> as the man page says, csplit is a context splitter.
> 
> csplit -s -k foo /BEGIN/+1 /END/-1

May work, but csplit is nowhere to be found on our system (BSD 4.3).

Seriously though, the following awk script does nicely, although it may
need a bit of work if there is more than one field on a line:

--- snip snip snippety snip --- snip snip snippety snip ---
 {
    if ($1 == "BEGIN")
     {
	state = 1
     }
    else if ($1 == "END")
     {
	state = 0
     }
    else if (state == 1)
     {
	print $1
     }
 }
--- snip snip snippety snip --- snip snip snippety snip ---
-- 
	dg@lakart.UUCP - David Goodenough		+---+
							| +-+-+
	....... !harvard!adelie!cfisun!lakart!dg	+-+-+ |
						  	  +---+

ok@quintus.UUCP (Richard A. O'Keefe) (05/06/88)

From article <762@pttesac.UUCP>, by robert@pttesac.UUCP (Robert Rodriguez):
> In article <1988Apr26.011145.27914@lsuc.uucp> dave@lsuc.UUCP (David Sherman) writes:
>>murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
>>>I need an sed line (or some gory pipeline) to extract the data between
>>>BEGIN and END.
>>[more stuff on "awk" deleted]
> I'm surprised nobody mentioned csplit.  It's perfect for this stuff,
> as the man page says, csplit is a context splitter.
> csplit -s -k foo /BEGIN/+1 /END/-1

There is a very good reason why nobody mentioned csplit: it doesn't do the
job.  The original poster had a file
	junk0
	BEGIN
	data1
	END
	junk1
	...
	junkn
	BEGIN
	datan
	END
	junk
and wanted all the "data" segments.  When the csplit command is corrected
to end with /END/ rather than /END/-1, it will create three files:
    xx00:
	junk0
	BEGIN
    xx01:
	data1
    xx02:
	END
	junk1
	...
	END
	junk
This is not at all what was wanted!  csplit has some rather unpleasant
restrictions, like not being able to create more than 99 (or is it 100?
the manual page is unclear) output files.