Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a text file into columns

Tags:

linux

unix

awk

Let's assume I have scientific data, all numbers arranged in a single column but representing an intensities matrix of n (width) by m (height). The column of the input file has in total n * m rows. An input example may look like that:

1  
2  
3  
......  
30 

The new output should be such that I have n new columns with m rows. Sticking to my example with 30 fields input and n = 3, m = 10, I would need an output file like this (separator does not matter much, could be a blank, a tab etc.):

1   11  21  
2   12  22  
... ... ...
10  20  30 

I use gawk under Windows. Please note that there are no special FS, more real-world examples are like 60 * 60 or bigger.

like image 798
greta Avatar asked Mar 28 '13 16:03

greta


Video Answer


1 Answers

If you are not limited to awk but have GNU core-utils (cygwin, native, ..) then the simplest solution is to use pr:

pr -ts" " --columns 3 file
like image 120
Chris Seymour Avatar answered Oct 24 '22 18:10

Chris Seymour