some kind of binary sed(1) command

Robert Bonomi bonomi at mail.r-bonomi.com
Thu Feb 2 07:47:45 UTC 2012


> From owner-freebsd-questions at freebsd.org  Thu Feb  2 00:27:33 2012
> Date: Thu, 2 Feb 2012 07:22:36 +0100
> From: Matthias Apitz <guru at unixarea.de>
> To: freebsd-questions at freebsd.org
> Subject: some kind of binary sed(1) command
>
>
> Hello,
>
> I have a normal ASCII file wich has in some places two lines of '*',
> separated by an empty line, i.e.
>
> ....\n
> *********************\n
> \n
> *********************\n
> ....\n
>
> and I want to substitute the \n between the star lines by \f; the
> 'binary' sed command would just be 
>
>     s/*****\n\n*****/*****\n\f*****/
>
> which ofc does not work with sed(1) because it is line oriented;
> I could write something in perl, C, awk or whatever language, but I
> would prefer to do it with the normal commands... any ideas?

<lightbulb mode="on">
Use sed.  
</lightbulb>

*GRIN*





As follows:

  sed -e ' t l
           :l
           /^[*]+$/ {
           N
           !/\n$/ { 
           P
           s/^.*\n//
           t l
           }
           /\n$/ {
           N
           !/\n\n[*]*$/ {
           P
           s/^.*\n//
           P
           s/^.*\n//
           t l
           }
           /\n\n[*]*$/ s/\n\n/\n\f/
           }  
           }'  

Note: "how" this incantation works is left as an excercise for the student. 
      Careful perusal of the FM will provide enlightenment.

Caveat: this will convert:
   {stars}
   {blank}
   {stars}
   {blank}
   {stars}

to;
   {stars}
   {formfeed}
   {stars}
   {blank}
   {stars}

not:
   {stars}
   {formfeed}
   {stars}
   {formfeed}
   {stars}


More information about the freebsd-questions mailing list