3

The POSIX description of the -b flag for the sort command is

Ignore leading characters when determining the starting and ending positions of a restricted sort key.

I can understand the use for the starting position of the key, but what about the ending positions? Can anybody give an example?

For instance, with the locale set to POSIX, the file with content

x  z
x a

would be sorted differently under sort -k 2 and sort -k 2b, but I haven't been able to come up with a case where, say, sort -k 2,3b and sort -k 2,3 would make a difference.

3

It has an effect when you add reverse to the comparison. Order of precedence are changed as in the -r only apply to last-resort comparison.

No reverse:

$ sort -k 1,2 sample
A  34
A 33

$ sort -k 1,2b sample
A  34
A 33

Reverse:

$ sort -rk 1,2 sample
A 33
A  34

$ sort -rk 1,2b sample
A  34
A 33
  • I'm still thinking about how it works, but thanks a lot! – ezequiel-garzon Oct 24 '13 at 16:37
  • I still don't see the rationale behind it. Why doesn't sort -k 1,2b alter the result in your first example? – ezequiel-garzon Oct 24 '13 at 19:52
  • 1
    @eze: When using b with -r, the -r only apply to last-resort comparison. – Runium Oct 25 '13 at 8:28
  • 1
    @eze: sort -k 1,2b has no effect. Field 2 is not left-trimmed. – Runium Oct 25 '13 at 8:35
  • 1
    @eze: The meaning change, not the contents. Though at this moment I'm starting to doubt myself. Also, if you use GNU-sort, check out the --debug option. It can perhaps help ... had forgotten about that one. – Runium Oct 25 '13 at 9:38
3

In case anybody else is curious about this... and happens to find this question, Sukminder's mention of the --debug flag in GNU sort has helped me find an answer to scratch this itch. Suppose, as an admittedly contrived example, that you want to sort this sloppily formatted file:

x   FRA-Paris    Pierre
x ESP-Barcelona   Jordi
x     FRA-Nice  Charles
x    FRA-Toulouse Nicole
x   ESP-Seville  Javier
x    ESP-Madrid  Carlos

into this:

x    ESP-Madrid  Carlos
x   ESP-Seville  Javier
x ESP-Barcelona   Jordi
x     FRA-Nice  Charles
x    FRA-Toulouse Nicole
x   FRA-Paris    Pierre

That is, only paying attention to the country in the second field and then to the person's name, in the third field. We want to consider the first three characters of the second field, and it is important to ignore leading blanks when determining both the first character and the third character.

The command sort -k 2b,2.3b -k 3b would work, but sort -k 2b,2.3 -k 3b wouldn't. It must be said that in this case the most natural way would be to apply a global -b option, simply as sort -bk 2,2.3 -k 3.

  • 1
    Great. And good example. On the side: You probably is aware, but GNU-sort also has the stable -s option which can be very useful. – Runium Oct 28 '13 at 10:31
  • I didn't know about it until I looked into the --debug option. Thanks a lot for coming back! I don't want this site to issue me another warning that I shouldn't chat in the comments! Thanks a lot, Sukminder! – ezequiel-garzon Oct 28 '13 at 21:18

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.