Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a file with 250k columns vertically?

Tags:

unix

awk

I need to split a file with 250k based on the size (preferable) or number of columns to several (~5) chunks. I am aware of split command for row-wise splitting, but don't know if there is any similar function to split column-wise. The number of columns in my file are not even, so the chunks cannot have equal number of columns.

Input:

AA BB CC DD EE FF GG HH II JJ KK LL MM
NN OO PP QQ RR SS TT UU VV WW XX YY ZZ

Desired output:

File1

AA BB CC DD        
NN OO PP QQ

File2

EE FF GG HH
RR SS TT UU

File3

II JJ KK LL MM
VV WW XX YY ZZ 
like image 729
user2162153 Avatar asked Feb 14 '23 18:02

user2162153


1 Answers

Using awk, you can adjust n to the number you expect.

awk '{for (i=1;i<=NF;i++)
         printf (i%n==0||i==NF)?$i RS:$i FS > "File" int((i-1)/n+1) ".txt"
      }' n=5 file
like image 69
BMW Avatar answered Feb 23 '23 13:02

BMW