[ale] sed & newline removal

Lathe magius at wittsend.com
Fri Apr 19 09:48:00 EDT 2002


Danny is pretty right on.  Sed is a flat out pain when dealing with multiple
lines.  I went with Awk instead and got it working in short order.  Thanks
all.


----- Original Message -----
From: "Danny Cox" <DCox at SnapServer.com>
To: ale at ale.org
To: "Lathe" <magius at wittsend.com>
Cc: "ALE Mailing List" <ale at ale.org>
Sent: Thursday, April 18, 2002 1:12 PM
Subject: Re: [ale] sed & newline removal


> Scott,
>
> On Thu, 2002-04-18 at 11:49, Lathe wrote:
> > Hey all,
> > I'm working with sed to create a .csv file from a list of file in a
directory.  Each of those files contains configuration values like look like
this:
> >
> > name=value
> > name2=value2
> >
> > now I've gotten to a point where commas are placed at the end of the
line for each name/value pair by using the following:
> >
> > cat * | sed -e 's/$/,/3'
> >
> > I'd now like to take 3 lines and make them one line, such as:
> >
> > name1=value1, name2=value2, name3=value3
> >
> > name4=value4, name5=value5, name6=value6
> >
> > By what I've read, the following should work:
> >
> > cat * | sed -e ':!$N;s/\n/,/3'
>
> Sed has some esoteric (meaning that *I* don't use 'em much ;-), that
> deal with multiple lines.  For example sed G file will effectivly double
> space lines.  Of course, this is exactly opposite what you wish.
> However, using 'man sed', and looking at commands in uppercase (HGNP and
> perhaps others), you can do what you wish.
>
> On the other hand Perl may be a simpler way to accomplish the same
> thing.
>
> Okay, after futzing around a little, here's what I have:
>
> sed -n -e 'H' -e '1~3{
> x
> > s/\
> > //g
> > p
> > x
> > }'
>
> It's not perfect, as it prints out the first line by itself, but the
> rest are in groups of 3.  This is GNU sed.  The general idea is:
>
> H appends every line to the hold space.
> for every 1 step 3 line:
> x exchange pattern and hold space
> s substitute a literal newline for nothing
> p print it
> x exchange pattern and hold space again
>
> Sorry, it ain't pretty.  Mostly because you must represent '\n' as a '\
> ', it really makes things hard to read.  Grump.
>
> --
> kernel, n.: A part of an operating system that preserves the
> medieval traditions of sorcery and black art.
>
> Danny
>


---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list