MKF
MKF

Reputation: 45

How to split one long columns into multiple columns shell script

I have a sequence of value in one column that I would like to split in multiple columns. The number of rows is known only number of columns is changing.

Here my input :

,
0,
3.375,
1,
21,
BEAM
-7.1,
,
1,
-100,
0,
0,
,
0,
,
2,
3.7375,
1,
0,
,
0,
,
0,
9.375,
1,
29,
BEAM
-7.9,
,
1,
-100,
0,
0,
,
0,
,
2,
3.7375,
1,
0,
,
0,

Desired output where number of columns is 2:

,          ,
0,         0,
3.375,     9.375,  
1,         1,
21,        29,
BEAM       BEAM,
-7.1,      -7.9
,          ,
1,         1,
-100,      -100,
0,         0,
0,         0,
,          ,
0,         0,
,          ,
2,         2, 
3.7375,    3.7375,
1,         1,
0,         0,
,          ,
0,         0,

What I tried 1st :

NUMBER=$(cat HBS-A | cut -d"," -f1 | sed '/\--/d' | uniq | wc -l)
pr -ts" " --columns $NUMBER HBS-value | tr -s " " "," | sed 's/^,//' > HBS-table 

First, I determine how many sequence are in the file to get the number of columns. THen I use pr command, it works beautifully but when the number of column is limited to 72. Most of the time I have more than 100+ columns.

Then I tried this :

awk -v row=21 '{A[(NR-1)%row]=A[(NR-1)%row]$0" ";next}END{for(i in A)print A[i]}' HBS-value 

It will give the right format but all my data is completely scrambled and I don't understand why but when I try with a simple example such as:

A 
B
C
D

I would get :

A C
B D

Any suggestion please ?

EDIT-----

This is my output from the example above :

0,          0,
0,          0,
,           ,
0,          0,
0,          0,
,           ,
2,          2,
3.7375,     3.7375,
1,          1,
0,          0,
,           ,
,           ,
0,          0,
3.375,      9.375,
1,          1,
21,         29,
BEAM        BEAM
-7.1,      -7.9,
,           ,
1,          1,
-100,      -100,

BEAM is supposed to be row #6 but it goes to #17

Upvotes: 2

Views: 1078

Answers (2)

MKF
MKF

Reputation: 45

I finally manager to get rid of the limit by adding -w LIMIT, I will put 5000 just to be on the safe side.

So in my case that would be :

NUMBER=$(cat HBS-A | cut -d"," -f1 | sed '/\--/d' | uniq | wc -l)
pr -ts" " --columns $NUMBER HBS-value -w 5000 | tr -s " " "," | sed 's/^,//' > HBS-table 

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 140970

There is utility columns that is part of autogen package on my archlinux, that let's you just specify the count of columns with -c option:

columns --by-columns -w 1 -c $(( $(wc -l <file) / 21 )) -i file

Upvotes: 2

Related Questions