Unix Shell - Convert multiple columns to multiple lines

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Convert multiple columns to multiple lines





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 Convert multiple columns to multiple lines
Dave

2007-12-07, 1:29 pm

I have a program which outputs about 100 columns of data. The first 6
are shown:


UTC 10:53:40 power 30 elevation 23

I want to convert that so it outputs

UCT 10:53:40
power 30
elevation 23
...

Any suggestions?



Icarus Sparry

2007-12-07, 1:29 pm

On Fri, 07 Dec 2007 17:50:19 +0000, Dave wrote:

> I have a program which outputs about 100 columns of data. The first 6
> are shown:
>
>
> UTC 10:53:40 power 30 elevation 23
>
> I want to convert that so it outputs
>
> UCT 10:53:40
> power 30
> elevation 23
> ..
>
> Any suggestions?


Well, if you want to split them pairwise, you could use

awk '{for(i=1;i<NF;i+=2) print $i,$(i+1); }'

If you need more, e.g. you have an odd number of columns, or pairwise is
not exactly what you want then you will need to give us more information.
Radoulov, Dimitre

2007-12-07, 1:29 pm


"Dave" wrote ...
>I have a program which outputs about 100 columns of data. The first 6 are
>shown:
>
>
> UTC 10:53:40 power 30 elevation 23
>
> I want to convert that so it outputs
>
> UCT 10:53:40
> power 30
> elevation 23
> ..


With GNU Awk:

awk 'ORS=NR%2?FS:"\n"' RS="[ \n]" filename


Dimitre


Janis Papanagnou

2007-12-07, 7:23 pm

Dave wrote:
> I have a program which outputs about 100 columns of data. The first 6
> are shown:
>
>
> UTC 10:53:40 power 30 elevation 23
>
> I want to convert that so it outputs
>
> UCT 10:53:40
> power 30
> elevation 23
> ..
>
> Any suggestions?



printf "%s %s\n" $( program_that_outputs_data )


Janis
Dave

2007-12-08, 1:45 am

Icarus Sparry wrote:
> On Fri, 07 Dec 2007 17:50:19 +0000, Dave wrote:
>
>
> Well, if you want to split them pairwise, you could use
>
> awk '{for(i=1;i<NF;i+=2) print $i,$(i+1); }'
>
> If you need more, e.g. you have an odd number of columns, or pairwise is
> not exactly what you want then you will need to give us more information.


Thank you. That worked fine.
Cyrus Kriticos

2007-12-08, 7:42 pm

Dave wrote:
> I have a program which outputs about 100 columns of data. The first 6
> are shown:
>
>
> UTC 10:53:40 power 30 elevation 23
>
> I want to convert that so it outputs
>
> UCT 10:53:40
> power 30
> elevation 23
> ..


name_of_your_program | xargs -n 2 echo

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos

2007-12-08, 7:42 pm

Cyrus Kriticos wrote:
> Dave wrote:
>
> name_of_your_program | xargs -n 2 echo


or

name_of_your_program | xargs -n 2

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com