[comp.unix.shell] easy for some

tchrist@convex.COM (Tom Christiansen) (05/10/91)

From the keyboard of lewis@tramp.Colorado.EDU (LEWIS WILLIAM M JR):
:In article <574@appserv.Eng.Sun.COM> lm@slovax.Eng.Sun.COM (Larry McVoy) writes:
:>matthew@gizmo.UK.Sun.COM (Matthew Buller - Sun EHQ - MIS) writes:
:>> problem: to extract text between start and end patterns in a file
:... more problem description
:>/bin/sh, usage shellscript start_pat stop_pat [files...]
:>
:... complex shell and perl programs to do
:
:	sed -n '/pattern1/,/pattern2/p' source_file > new_file

nope -- you included the endpoints.

i didn't see the original posting, so i don't know whether it's
possibly to have multiple sets of /pat1/,/pat2/ areas
in the file.  if so, the 1,/pat1/d /pat2,$d posting i just
saw won't work.

followups have been redirected.  this isn't particularly wizardly.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

marc@mercutio.ultra.com (Marc Kwiatkowski {Host Software-AIX}) (05/16/91)

In article <1991May9.185503.325@jpl-devvax.jpl.nasa.gov> lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) writes:
>   In article <1991May9.153351.1754@colorado.edu> lewis@tramp.Colorado.EDU (LEWIS WILLIAM M JR) writes:
>   : In article <574@appserv.Eng.Sun.COM> lm@slovax.Eng.Sun.COM (Larry McVoy) writes:
>   : >matthew@gizmo.UK.Sun.COM (Matthew Buller - Sun EHQ - MIS) writes:
>   : >> problem: to extract text between start and end patterns in a file
>   : ... more problem description
>   : >/bin/sh, usage shellscript start_pat stop_pat [files...]
>   : >
>   : ... complex shell and perl programs to do
>   : 
>   : 	sed -n '/pattern1/,/pattern2/p' source_file > new_file

>   Here's the perl equivalent of what you said:
>
>       perl -ne 'print if /pattern1/../pattern2/' source_file >new_file
>
>   When using Perl to do the other thing, I personally prefer a straightforward
>   approach:

Ahh.  In grand c.u.w tradition the lesser sin of a non-wizardly question
is met with the greater sin of slightly-correct to downright wrong
answers.  I know this isn't the right newsgroup, but the posters
question hasn't been answered.  The sed suggestions are all wet.
The perl one will work and in terms of execution and readability
is probably the best, but the original poster stated that he 
preferred an answer for /bin/sh.  I am surprised noone suggested 
something like the following answer:

	cat foo | sed -n '
	:lbl00
	    /pattern00/ {
	:lbl01
		n
		/pattern01/ {
			b lbl00
		}
		p
		b lbl01
	}'

The above will filter multiple instances of /pattern00/..../pattern01/.
If only one is desired, replace 'b lbl00' with 'q'.

Note follow-up.  sed, a utility more sinned against than sinning.
--
	------------------------------------------------------------------
	Marc P. Kwiatkowski			Ultra Network Technologies
	Internet: marc@ultra.com		101 Daggett Drive
	uucp: ...!ames!ultra!marc		San Jose, CA 95134 USA
	telephone: 408 922 0100 x249

	Ignore the following signature.
-- 
	------------------------------------------------------------------
	Marc P. Kwiatkowski			Ultra Network Technologies
	Internet: marc@ultra.com		101 Daggett Drive
	uucp: ...!ames!ultra!marc		San Jose, CA 95134 USA