1

I have a file containing three TAB-separated columns. The 1st column is a number, the second is a sequence of 8 characters followed by 1-3 digits, the 3rd is the same as the 2nd column. Here's a minimum reproducible example:

1       abceefgh10      abceefgh22
1       abceefgh10      abceefgh9
1       abceefgh11      abceefgh10
1       abceefgh13      abceefgh11
1       abceefgh14      abceefgh13
1       abceefgh15      abceefgh14
1       abceefgh17      abceefgh16
-1      abceefgh18      abceefgh17
1       abceefgh19      abceefgh18
-1      abceefgh1       abceefgh2
-1      abceefgh20      abceefgh12
1       abceefgh21      abceefgh19
1       abceefgh22      abceefgh20
-1      abceefgh23      abceefgh21
1       abceefgh24      abceefgh24
1       abceefgh2       abceefgh1
1       abceefgh3       abceefgh3
1       abceefgh5       abceefgh5
1       abceefgh6       abceefgh25
1       abceefgh6       abceefgh6
1       abceefgh7       abceefgh7
-1      abceefgh8       abceefgh3
1       abceefgh9       abceefgh8

This example is what I get when I try to sort the columns with sort -gk2.9.

To the best of my knowledge I should expect to see the second column sorted from 1 to 24, and with increasing numerical value (i.e. 1,2,3,4,... and not 1,10,2,20,..., which would result if using -n).

If I cut the 2nd column and sort it with the same command (cut -f 2 ${file} | sort -gk1.9), I actually get the sorting that I want. Am I getting something obvious wrong?

3
  • 2
    what about sort -t $'\t' -V -k2, from this answer general numeric sort is for number with exponent Commented Jul 12, 2019 at 12:02
  • Thank you, it worked. As a side-note, it worked also without using the natural version sorting (-V), as long as I specified the field separator using $'\t'. Commented Jul 12, 2019 at 12:17
  • 2
    more exactly should be sort -t $'\t' -V -k2,2 to sort only over second column otherwise sorts also over the next columns Commented Jul 12, 2019 at 12:46

1 Answer 1

3

Using --debug option you can see column selection does not work as expected:

1>abceefgh10>abceefgh9  
         ^ no match for key 

specifying separator in accordance with Nahuel's comment works (sort -t $'\t' --debug -gk2.9):

1>abceefgh10>abceefgh9     
          __         
Sign up to request clarification or add additional context in comments.

2 Comments

looking at the manual : KEYDEF ... If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace.
Right. Deleted "poor documentation" paragraph.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.