Peter Pang Peter Pang - 4 months ago 8
Linux Question

Split a single row of data (dat file) into multiple columns

I want to split a row of data into multiple columns like

a.dat
A B C D E F G H I J K L M N O P Q R S T U


into

b.dat
A B C D E F G
H I J K L M N
O P Q R S T U


I have tried using the pr function

pr -ts" " --columns 7 --across a.dat > b.dat


But it doesn't work, b.dat is the same as a.dat

Answer

I like fold for these thingies:

$ fold -w 14 file
A B C D E F G 
H I J K L M N 
O P Q R S T U

With -w you set the width you desire to have.

Although xargs is more useful if you want to split based on number of fields instead of characters:

$ xargs -n 7 < file
A B C D E F G
H I J K L M N
O P Q R S T U

Regarding your attempt in pr: I don't really know why it is not working, although from some examples I see it doesn't look like the tool for such job.