Unix Shell - SED Join help needed

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2007 > SED Join help needed





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author SED Join help needed
Paul E. Lehmann

2007-02-19, 1:16 pm

I am trying to join two lines together. For
example, I would like to join lines that contain
"*" with the line that follows. The lines that
contain "*" are always be in the format shown in
the sample lines below.

For example, what I would like would be - the
first two lines would become

*20***38 36 33

and the next line would become

*21***38 36 33

etc

I have looked at "N" in my book and web pages but
but can't get it to work.

Any help would be appreciated.

Sample lines below:

*20***
38 36 33
*21***
38 36 33
*22***
36 33 30

Ed Morton

2007-02-19, 1:16 pm

Paul E. Lehmann wrote:
> I am trying to join two lines together. For
> example, I would like to join lines that contain
> "*" with the line that follows. The lines that
> contain "*" are always be in the format shown in
> the sample lines below.
>
> For example, what I would like would be - the
> first two lines would become
>
> *20***38 36 33
>
> and the next line would become
>
> *21***38 36 33
>
> etc
>
> I have looked at "N" in my book and web pages but
> but can't get it to work.
>
> Any help would be appreciated.
>
> Sample lines below:
>
> *20***
> 38 36 33
> *21***
> 38 36 33
> *22***
> 36 33 30
>


I'd use awk for anything involving more than one line:

awk '{ ORS = (/\*/ ? "" : "\n") }1' file

Ed.
Bill Marcum

2007-02-19, 7:15 pm

On Mon, 19 Feb 2007 09:18:56 -0500, Paul E. Lehmann
<someone@anywhere.com> wrote:
>
>
> I am trying to join two lines together. For
> example, I would like to join lines that contain
> "*" with the line that follows. The lines that
> contain "*" are always be in the format shown in
> the sample lines below.
>
> For example, what I would like would be - the
> first two lines would become
>
> *20***38 36 33
>
> and the next line would become
>
> *21***38 36 33
>
> I have looked at "N" in my book and web pages but
> but can't get it to work.
>

sed '/\*/{N;s/\n//;}'


--
In the long run, every program becomes rococco, and then rubble.
-- Alan Perlis
Paul E. Lehmann

2007-02-20, 1:26 am

Bill Marcum wrote:

> sed '/\*/{N;s/\n//;}'


Thanks Bill, that works.

When I put it in a sed file along with the other
lines I am using to operate on the file, I had to
change the enclosing "'" characters to curly
braces { }.

I am not sure why I had to do this to make it
work.

It was just trial and error after I found out your
solution worked by itself but not in a file along
with other editing lines.

If you could tell me briefly why this is so, I
would be even more grateful.

These neurons don't fire so fast at age 62 and
weaning myself from windoze. BTW - I don't think
windoze does this sort of neat stuff.

thanx

Michael Paoli

2007-02-20, 7:18 am

Bill Marcum wrote:
> On Mon, 19 Feb 2007 09:18:56 -0500, Paul E. Lehmann
> <someone@anywhere.com> wrote:
> sed '/\*/{N;s/\n//;}'


If one always wants to join lines containing "*" to the following
lines, even when multiple consecutive lines contain "*", a somewhat
different sed solution would be needed. E.g.:
sed -ne '

: t
/\*/!{
p
n
b t
}

h
: l
$p
n
/\*/!{
H
x
s/\n//g
p
n
b t
}

H
x
s/\n//g
${
p
q
}
x
b l
'

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com