"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


Reply via email to