2
votes

I have a text file containing a single column of data as below :

1
2
3
4
5
6

I want to split this into multiple columns using awk or sed command as follows:

1 2 3
4 5 6

How can I do this? Thanks in advance

4

4 Answers

6
votes

here is another approach

paste -d' ' - - - < file

with awk

awk 'ORS=NR%3?FS:RS' file

also

pr -3ats' ' file
4
votes

This should work because echo is a default command for xargs

xargs -n 3 file
4
votes

Using rs - reshape a data array:

$ cat file | rs -C' ' 2 3
1 2 3 
4 5 6 
  • rs reads the standard input
  • -C output column separator
  • 2 3 rows and columns

Use -t to transpose the input:

$ cat file | rs -C' ' -t 2 3
1 3 5 
2 4 6 
2
votes

With sed:

sed 'N; N; s/\n/ /g' file