Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 May 2009 15:30:00 +0000
From:      Paul Schmehl <pschmehl_lists@tx.rr.com>
To:        Manish Jain <invalid.pointer@gmail.com>, FreeBSD Mailing List <freebsd-questions@freebsd.org>
Cc:        Roland Smith <rsmith@xs4all.nl>
Subject:   Re: Need sed to do something which sounds simple
Message-ID:  <81CA7451D4C324A373C6451B@utd65257.utdallas.edu>
In-Reply-To: <4A1E8824.1020604@gmail.com>
References:  <4A1E8824.1020604@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
--On Thursday, May 28, 2009 07:48:36 -0500 Manish Jain 
<invalid.pointer@gmail.com> wrote:

>
>
> Hi,
>
> I need sed to do something which sounds simple, but I can't figure out
> the right command. All I need to do is insert a blank after a '}' at the
> end of a line if the next line begins immediately afterwards (i.e. with
> no blank line between).
>
> //abc.cpp :
> int myclass::fx(int * arg)
> {
>         if(! (isValid()))
>         {
>                 return -1;
>         }
>         return ptr->fx(arg);
> }
>
> //what-i-want.cpp :
> int myclass::fx(int * arg)
> {
>         if(! (isValid()))
>         {
>                 return -1;
>         }
>
>         return ptr->fx(arg);
> }
>
> The commands I have tried are :
>
> i)
> sed -e 's/\(}$\)\n\(^[[:space:]]*[[:alpha:]]\+\)/\1\n\n\2/' \
> <abc.cpp  >what-i-want.cpp
>
> ii)
> sed -e 's/\(}$\)\(^[[:space:]]*[[:alpha:]]\+\)/\1\n\2/' \
> <abc.cpp  >what-i-want.cpp
>
> but obviously neither works, which is why posting this message.
>
> Can anybody please tell me what the correct command would be like ?
>

Seems like this would work to add a space only to lines where the next line 
only has a new line :

sed  '  /\}$/ { N /}$\n\n/ { s/\}$\n/\} $\n/} } ' file

If the possibility exists that the new line might have spaces as well, you 
could do this:

sed  '  /\}$/ { N /}$\n\n/ { s/\}$\n[ ]?/\} $\n/} } '

Note: I haven't tested this, so it may require some modification.  Read this 
page on dealing with multiple lines in sed to gain further understanding - 
http://www.grymoire.com/Unix/Sed.html

-- 
Paul Schmehl, Senior Infosec Analyst
As if it wasn't already obvious, my opinions
are my own and not those of my employer.
*******************************************
Check the headers before clicking on Reply.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?81CA7451D4C324A373C6451B>