Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Apr 2007 12:57:35 +0200
From:      Jonathan McKeown <jonathan@hst.org.za>
To:        freebsd-questions@freebsd.org
Subject:   Re: [OT] simpliest way to process this data file
Message-ID:  <200704261257.35584.jonathan@hst.org.za>
In-Reply-To: <1177570289.22129.39.camel@joe.realss.com>
References:  <1177557488.22129.16.camel@joe.realss.com> <1177570289.22129.39.camel@joe.realss.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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



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