paste each 10 lines of single column to several column

Hi,

I need to paste each 10 lines of single column to several columns.
Please, can anyone tell me how to write in awk?

Input File:

22
34
36
12
17
19
15
11
89
99
56
38
29
29
49
29
67
89
76
19
22
44
12
44
66
87
45
34
67
98

Desired Output:

22 56 22
34 38 44
36 29 12
12 29 44
17 49 66
19 29 87
15 67 45
11 89 34
89 76 67
99 19 98

Thanks

Try this:

awk '
{a[NR%10]=a[NR%10]?a[NR%10]" "$0:$0}
END{a[10]=a[0];for(i=1;i<11;i++){print a}}
' file

Regards

Hi Franklin52,

Your code is work!:b:

Thanks so much.....:slight_smile:

Hi, below should be ok for you. Can change below 3 to whatever, so that make them any number of columns.

#!/usr/bin/perl
use POSIX;
sub _toArr{
	my $file=shift;
	local $/="";
	open FH,"<$file";
	my $str=<FH>;
	my @arr=split("\n",$str);
	return \@arr;
}
sub splitArr{
	my ($ref,$num)=(@_);
	my @arr=@{$ref};
	my $loop=ceil(($#arr+1)/$num);
	#print $loop,"\n";
	for(my $i=0;$i<=$loop-1;$i++){
		for(my $j=0;$j<=$num-1;$j++){
			my $tmp=$i+$loop*$j;
			print $arr[$tmp]," ";
	}
	print "\n";
	}
}
my $ref=_toArr("a.txt");
splitArr($ref,3);
line=`cat a.txt | wc -l`
nawk -v l="$line" -v col="4" '{
	num=l/col
	if(l%col!=0)
		num=int(num)+1
	arr[NR%num]=arr[NR%num]?arr[NR%num]" "$0:$0
}
END{
	arr[num]=arr[0]
	for(i=1;i<=num;i++)
		print arr
}' a.txt

Thanks summer_cherry, for perl & nawk code :b: