How to join two lines into one

How to join two lines into one

am 19.10.2007 19:00:55 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 join two lines into one

am 19.10.2007 21:15:05 von Tiago Peczenyj

My 2 cents using AWK

$ cat data
A
B
A
B
A
B

$ awk -v N=2 '{ A = A " " $0 } NR % N == 0{ print A ; A="" }' data
A B
A B
A B

$ cat data2
A
B
c
A
B
c
A
B
c

$ awk -v N=3 '{ A = A " " $0 } NR % N == 0{ print A ; A="" }' data2
A B c
A B c
A B c

Best Regards

Tiago

On Oct 19, 3:00 pm, cdmonl...@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

Re: How to join two lines into one

am 19.10.2007 21:19:08 von james19390

On Oct 19, 1:00 pm, cdmonl...@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
>
awk '{total=total" "$0}NR%2==0{print total; total=""}' 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, such as the following:
>
> A
> B
> C
> A
> B
> C
>
> to be converted to the following:
>
> A B C
> A B C

awk '{total=total" "$0}NR%3==0{print total; total=""}' file

>
> 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 join two lines into one

am 19.10.2007 21:26:37 von Tiago Peczenyj

Or based on first element

$ awk 'NR == 1{ FIRST = $0 }
NR > 1 && FIRST ~ $0 { print " " }
{ printf "%s ", $0 }
END{ print " "}' data
A B
A B
$ awk 'NR == 1{ FIRST = $0 }
NR > 1 && FIRST ~ $0 { print " " }
{ printf "%s ", $0 }
END{ print " "}' data2
A B c
A B c
A B c



On Oct 19, 5:15 pm, Tiago Peczenyj wrote:
> On Oct 19, 3:00 pm, cdmonl...@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

Re: How to join two lines into one

am 20.10.2007 02:13:38 von wayne

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.

The simplest way is to use the 'paste' command:

$ for i in `seq 10`; do printf 'A\nB\n'; done >AB
$ cat AB
A
B
A
B
A
B
A
B
A
B
A
B
A
B
A
B
A
B
A
B
$ paste - - A B
A B
A B
A B
A B
A B
A B
A B
A B
A B

-Wayne

Re: How to join two lines into one

am 20.10.2007 06:40:29 von Richard James

Wayne wrote:

> cdmonline@mac.com wrote:
>> What's an easy way to merge multiple rows into single rows? For

>> 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 ;-)
> The simplest way is to use the 'paste' command:
> $ for i in `seq 10`; do printf 'A\nB\n'; done >AB
> $ cat AB
> A

> B
> $ paste - - > A B

> A B

how to do that for every three lines?
$ paste - - - and so on adding a - for every extra line
to use spaces instead of tabs
$ paste -d" " - - - - - - - - A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H
A B C D E F G H

Richard James

Re: How to join two lines into one

am 20.10.2007 14:08:49 von gazelle

In article <1192804522.299217.48850@q3g2000prf.googlegroups.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:

This is the canonical way to do it in AWK.

#!gawk
ORS = NR % 3 ? " " : "\n"

Re: How to join two lines into one

am 20.10.2007 21:15:43 von William Park

Richard James wrote:
> > $ paste - -
> $ paste - - -
> $ paste -d" " - - - - - - - -
Also, OP may want to try '-s' option of 'paste', eg.
paste -s -d ' \n'

--
William Park , Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/

Re: How to join two lines into one

am 21.10.2007 04:25:39 von wyhang

I believe you're trying to reinvent a wheel as it is there for you...

try xargs pls...

here's an example:

F:\TOOLs\UnixUtil>cat a
A
B
C
A
B
c

F:\TOOLs\UnixUtil>cat a | xargs -n 2
A B
C A
B c

F:\TOOLs\UnixUtil>
F:\TOOLs\UnixUtil>cat a | xargs -n 3
A B C
A B c

On Oct 19, 12:00 pm, cdmonl...@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

Re: How to join two lines into one

am 21.10.2007 04:31:19 von wyhang

echo yourfile | xargs -n N
N is the record number you want to put in a line...


--why my reply don't appear? or a little delay?

On Oct 19, 12:00 pm, cdmonl...@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

Re: How to join two lines into one

am 21.10.2007 13:08:16 von Stephane CHAZELAS

2007-10-20, 19:31(-07), wyhang@gmail.com:
> echo yourfile | xargs -n N
> N is the record number you want to put in a line...

There are several problems with that. First, I believe you meant
< yourfile xargs -n N

Then, it should be noted that xargs works word-wise, not
line-wise and the definition of a word will vary depending on
the implementation of xargs.

printf 'a\ b c d\ne\n' | xargs -n 2
will give
a b c
d e

instead of
a\ b c d e
as would have
printf 'a\ b c d\ne\n' | paste -sd ' \n' -

Note that xargs above will call echo for every N lines. echo is
one of the least portable commands on Unix. The above would work
if you can guarantee that none of the lines start with "-" or
contain any backslash character.


> --why my reply don't appear? or a little delay?

You seem to be using a web gateway interface to usenet: google
groups. I suggest you have a look at their support site. You
could also use a real newsreader and your local newsserver
instead.

Also, please note that top-posting should be avoided on usenet
(and anywhere else, btw).

--
Stéphane

Re: How to join two lines into one

am 21.10.2007 21:12:09 von wyhang

On Oct 21, 6:08 am, Stephane CHAZELAS wrote:
> 2007-10-20, 19:31(-07), wyh...@gmail.com:
>
> > echo yourfile | xargs -n N
> > N is the record number you want to put in a line...
>
> There are several problems with that. First, I believe you meant
> < yourfile xargs -n N
>
> Then, it should be noted that xargs works word-wise, not
> line-wise and the definition of a word will vary depending on
> the implementation of xargs.
>
> printf 'a\ b c d\ne\n' | xargs -n 2
> will give
> a b c
> d e
>
> instead of
> a\ b c d e
> as would have
> printf 'a\ b c d\ne\n' | paste -sd ' \n' -
>
> Note that xargs above will call echo for every N lines. echo is
> one of the least portable commands on Unix. The above would work
> if you can guarantee that none of the lines start with "-" or
> contain any backslash character.
>
> > --why my reply don't appear? or a little delay?
>
> You seem to be using a web gateway interface to usenet: google
> groups. I suggest you have a look at their support site. You
> could also use a real newsreader and your local newsserver
> instead.
>
> Also, please note that top-posting should be avoided on usenet
> (and anywhere else, btw).
>
> --
> St=E9phane

oh,yes,I use google groups. thank you for the suggestion:) I just
noticed that every other post is not top-posting. sorry to all for
this...

have a nice Sunday afternoon:)