column to row convert - script - help

Hi,

I have a file named col.txt

1.000
2.000
3.000
4.000
5.000
6.000
7.000
8.000

I should get this

1.000 5.000
2.000 6.000
3.000 7.000
4.000 8.000

Show the original file

> cat file68
1.000
2.000
3.000
4.000
5.000
6.000
7.000
8.000

Split the file after 4 lines, paste together, get rid of temporary files

> split -4 file68 f68s; paste f68saa f68sab; rm f68saa f68sab
1.000   5.000
2.000   6.000
3.000   7.000
4.000   8.000

Same as previous, except write to a file rather than the screen

> split -4 file68 f68s; paste f68saa f68sab > file68new; rm f68saa f68sab
> cat file68new
1.000   5.000
2.000   6.000
3.000   7.000
4.000   8.000

Thanks, works fine.

And what about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

I would like the same as above, split after 4 lines and should get 4 columns...

#  nawk '{t[NR%4]=t[NR%4]" "$0} END {for (i=1;i<=3;i++){print t};print t[0]} ' infile
      1      5      9     13
      2      6     10     14
      3      7     11     15
      4      8     12     16

more generic:

#  nawk -v m=4 '{t[NR%m]=t[NR%m]" "$0} END {for (i=1;i<=m-1;i++){print t};print t[0]} ' infile
      1      5      9     13
      2      6     10     14
      3      7     11     15
      4      8     12     16


#  nawk -v m=2 '{t[NR%m]=t[NR%m]" "$0} END {for (i=1;i<=m-1;i++){print t};print t[0]} ' infile
      1      3      5      7      9     11     13     15
      2      4      6      8     10     12     14     16

Excellent, thanks!

Edit: just realized the OP wants a different output, so please ignore.

Edit: see above.

perl:

undef $/;
open FH,"<file";
$str=<FH>;
@arr=split("\n",$str);
$t=($#arr+1)/4;
for($i=0;$i<=3;$i++){
	print $arr[$i];
	for($j=1;$j<=$t;$j++){
		print " ",$arr[$i+4*$j];
	}
	print "\n";
}
close FH;

Guys,
why difficult ways when u can simply use
pr -2 filename > outfile

Or even without external commands if you have zsh installed :slight_smile:

$ print -lC2 {1..16}      
1  9
2  10
3  11
4  12
5  13
6  14
7  15
8  16
$ print -lC4 {1..16}
1   5   9   13
2   6   10  14
3   7   11  15
4   8   12  16

Perhaps the default pr output could be modified a bit:

pr -i' ' -t2 input