NickSerao NickSerao - 4 months ago 13
Linux Question

Split one column by every into "n" columns with one character each

I have a file with one single column and 10 rows. Every column has the same number of characters (5). From this file I would like to get a file with 10 rows and 5 columns, where each column has 1 character only. I have no idea on how to do that in linux.. Any help? Would AWK do this?

The real data has many more rows (>4K) and characters (>500K) though. Here is a short version of the real data:

31313
30442
11020
12324
00140
34223
34221
43124
12211
04312


Desired output:

3 1 3 1 3
3 0 4 4 2
1 1 0 2 0
1 2 3 2 4
0 0 1 4 0
3 4 2 2 3
3 4 2 2 1
4 3 1 2 4
1 2 2 1 1
0 4 3 1 2


Thanks!

Answer

You can use this sed as well:

sed 's/./& /g; s/ $//' file

3 1 3 1 3
3 0 4 4 2
1 1 0 2 0
1 2 3 2 4
0 0 1 4 0
3 4 2 2 3
3 4 2 2 1
4 3 1 2 4
1 2 2 1 1
0 4 3 1 2