How to merge multiple lines into one - repeatedly

How to merge multiple lines into one - repeatedly

am 19.10.2007 16:38:41 von cdmonline

What's an easy way to merge multiple rows into single rows? For
example, if I have the following:

A
B
A
B
A
B

How can I convert this into:

A B
A B
A B

I want to be able to merge various numbers of rows into single. In the
above example, it's 2 rows being merged into 1 but what if I need to
merge 3 into 1, such as the following:

A
B
C
A
B
C

to be converted to the following:

A B C
A B C

Ideally, I want to be able to do this from standar shell commands
(awk, sed, etc.) as opposed to perl (although I welcome perl solutions
also ;-)

I want to be able to perform this action regardless of the text that
is contained within each line. In other words, I don't necessarily
have the luxury of knowing the contents of each line in the input.
Also, the contents of each line may differ througout the input.

- CDM

Re: How to merge multiple lines into one - repeatedly

am 19.10.2007 16:44:42 von Ed Morton

cdmonline@mac.com wrote:
> What's an easy way to merge multiple rows into single rows? For
> example, if I have the following:
>
> A
> B
> A
> B
> A
> B
>
> How can I convert this into:
>
> A B
> A B
> A B
>
> I want to be able to merge various numbers of rows into single. In the
> above example, it's 2 rows being merged into 1 but what if I need to
> merge 3 into 1, such as the following:
>
> A
> B
> C
> A
> B
> C
>
> to be converted to the following:
>
> A B C
> A B C
>
> Ideally, I want to be able to do this from standar shell commands
> (awk, sed, etc.) as opposed to perl (although I welcome perl solutions
> also ;-)
>
> I want to be able to perform this action regardless of the text that
> is contained within each line. In other words, I don't necessarily
> have the luxury of knowing the contents of each line in the input.
> Also, the contents of each line may differ througout the input.
>
> - CDM
>

awk 'ORS=NR%2?FS:RS' file

Just change the "2" to a "3" or whatever other number of lines you want
to merge.

Ed.

Re: How to merge multiple lines into one - repeatedly

am 19.10.2007 17:42:47 von Stephane CHAZELAS

2007-10-19, 07:38(-07), cdmonline@mac.com:
> What's an easy way to merge multiple rows into single rows? For
> example, if I have the following:
>
> A
> B
> A
> B
> A
> B
>
> How can I convert this into:
>
> A B
> A B
> A B

paste -sd ' \n' file

> I want to be able to merge various numbers of rows into single. In the
> above example, it's 2 rows being merged into 1 but what if I need to
> merge 3 into 1
[...]

paste -sd ' \n' file

--
Stéphane

Re: How to merge multiple lines into one - repeatedly

am 20.10.2007 14:56:30 von gazelle

In article ,
Ed Morton wrote:
....
>awk 'ORS=NR%2?FS:RS' file
>
>Just change the "2" to a "3" or whatever other number of lines you want
>to merge.

I like that. Using FS & RS is a nice variation on the theme.