2

I am trying to write a script to change the following set of numbers

2.659980
3.256998
4.589778
2.120150

2.223365
2.325566
2.121112
3.020111

4.065112
0.221544
1.236665
1.395958

to the following form (essentially making a matrix out of a list of numbers which are separated by an empty line)

2.659980    2.223365    4.065112
3.256998    2.325566    0.221544
4.589778    2.121112    1.236665
2.120150    3.020111    1.395958

Can somebody help how to achieve this.

2
  • 1
    csplit and paste should do it, I would think. My first thought was column but that doesn't quite work.
    – Wildcard
    Dec 12, 2016 at 3:51
  • If you know the number of columns, then maybe something as simple as pr -t -3 file? Dec 12, 2016 at 3:54

2 Answers 2

4

With *BSD's rs(1), assuming the input file is well-formed:

rs -C -t $( awk '/^$/ { print NR-1; exit }' file ) <file
2
  • It works. If you could explain how it works then it will be more useful.
    – ankit7540
    Dec 12, 2016 at 6:45
  • 1
    @ankit7540 The awk command prints the number of lines till the first blank line. That's pretty much all there is to it, the rs(1) options are described in the manual. Dec 12, 2016 at 6:48
1

A pure awk approach

awk '{a[NR%5]=a[NR%5] $0 " "}END{printf "%s\n%s\n%s\n%s\n",a[1],a[2],a[3],a[4]}' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .