GNU bug report logs - #24906
gnu sort, what am I doing wrong?

Previous Next

Package: coreutils;

Reported by: Arnold Robbins <arnold <at> skeeve.com>

Date: Wed, 9 Nov 2016 04:24:01 UTC

Severity: normal

Tags: notabug

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 24906 in the body.
You can then email your comments to 24906 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#24906; Package coreutils. (Wed, 09 Nov 2016 04:24:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Arnold Robbins <arnold <at> skeeve.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Wed, 09 Nov 2016 04:24:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Arnold Robbins <arnold <at> skeeve.com>
To: bug-coreutils <at> gnu.org
Subject: gnu sort, what am I doing wrong?
Date: Wed, 09 Nov 2016 06:23:06 +0200
Hi.

I'm trying to sort by the fifth field and then numerically in reverse
order by the sixth field. 

$ sort --version
sort (GNU coreutils) 8.25

Here's my data:

$ cat checkbook.txt 
# Year : Month : Day : Recipient : D / W : Amount
2015:11:9:Joe's Coffee:W:5.00
2015:11:12:Mary's Doughnuts:W:5.00
2015:12:10:Joe's Coffee:W:10.00
2015:12:15:Mary's Doughnuts:W:10.00
2016:1:2:Hank's Party Store:W:35.00
2016:1:31:O'Reilly Media:D:100.00

And here's what I'm doing:

$ grep -v ^# checkbook.txt | sort -t: -k5 -k6rg
2016:1:31:O'Reilly Media:D:100.00
2015:12:10:Joe's Coffee:W:10.00
2015:12:15:Mary's Doughnuts:W:10.00
2016:1:2:Hank's Party Store:W:35.00
2015:11:12:Mary's Doughnuts:W:5.00
2015:11:9:Joe's Coffee:W:5.00

Why aren't these sorted by amounts in descending order?
What am I missing?

Thanks,

Arnold




Information forwarded to bug-coreutils <at> gnu.org:
bug#24906; Package coreutils. (Wed, 09 Nov 2016 06:15:01 GMT) Full text and rfc822 format available.

Message #8 received at 24906 <at> debbugs.gnu.org (full text, mbox):

From: Assaf Gordon <assafgordon <at> gmail.com>
To: Arnold Robbins <arnold <at> skeeve.com>
Cc: 24906 <at> debbugs.gnu.org
Subject: Re: bug#24906: gnu sort, what am I doing wrong?
Date: Wed, 9 Nov 2016 01:14:35 -0500
Hello Arnold,

> On Nov 8, 2016, at 23:23, Arnold Robbins <arnold <at> skeeve.com> wrote:
> 
> $ grep -v ^# checkbook.txt | sort -t: -k5 -k6rg
> [...]
> Why aren't these sorted by amounts in descending order?
> What am I missing?

The option "-k5" means "field 5 till the end of the line", which includes field 6.
e.g "W:10.00" sorts before "W:35.00".
You likely want "-k5,5" which means "field 5 only".

The "--debug" option will show which part of each line is taken as the sorting key:
===
$ grep -v ^# checkbook.txt | sort --debug -t: -k5 -k6rg
sort: using ‘en_US.UTF-8’ sorting rules
sort: key 2 is numeric and spans multiple fields
2016:1:31:O'Reilly Media:D:100.00
                         ________
                           ______
_________________________________
2015:12:10:Joe's Coffee:W:10.00
                        _______
                          _____
_______________________________
2015:12:15:Mary's Doughnuts:W:10.00
                            _______
                              _____
___________________________________
2016:1:2:Hank's Party Store:W:35.00
                            _______
                              _____
___________________________________
2015:11:12:Mary's Doughnuts:W:5.00
                            ______
                              ____
__________________________________
2015:11:9:Joe's Coffee:W:5.00
                       ______
                         ____
_____________________________

===

versus:

===
$ grep -v ^# checkbook.txt | sort --debug -t: -k5,5 -k6,6rg
sort: using ‘en_US.UTF-8’ sorting rules
2016:1:31:O'Reilly Media:D:100.00
                         _
                           ______
_________________________________
2016:1:2:Hank's Party Store:W:35.00
                            _
                              _____
___________________________________
2015:12:10:Joe's Coffee:W:10.00
                        _
                          _____
_______________________________
2015:12:15:Mary's Doughnuts:W:10.00
                            _
                              _____
___________________________________
2015:11:12:Mary's Doughnuts:W:5.00
                            _
                              ____
__________________________________
2015:11:9:Joe's Coffee:W:5.00
                       _
                         ____
_____________________________
===

regards,
 - assaf





Information forwarded to bug-coreutils <at> gnu.org:
bug#24906; Package coreutils. (Wed, 09 Nov 2016 14:17:02 GMT) Full text and rfc822 format available.

Message #11 received at 24906 <at> debbugs.gnu.org (full text, mbox):

From: arnold <at> skeeve.com
To: assafgordon <at> gmail.com, arnold <at> skeeve.com
Cc: 24906 <at> debbugs.gnu.org
Subject: Re: bug#24906: gnu sort, what am I doing wrong?
Date: Wed, 09 Nov 2016 07:11:23 -0700
Hi Assaf.

Much thanks for the explanation. That makes sense and helps a lot.

Arnold

Assaf Gordon <assafgordon <at> gmail.com> wrote:

> Hello Arnold,
>
> > On Nov 8, 2016, at 23:23, Arnold Robbins <arnold <at> skeeve.com> wrote:
> > 
> > $ grep -v ^# checkbook.txt | sort -t: -k5 -k6rg
> > [...]
> > Why aren't these sorted by amounts in descending order?
> > What am I missing?
>
> The option "-k5" means "field 5 till the end of the line", which includes field 6.
> e.g "W:10.00" sorts before "W:35.00".
> You likely want "-k5,5" which means "field 5 only".
>
> The "--debug" option will show which part of each line is taken as the sorting key:
> ===
> $ grep -v ^# checkbook.txt | sort --debug -t: -k5 -k6rg
> sort: using ‘en_US.UTF-8’ sorting rules
> sort: key 2 is numeric and spans multiple fields
> 2016:1:31:O'Reilly Media:D:100.00
>                          ________
>                            ______
> _________________________________
> 2015:12:10:Joe's Coffee:W:10.00
>                         _______
>                           _____
> _______________________________
> 2015:12:15:Mary's Doughnuts:W:10.00
>                             _______
>                               _____
> ___________________________________
> 2016:1:2:Hank's Party Store:W:35.00
>                             _______
>                               _____
> ___________________________________
> 2015:11:12:Mary's Doughnuts:W:5.00
>                             ______
>                               ____
> __________________________________
> 2015:11:9:Joe's Coffee:W:5.00
>                        ______
>                          ____
> _____________________________
>
> ===
>
> versus:
>
> ===
> $ grep -v ^# checkbook.txt | sort --debug -t: -k5,5 -k6,6rg
> sort: using ‘en_US.UTF-8’ sorting rules
> 2016:1:31:O'Reilly Media:D:100.00
>                          _
>                            ______
> _________________________________
> 2016:1:2:Hank's Party Store:W:35.00
>                             _
>                               _____
> ___________________________________
> 2015:12:10:Joe's Coffee:W:10.00
>                         _
>                           _____
> _______________________________
> 2015:12:15:Mary's Doughnuts:W:10.00
>                             _
>                               _____
> ___________________________________
> 2015:11:12:Mary's Doughnuts:W:5.00
>                             _
>                               ____
> __________________________________
> 2015:11:9:Joe's Coffee:W:5.00
>                        _
>                          ____
> _____________________________
> ===
>
> regards,
>  - assaf
>




Added tag(s) notabug. Request was from Assaf Gordon <assafgordon <at> gmail.com> to control <at> debbugs.gnu.org. (Sun, 28 Oct 2018 07:19:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 24906 <at> debbugs.gnu.org and Arnold Robbins <arnold <at> skeeve.com> Request was from Assaf Gordon <assafgordon <at> gmail.com> to control <at> debbugs.gnu.org. (Sun, 28 Oct 2018 07:19:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 25 Nov 2018 12:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 6 years and 205 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.