From owner-freebsd-questions@FreeBSD.ORG Thu Apr 26 10:55:00 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 542A816A401 for ; Thu, 26 Apr 2007 10:55:00 +0000 (UTC) (envelope-from jonathan@hst.org.za) Received: from sirian.hst.org.za (sirian.hst.org.za [209.203.2.130]) by mx1.freebsd.org (Postfix) with ESMTP id 1EDCC13C458 for ; Thu, 26 Apr 2007 10:54:49 +0000 (UTC) (envelope-from jonathan@hst.org.za) Received: from localhost (localhost.hst.org.za [127.0.0.1]) by sirian.hst.org.za (Postfix) with ESMTP id ECF3631D56C for ; Thu, 26 Apr 2007 12:49:58 +0200 (SAST) Received: from sirian.hst.org.za ([127.0.0.1]) by localhost (sirian.hst.org.za [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 61498-07 for ; Thu, 26 Apr 2007 12:49:58 +0200 (SAST) Received: from sysadmin.hst.org.za (sysadmin.int.dbn.hst.org.za [10.1.1.20]) by sirian.hst.org.za (Postfix) with ESMTP id 8464A31CB58 for ; Thu, 26 Apr 2007 12:49:58 +0200 (SAST) From: Jonathan McKeown Organization: Health Systems Trust To: freebsd-questions@freebsd.org Date: Thu, 26 Apr 2007 12:57:35 +0200 User-Agent: KMail/1.7.2 References: <1177557488.22129.16.camel@joe.realss.com> <1177570289.22129.39.camel@joe.realss.com> In-Reply-To: <1177570289.22129.39.camel@joe.realss.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200704261257.35584.jonathan@hst.org.za> X-Virus-Scanned: by amavisd-new at hst.org.za Subject: Re: [OT] simpliest way to process this data file X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Apr 2007 10:55:00 -0000 On Thursday 26 April 2007 08:51, Zhang Weiwu wrote: > On Thu, 2007-04-26 at 11:18 +0800, Zhang Weiwu wrote: [snip] > > I have a data file formatted like this each block of data consist of > > several lines; blocks are separated by empty lines like this > > > > This is a > > block > > of data > > with lines > > > > and another block > > of > > data > > > > The task is to move the last line of each block to the first line of the > > block. So the processed data look like this: > > > > with lines > > This is a > > block > > of data > > > > data > > and other block > > of [snip] I would use Perl, which can read in a paragraph at a time. Assuming you don't mind having multiple blank lines in the input replaced by a single blank line, this Perl commandline will do what you want: perl -l -00 -pe 's/(.*)^(.*)$/$2\n$1/ms' datafile Read perldoc perlrun for the flags, and perldoc perlre to understand the regex (in particular the m and s modifiers). Jonathan