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

murillo@sigi.Colorado.EDU (Rodrigo Murillo) (04/19/88)

Gentle readers:

A clarifiction to my previous posting.

I need an sed line (or some gory pipeline) to extract the data between
BEGIN and END.

I should have added the following:

	>I have a text file of this form:
	    [... junk ...]
	>   BEGIN
	>     1
	>     2
	>     3
	>   END
	    [... junk ...]
	>   BEGIN
	>     4
	>     5
	>     6
	>   END
	    [... junk ...]

Add to that the fact that the data between BEGIN...END is really not
plain sequential numbers; it is random text.  Sorry I was not more
clear.  If you have a SED line to deal with the above, I would love to 
see it.  Thanks.
-- 
_______________________________________________________________________________
 Rodrigo Murillo, University of Colorado - Boulder  (303) 761-0410 
 murillo@boulder.colorado.edu | ..{ncar|nbires}!boulder!murillo
 ( Machines have less problems.  I'd like to be a machine. -- Andy Warhol )

daveb@laidbak.UUCP (Dave Burton) (04/20/88)

In article <5490@sigi.Colorado.EDU> murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
|I need an sed line (or some gory pipeline) to extract the data between
|BEGIN and END.
|	>I have a text file of this form:
|	    [... junk ...]
|	>   BEGIN
|	>     1
|	>     2
|	>     3
|	>   END
|	    [... junk ...]
|	>   BEGIN
|	>     4
|	>     5
|	>     6
|	>   END
|	    [... junk ...]
|Add to that the fact that the data between BEGIN...END is really not
|plain sequential numbers; it is random text.  Sorry I was not more
|clear.  If you have a SED line to deal with the above, I would love to 
|see it.  Thanks.

Awk would be the tool of choice here. Your basic problem is that you
have state in your text file (in,out), and the state changes only on
subsequent lines. It is difficult (probably not impossible) to write
a sed script, though an awk script is trivial. Try:

awk '
/^BEGIN$/ { state = "in";  next }
/^END$/   { state = "out"; next }
{ if (state == "in") print $0 }
' < yourfile

-- 
--------------------"Well, it looked good when I wrote it"---------------------
 Verbal: Dave Burton                        Net: ...!ihnp4!laidbak!daveb
 V-MAIL: (312) 505-9100 x325            USSnail: 1901 N. Naper Blvd.
#include <disclaimer.h>                          Naperville, IL  60540

r_ellison@hpfcdq.HP.COM (Bob Ellison) (04/21/88)

How about

    sed '/BEGIN/,/END/p
    d' files...

in sh or ksh or

    sed '/BEGIN/,/END/p\
    d' files...

in csh?  These lines will include the BEGIN and END in the output, but will
strip all other lines.

I'd prefer to use awk:

    awk '/BEGIN/,/END/' files...


papillo the awkward
Bob Ellison

sham@arizona.edu (Shamim Pogner Mohamed) (04/21/88)

In article <5490@sigi.Colorado.EDU>, murillo@sigi.Colorado.EDU (Rodrigo Murillo) writes:

> I need an sed line (or some gory pipeline) to extract the data between
> BEGIN and END.

Here's a script that seems to work:  (to be run as: sed -n -e script)
-----------------------------------------------------------------------------
s/BEGIN//
t nums
b
: nums
n
s/END//
t
p
b nums
-----------------------------------------------------------------------------

I was going to e-mail but thought this solution is rather kludgy. Does 
someone have a nice elegant solution?

Yes, I know it can be done in awk etc.... 
-- 
Shamim Mohamed,  Dept. of Computer Science,
U of Arizona, Tucson AZ 85721  (602) 621-4891

{ihnp4,allegra,cmcl2...}!arizona!sham  |  sham@arizona.edu