Converting line output to column based output

Hi Guys,

I am trying to convert a file which has a row based output to a column based output. My original file looks like this:

1
2
3
4
5
6
1
2
3
 
 
 
1
2
3
4
 
 
1
2
3
4
5
6

I want output of:

1 1 1 1
2 2 2 2
3 3 3 3
4    4 4
5       5
6       6

I am using the following statement:

awk '{a[NR%6]=a[NR%6]" "$0} END{for (i=1;i<=6;i++) {print o= (i!=6)? a : a[0] } }' test.txt > test1.txt

For some reason, it is not seeing the blank lines.

What may be wrong with this statement?

Thanks.

What do you mean the awk script doesn't see the blank lines ? What does it output ?
It works fine with me (using nawk vs awk on Solaris).

$ cat test.txt
1
2
3
4
5
6
1
2
3
 
 
 
1
2
3
4
 
 
1
2
3
4
5
6
$ nawk '{a[NR%6]=a[NR%6]" "$0} END{for (i=1;i<=6;i++) {print o= (i!=6)? a : a[0] } }' test.txt > test1.txt 
$ cat test1.txt
 1 1 1 1
 2 2 2 2
 3 3 3 3
 4   4 4
 5     5
 6     6

Ok can you try the attached file and send me the output file?

The delimiter is 53. The third group of data does not have all 53 points.

When I try this file, the third group appears in the last column in the output file. It should appear as the third column.

Here is what I am using:

awk '{a[NR%53]=a[NR%53]" "$0} END{for (i=1;i<=53;i++) {print o= (i!=53)? a : a[0] } }' test.txt  >  test1.txt

Works fine with me. I got 53 lines with 10 columns minus some empty fields. Perhaps are those empty fields an issue ?

I got the attached file.

The empty fields is not the problem. The one column which has the empty fields is the last column. That's the problem. That last column should be the third one.

Can you upload the output file you get?

Thanks.

Ok I found out what was happening. The original text file had a (space) on every blank line that was messing up the order. I removed the spaces and it worked just fine.

Thanks.

my $ref;
while(<DATA>){
	chomp;
	my $tmp = $.%6;
	my $first=($tmp==0)?6:$tmp;
	my $second=($tmp==0)?int($./6):int($./6)+1;
	$ref->[$first]->[$second]=$_;
}
my @tmp=@{$ref};
for(my $i=1;$i<=6;$i++){
	my @t= @{$tmp[$i]};
	for(my $j=1;$j<=$#t+1;$j++){
		print $t[$j]," ";
	}
	print "\n";
}
__DATA__
1
2
3
4
5
6
1
2
3
 
 
 
1
2
3
4
 
 
1
2
3
4
5
6

Thanks, that worked too.

So the empty fields were the problem as I suspected. The awk script is correctly doing its job regardless of these blank fields. The issue was more on your output file parsing which was confused by field contents and fields delimiters being both the space character. A good approach would have been to use a visible delimiter like a comma, a tab or a semicolon.