Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from a column, take each 3 values and put them in a row

Tags:

bash

shell

I’d like to accomplish this with a shell script. Here’s what my file looks like:

1
2
3
4
5
6
7
8
9

I want to convert it to be:

1 2 3
4 5 6
7 8 9

It's bigger than that, so I need something generic that works for a long file.

Thanks!

like image 347
Arabisch Avatar asked Oct 29 '25 01:10

Arabisch


2 Answers

Here is one way:

awk 'NR%3!=0{printf "%s ",$1;next}1' inputFile
like image 134
jaypal singh Avatar answered Oct 31 '25 04:10

jaypal singh


You can do that with paste, for example:

seq 9 | paste - - - -d ' '

The 3 dashes are to have 3 columns. You add or remove dashes to have more or fewer columns.

The -d ' ' is to separate the columns by space, because the default is tab.

like image 27
janos Avatar answered Oct 31 '25 04:10

janos