[SLL] sed to delete blank line above and after a regex?

Ted Stern dodecatheon at gmail.com
Fri Mar 21 14:10:21 PDT 2008


On 21 Mar 2008 13:51:39 -0700, Jeremy C. Reed wrote:
>
> Can someone share a sed example of deleting a blank line above and after a 
> regex?
>
> (Or just delete one line above and one line after a regex?)
>
> file is like this
>
> %
>
> but I don't want blank lines
>
> %
>
> before and after each percent sign
>
> %
>
>
> Any ideas on how to do that with sed?
>
>   Jeremy C. Reed
>

Hi Jeremy,

Yes, you can do it on a single line.  What you want to do is enlarge
the search space a bit.

     sed -e '/^$/{N;N;s/^\n\(RE\)\n$/\1/}'

     /^$/   Search for blank line
     {      begin multiple operations
     N;     swallow another line
     N;     swallow another line

     s/^\n\(RE\)\n$/\1/     ^      = beginning of multi-line
                            \n     = first embedded newline
                            \(RE\) = keep track of the RE you're
                                     searching for
                            \n     = 2nd embedded newline
                            $      = end of the multi-line space
                            \1     = the RE you found ... put it back in.
     }  finish up

Does that make sense?
     
Ted
-- 
 dodecatheon at gmail dot com
 Frango ut patefaciam -- I break so that I may reveal


More information about the linux-list mailing list