How to convert a single column into several columns?

Hi kumaran_5555,

My OS is Sun Solaris Unix based. Can you convert your code to Unix please... It is giving me an error...

root@pinpe>nawk -v row=3 '{arr[NR%row]=arr[NR%row]" "$0} END{n=asort(arr);for(i=1;i<=n;i++){print arr}} ' test.txt
nawk: calling undefined function asort
 input record number 81, file test.txt
 source line number 1
root@pinpe>gawk -v row=3 '{arr[NR%row]=arr[NR%row]" "$0} END{n=asort(arr);for(i=1;i<=n;i++){print arr}} ' test.txt

CORRECT>awk -v row=3 '{arr[NR%row]=arr[NR%row]" "$0} END{n=asort(arr);for(i=1;i<=n;i++){print arr}} ' test.txt (y|n|e|a)?

Thanks in advance.

Br,
Pete

#!/usr/bin/perl -w

my $COLS = 3; # or shift 

my $maxlength = 0;
my @words = ();
while (<>) {
    s/\s+$//;
    $maxlength = length if length > $maxlength;
    push @words, $_;
}

$maxlength++;

my $rows = int @words/$COLS;
my @lasts = splice @words, ($COLS*$rows);

for (my $i = 0; $i < $rows; $i++) {
    for (my $j = 0; $j < $COLS; $j++) {
        my $word = $words[$i + $j*$rows];
        printf "%-${maxlength}s", $word;
    }
    print "\n";
}
if (@lasts) {
    printf "%-${maxlength}s", $_ for @lasts;
    print "\n";
}

Not very perlish although.

I couldn't make it using awk in solaris,

Here is bash script which will work any where, change the rows variable as you need.

#!/bin/bash

rows=3
j=0
i=0

while read line
do
        key=`expr $i % $rows`
        if [ $j -lt $key ]
        then
                j=$key
        fi
        arr[$key]=${arr[$key]}" "$line
        i=`expr $i + 1`
done  < test.txt

i=0
while [ $i -le $j ]
do    
    echo ${arr[$i]}
    i=`expr $i + 1`
done

an awk-one; I hope some one from this forum will give more elegant "awk" script.

 
awk '1' ORS=" " input_file > modifiled_file
 
echo "\n" >> modifiled_file
 
awk '{ for(i=1;i<=NF;i++) { for(j=i;j<=NF;j=j+5) {printf "%s ", $j ;$j=""};printf "\n";}}' modifiled_file | sed '/^[ ]*$/d' 

Hi getmmg,

YES! now its working. But please can you try one more time the input file below. This is my "actual" file. Hope you can spare more time on trying to run this again to give 3 columns in order. Thank you so much mate!!

input file:

382
527
10625
87868
18
5
1807367
8614
730
7025
375
837
3558
15530
5363
7807
368
681
12372
18789
13
254508
366
491
697
20194
18
5
1341408
4419
666
5618
362
837
2645
1272
5046
7690
332
139
11201
18717
13
9760
0
0
10
10
0
0
42629
96
1
247
0
0
3
8
58
0
0
3
24
23
0
0

output file:

382         366         0    
527         491         0    
10625       697         10   
87868       20194       10   
18          18          0    
5           5           0    
1807367     1341408     42629
8614        4419        96   
730         666         1    
7025        5618        247  
375         362         0    
837         837         0    
3558        2645        3    
15530       1272        8    
5363        5046        58   
7807        7690        0    
368         332         0    
681         139         3    
12372       11201       24   
18789       18717       23   
13          13          0    
254508      9760        0    

Br,
Pete

This worked for me:

 
 
 
awk '1' ORS=" " input_file  | awk 'BEGIN {$0=$0"\n"}1'|awk '{ for(i=1;i<=NF;i++) { for(j=i;j<=NF;j=j+NF/3) {printf "%s\t", $j ;$j=""};printf "\n";}}' |  | awk 'NF>1'
 

where 3-decides how many columns you want!!!

Problem happened when it was getting sorted. It sorts row 1,10...19,then 2,3 etc.,

I have added formatting for rows to fix that

Hope this code will your issue.

perl -0ane 'BEGIN{$cols=3;$k=0}END{foreach(@F){$j=sprintf("%03d",++$k);$hash{$j}.= "$_\t";$k=0 if($k == (($#F+1)/$cols))}; print "$hash{$_}\n" for sort keys %hash}' input

Cheers.

1 Like

It was rather easy in awk, i was little confuesed,

If you have rows as input,

 nawk -v rows=22 '{arr[(NR-1)%rows]=arr[(NR-1)%rows]"\t"$0} END{for(i=0;i<rows;i++){print arr}}' test.txt

if you have columns as input, 3 represents your number of columns

nawk -v rows=$(echo `wc -l test.txt|cut -d' ' -f1` / 3 |bc) '{arr[(NR-1)%rows]=arr[(NR-1)%rows]"\t"$0} END{for(i=0;i<rows;i++){print arr}}' test.txt

This one works on solaris with standard nawk

Output

        382     366     0
        527     491     0
        10625   697     10
        87868   20194   10
        18      18      0
        5       5       0
        1807367 1341408 42629
        8614    4419    96
        730     666     1
        7025    5618    247
        375     362     0
        837     837     0
        3558    2645    3
        15530   1272    8
        5363    5046    58
        7807    7690    0
        368     332     0
        681     139     3
        12372   11201   24
        18789   18717   23
        13      13      0
        254508  9760    0

Hi getmmg,

This is one works totally for me. Thank you so much mate!

Now for the final touch, after the desired output into 3 columns I want it to trim the trailing spaces from the output file. Please insert some more code on this perl script.

Thanks again in advance.

Br,
Pinpe

 
perl -0ane 'BEGIN{$cols=3;$k=0}END{foreach(@F){$j=sprintf("%03d",++$k);$hash{$j}.= "$_\t";$k=0 if($k == (($#F+1)/$cols))}; for (sort keys %hash){chop($hash{$_});print "$hash{$_}\n"}}' inp

1 Like

PERFECT!!! Thanks mate!! :b: :slight_smile:

Br,
Pinpe