From mboxrd@z Thu Jan 1 00:00:00 1970 Date: Sun, 30 Nov 2008 00:29:49 -0800 From: Yard Ape To: 9fans@9fans.net Message-ID: <49324efd.rWhPmwznqAgtCKK7%yard-ape@telus.net> References: <20081024170237.68ED28DE7@okapi.maths.tcd.ie> <6520c845566013ada472281bf9c0da73@coraid.com> <2e4a50a0810241652r38d2aa1ft2b6fb9104d2988ae@mail.gmail.com> In-Reply-To: User-Agent: Heirloom mailx 12.3 7/15/07 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: Re: [9fans] non greedy regular expressions Topicbox-Message-UUID: 53a81c2e-ead4-11e9-9d60-3106f5b1d025 "Rudolf Sykora" wrote: > I have a file say like this > > ABC asassadfasdf asdfasdf asdfasdf CBA hhhhhhhhhhjjjjjjjjjjioioioi > sodifs > sdfsd > ABC > dasdfas aasdfa > njnjn CBA > > and I want to get > > ' asassadfasdf asdfasdf asdfasdf ' > 'dasdfas aasdfa' > 'njnjn' > > ...i.e. delimited with ABC from the left and CBA (or > e.g. EFG) from the right? Say I have a file with such a > structure and want to only get 'the interesting parts' > form it. > > ...using exclusively sam (and more, possibly without that > 's' command > One of the things I love about ed is how easy it is to loop through *ranges* which can be delimited by regexes, like so: ,g/pre-interesting/.+,/post-interesting/-\ s/this/that/g The same method can be used in sam: ,x/pre-interesting/+#0;/post-interesting/-#0{ x/this/c/that/ } In cases where the delimeters don't alternate perfectly (as in nesting), you can just start adding conditionals and/or subloops: ,x/pre-interesting/.+#0;/post-interesting/-#0{ v/pre-interesting/{ x/this/c/that/ } } You can operate on the inverse of the interesting range by employing a marker in the loop to maintain a "trailing" range throughout: 0k ,x/pre-interesting/.+#0;/post-interesting/-#0{ v/pre-interesting/{ ',.-#0{ x/this/c/that/ } .+#0k } } etc., etc. Here's a version that works on your example: 0k ,x/ABC/+#0;/CBA|EFG/{ ',.-#0d .+#0??d .+#0k } -Derek