Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!convex!usenet From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.wizards Subject: Re: easy for some Message-ID: <1991May09.033951.4383@convex.com> Date: 9 May 91 03:39:51 GMT References: <6686@male.EBay.Sun.COM> <574@appserv.Eng.Sun.COM> Sender: usenet@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 66 Nntp-Posting-Host: pixel.convex.com From the keyboard of lm@slovax.Eng.Sun.COM (Larry McVoy): :matthew@gizmo.UK.Sun.COM (Matthew Buller - Sun EHQ - MIS) writes: :> problem: to extract text between start and end patterns in a file :> eg:- :> :> file: :> :> pattern1--- :> :> stuff :> stuff :> stuff :> :> pattern2--- : :/bin/sh, usage shellscript start_pat stop_pat [files...] ug. A shell solution is obscene. :-) I don't know how to do it in sed. An awk solution would have made certain others happy, but wouldn't have been so nifty. > /bin/perl, same usage (see the notes on the ".." operator, cool thingy). But since we do happen to be on the perl topic... > $START = shift; > $STOP = shift; > while (<>) { > if (/^$START$/../^$STOP/) { > next if /^$START$/; # skip starting pattern > last if /^$STOP/; # done if last; > print; > } > } The following code should be faster because it's got fewer regexp compiles. The /o is to tell perl to compile the pattern only one. It also uses the fact that .. returns the sequence number, and that the last in the sequence has an E0 appended to it, for example making 144 be seen as 144E0, which is the same numerically, but you can do string or pattern operations on it. $START = shift; $STOP = shift; while (<>) { if ( $which = /^$START$/o .. /^$STOP$/o ) { next if $which == 1; last if $which =~ /E/; print; } } or maybe instead of the next/last pair of lines, just next if $which =~ /^1$|E/; if they want all instances in the stream extracted. --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."