0

I have a datafile, which looks like:

1 4.2
1 5.6
1 3.3
.
.
1 5.3
2 1.4
2 3.2
2 7.2
.
.
.
2 6.6
.
.
.
100 4.1
100 5.8
100 8.8
.
.
.
100 7.7

There are 100 lines for each different value in the first column, i.e. I have 10000 lines with two columns. I would need to rearrange the elements in such a way to get:

1 4.2 5.6 3.3 ... 5.3
2 1.4 3.2 7.2 ... 6.6
.
.
.
100 4.1 5.8 8.8 ... 7.7

That is, I need to get a 100x101 array. I'm thinking about using awk, but I am stuck. Any ideas?

Igor

  • Thats one of those moments where I think: yep, maybe some super-guru can write an undecipherable awksed expression for that ... but ... why? Why dont you take your favorite scripting language and write some real program for that? It would surprise me if you would need more than 20 lines of python to do that. **Readable** and maintainable python that would be. Something where somebody else can come back in 5 months from now to make some updates without helplessly starring at some awkmonsterexpression thing for half a day. And using R, probably a less-than-5-liner – GhostCat Oct 14 '16 at 11:04
  • If I had time to gain enough knowledge in scripting, I would do it myself, and would not ask the question here. I really appreciate the help I get here from the community, but I do not appreciate people who do not appreciate other people's time. I'm not that much in coding, but I suppose you would also not like if I answered your question with: "See there... that's a library. There is a bunch of books there about this topic, and when you read all those books, you will be able do answer your question yourself." – user5694985 Oct 14 '16 at 11:47
  • No idea whom you are complaining about, but just for the record: I didnt answer, I *commented*. For a reason. And in case you complain about the close action ... that duplicated question contains the thing you are looking for. And finally: of course it is also convenient that other people sometimes take their time to fully solve your problem; but you should also understand that this is *not* the meaning of SO. This is not a "we code for you service" in the first place. – GhostCat Oct 14 '16 at 11:50

1 Answers1

1

awk can help:

awk '{a[$1]=a[$1] " " $2} END{for(i in a) print i a[i]}' file

The array a is filled with the second parameter of each line and is indexed by the first parameter. When the file is read, awk outputs the content of the array.

oliv
  • 12,690
  • 25
  • 45