4

I wish to sort the TSV file below (called min_ex) by the first column lexicographically and by the second column numerically.

A X, N    2.2
A, N    5.7
A, A    5.8
A, N    2.1
A, T    0.2
B G, M    2.3
B, L    0.1
B, I    0.2
B, M    9.3
B, C    9.9

I tried to do it with sort -k1,2 -n min_ex. but it doesn't work as it results in:

A, A    5.8
A, N    2.1
A, N    5.7
A, T    0.2
A X, N    2.2
B, C    9.9
B G, M    2.3
B, I    0.2
B, L    0.1
B, M    9.3

I also think am also pretty sure (through experimentation) that sort is taking any blank space as the delimiter, but I don't see an option to set the separators.

I'd like to have solutions using either pure AWK or no-sed at all (preferably both, separately), and I'd like to remain as POSIX compliant as possible.

6
  • 1
    Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly... – Stephen Kitt Jan 7 '19 at 10:09
  • @StephenKitt Take for instance the first key, the first element is A. Then, in this group, I'm getting 5.8>2.1>5.7. – user330157 Jan 7 '19 at 10:18
  • Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.) – Stephen Kitt Jan 7 '19 at 10:22
  • It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt – user330157 Jan 7 '19 at 10:24
  • @StephenKitt I asked the wrong question. Thank you. – user330157 Jan 7 '19 at 10:31
4
sort -t$'\t' -k1,1 -k2,2n

does the trick, and it’s POSIX-compliant apart from the $'\t' part. -t specifies the field delimiter (instead of blank-to-non-blank transitions, which is the default); the n suffix can be applied to single field definitions.

5
  • Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question? – user330157 Jan 7 '19 at 10:05
  • @Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)... – Stephen Kitt Jan 7 '19 at 10:08
  • Thank you. Any idea why sort -t$'\t' -nk2 min_ex does not sort by the numerical column? – user330157 Jan 7 '19 at 10:32
  • @user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is. – mosvy Jan 7 '19 at 10:35
  • @user330157 also -nk2 is not the same as -kn2. – mosvy Jan 7 '19 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.