[comp.unix.questions] Need script to pluck lines of text from a file

jfb200@cbnewsd.att.com (joseph.f.baugher) (07/01/90)

I am looking for a Shell script capable of doing the following job:

I have a file consisting of several dozen lines of text, as per example:

		text in line 1
		text in line 2
		....
		text in line n
		startmarker
		text in line n+1
		text in line n+2
		....
	        ...
		text in line n+m-1
		endmarker
		text in line n+m+1
		text in line n+m+2
		....
	        ....
where startmarker and endmarker are lines of text with characteristic
markers in them which identify them as unique and different from the others.
I want to be able to pipe the lines of text BETWEEN startmarker and
endmarker to another command for further processing.  Unfortunately, I do not
know ahead of time what the numbers n and m are.

I have been searching through the AWK textbook by Kernigan et. al. for some
hints on how to do this, but I find this book hard to understand.  IMHO, the
AWK language is appropriately named, and I come from Bell Labs, no less!

If anyone out there can give me pointers to a solution, I would be eternally
grateful.


Joe Baugher				**************************************
AT&T Bell Laboratories			*  "Of all the gin joints in all the *
200 Park Plaza				*  towns in all the world, she walks *
Naperville, Illinois 60566-7050		*  into mine."                       *
(708) 713 4548				**************************************
ihlpm!jfb
jfb200@cbnewsd.att.com
				  Who, me?  Speak for AT&T?  Surely you jest!	

rouben@math9.math.umbc.edu (Rouben Rostamian) (07/01/90)

In article <1990Jun30.180958.28569@cbnewsd.att.com> jfb200@cbnewsd.att.com (joseph.f.baugher) writes:
 >
 >I have a file consisting of several dozen lines of text, as per example:
 >
 >		text in line 1
 >		text in line 2
 >		....
 >		text in line n
 >		startmarker
 >		text in line n+1
 >		text in line n+2
 >		....
 >	        ...
 >		text in line n+m-1
 >		endmarker
 >		text in line n+m+1
 >		text in line n+m+2
 >		....
 >	        ....
 >where startmarker and endmarker are lines of text with characteristic
 >markers in them which identify them as unique and different from the others.
 >I want to be able to pipe the lines of text BETWEEN startmarker and
 >endmarker to another command for further processing. 

This should do:

awk < infile '/startmarker/,/endmarker/ { \
         if ($0 != "startmarker && $0 != "endmarker") \
              print }'

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

dan@scooter.rosemount.com (Dan Messinger) (07/04/90)

In article <1990Jun30.180958.28569@cbnewsd.att.com>,
jfb200@cbnewsd.att.com (joseph.f.baugher) writes:
|> 
|> I am looking for a Shell script capable of doing the following job:
|> 
|> I have a file consisting of several dozen lines of text, as per example:
|> 
|> 		text in line 1
|> 		text in line 2
|> 		....
|> 		text in line n
|> 		startmarker
|> 		text in line n+1
|> 		text in line n+2
|> 		....
|> 	        ...
|> 		text in line n+m-1
|> 		endmarker
|> 		text in line n+m+1
|> 		text in line n+m+2
|> 		....
|> 	        ....
|> where startmarker and endmarker are lines of text with characteristic
|> markers in them which identify them as unique and different from the others.
|> I want to be able to pipe the lines of text BETWEEN startmarker and
|> endmarker to another command for further processing.  Unfortunately,
I do not
|> know ahead of time what the numbers n and m are.
|> 

Sounds like a job for sed.

sed -n "/^startmarker/,/^endmarker/p"

Dan Messinger
dan@scooter.rosemount.com

dan@scooter.rosemount.com (Dan Messinger) (07/05/90)

In article <8957@rosevax.Rosemount.COM>, dan@scooter.rosemount.com (Dan
Messinger) writes:
|> In article <1990Jun30.180958.28569@cbnewsd.att.com>,
|> jfb200@cbnewsd.att.com (joseph.f.baugher) writes:
|> |> 
|> |> I am looking for a Shell script capable of doing the following job:
|> |> 
|> |> I have a file consisting of several dozen lines of text, as per example:
|> |> 
|> |> 		text in line 1
|> |> 		text in line 2
|> |> 		....
|> |> 		text in line n
|> |> 		startmarker
|> |> 		text in line n+1
|> |> 		text in line n+2
|> |> 		....
|> |> 	        ...
|> |> 		text in line n+m-1
|> |> 		endmarker
|> |> 		text in line n+m+1
|> |> 		text in line n+m+2
|> |> 		....
|> |> 	        ....
|> |> where startmarker and endmarker are lines of text with characteristic
|> |> markers in them which identify them as unique and different from
the others.
|> |> I want to be able to pipe the lines of text BETWEEN startmarker and
|> |> endmarker to another command for further processing.  Unfortunately,
|> I do not
|> |> know ahead of time what the numbers n and m are.
|> |> 
|> 
|> Sounds like a job for sed.
|> 
|> sed -n "/^startmarker/,/^endmarker/p"

I was informed of a problem with this sed command, in that it also prints the
start and end markers.  So I will try again.  sed doesn't support an offset
on an address pattern, but ex does.  I think this will do the trick.

ex - "+/^startmark/+1,/^endmark/-1 p|q" file

It is slightly limited in that the source text must be in a file, and not
piped into ex.  But the output can be piped to another command as Joseph
specified.

Dan Messinger
dan@scooter.rosemount.com

ted@isgtec.UUCP (Ted Richards) (07/10/90)

In article <8962@rosevax.Rosemount.COM> dan@scooter.rosemount.com (Dan Messinger) writes:
|> In article <8957@rosevax.Rosemount.COM>, dan@scooter.rosemount.com (Dan
|> Messinger) writes:
|> |> In article <1990Jun30.180958.28569@cbnewsd.att.com>,
|> |> jfb200@cbnewsd.att.com (joseph.f.baugher) writes:
|> |> |> 
|> |> |> I am looking for a Shell script capable of doing the following job:
|> |> |> 
|> |> |> I have a file consisting of several dozen lines of text, as per example:
|> |> |> 
|> |> |> 		text in line 1
|> |> |> 		text in line 2
|> |> |> 		....
|> |> |> 		text in line n
|> |> |> 		startmarker
|> |> |> 		text in line n+1
|> |> |> 		text in line n+2
|> |> |> 		....
|> |> |> 	        ...
|> |> |> 		text in line n+m-1
|> |> |> 		endmarker
|> |> |> 		text in line n+m+1
|> |> |> 		text in line n+m+2
|> |> |> 		....
|> |> |> 	        ....
|> |> |> where startmarker and endmarker are lines of text with characteristic
|> |> |> markers in them which identify them as unique and different from
|> the others.
|> |> |> I want to be able to pipe the lines of text BETWEEN startmarker and
|> |> |> endmarker to another command for further processing.  Unfortunately,
|> |> I do not
|> |> |> know ahead of time what the numbers n and m are.
|> |> |> 
|> |> 
|> |> Sounds like a job for sed.
|> |> 
|> |> sed -n "/^startmarker/,/^endmarker/p"
|> 
|> I was informed of a problem with this sed command, in that it also prints the
|> start and end markers.  So I will try again.

How about:

	 sed -e "1,/^startmarker/d" -e "/^endmarker/,$d"
-- 
Ted Richards          ...uunet!utai!lsuc!isgtec!ted         ted@isgtec.UUCP
ISG Technologies Inc.   3030 Orlando Dr. Mississauga  Ont.  Canada   L4V 1S8