From owner-freebsd-questions@FreeBSD.ORG Thu May 28 15:30:01 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6295D106564A for ; Thu, 28 May 2009 15:30:01 +0000 (UTC) (envelope-from prvs=3922c325c=pschmehl_lists@tx.rr.com) Received: from ip-relay-001.utdallas.edu (ip-relay-001.utdallas.edu [129.110.20.111]) by mx1.freebsd.org (Postfix) with ESMTP id 30DED8FC0C for ; Thu, 28 May 2009 15:30:00 +0000 (UTC) (envelope-from prvs=3922c325c=pschmehl_lists@tx.rr.com) X-Group: RELAYLIST X-IronPort-AV: E=Sophos;i="4.41,265,1241413200"; d="scan'208";a="12642736" Received: from smtp3.utdallas.edu ([129.110.20.110]) by ip-relay-001.utdallas.edu with ESMTP; 28 May 2009 10:30:00 -0500 Received: from utd65257.utdallas.edu (utd65257.utdallas.edu [129.110.3.28]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp3.utdallas.edu (Postfix) with ESMTPSA id 412B94EF4A; Thu, 28 May 2009 10:30:00 -0500 (CDT) Date: Thu, 28 May 2009 15:30:00 +0000 From: Paul Schmehl To: Manish Jain , FreeBSD Mailing List Message-ID: <81CA7451D4C324A373C6451B@utd65257.utdallas.edu> In-Reply-To: <4A1E8824.1020604@gmail.com> References: <4A1E8824.1020604@gmail.com> X-Mailer: Mulberry/4.0.6 (Linux/x86) X-Munged-Reply-To: Figure it out MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Cc: Roland Smith Subject: Re: Need sed to do something which sounds simple X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Paul Schmehl List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 May 2009 15:30:01 -0000 --On Thursday, May 28, 2009 07:48:36 -0500 Manish Jain 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/' \ > what-i-want.cpp > > ii) > sed -e 's/\(}$\)\(^[[:space:]]*[[:alpha:]]\+\)/\1\n\2/' \ > 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.