From unknown Tue Jun 17 22:27:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#22084: Potential Bug in sort -r Resent-From: =?UTF-8?Q?Adri=C3=A0?= Rovira Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Thu, 03 Dec 2015 16:51:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 22084 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: 22084@debbugs.gnu.org Cc: Deimos Ibanez Segura X-Debbugs-Original-To: bug-coreutils@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.144916144427232 (code B ref -1); Thu, 03 Dec 2015 16:51:02 +0000 Received: (at submit) by debbugs.gnu.org; 3 Dec 2015 16:50:44 +0000 Received: from localhost ([127.0.0.1]:37698 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4X5N-00074m-IT for submit@debbugs.gnu.org; Thu, 03 Dec 2015 11:50:44 -0500 Received: from eggs.gnu.org ([208.118.235.92]:56924) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4UUd-00019b-Pc for submit@debbugs.gnu.org; Thu, 03 Dec 2015 09:04:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a4UUZ-0004lu-KC for submit@debbugs.gnu.org; Thu, 03 Dec 2015 09:04:19 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,HTML_MESSAGE autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:58749) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4UUZ-0004lp-HW for submit@debbugs.gnu.org; Thu, 03 Dec 2015 09:04:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38665) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4UUT-0000uX-Ix for bug-coreutils@gnu.org; Thu, 03 Dec 2015 09:04:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a4UUP-0004kk-HJ for bug-coreutils@gnu.org; Thu, 03 Dec 2015 09:04:09 -0500 Received: from dash.upc.es ([147.83.2.50]:38925) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4UUP-0004k9-7F for bug-coreutils@gnu.org; Thu, 03 Dec 2015 09:04:05 -0500 Received: from dors2.upc.edu (dors2.upc.edu [147.83.2.222]) by dash.upc.es (8.14.1/8.13.1) with ESMTP id tB3CU6p6028230; Thu, 3 Dec 2015 13:30:07 +0100 Received: from dors2.upc.edu (localhost [127.0.0.1]) by dors2.upc.edu (8.14.4/8.14.4/Debian-4.1ubuntu1) with ESMTP id tB3CU6ws051103; Thu, 3 Dec 2015 13:30:06 +0100 Received: (from www-data@localhost) by dors2.upc.edu (8.14.4/8.14.4/Submit) id tB3CU6C1051102; Thu, 3 Dec 2015 13:30:06 +0100 X-Authentication-Warning: dors2.upc.edu: www-data set sender to adria.rovira@upc.edu using -f Received: from gage83.upc.edu (gage83.upc.edu [147.83.104.83]) by correu.upc.edu (Horde Framework) with HTTP; Thu, 03 Dec 2015 13:30:06 +0100 Date: Thu, 03 Dec 2015 13:30:06 +0100 Message-ID: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> From: =?UTF-8?Q?Adri=C3=A0?= Rovira User-Agent: Horde Application Framework 5 Content-Type: multipart/alternative; boundary="=__fuZvlMF8sOewK5jCcxqgL4" MIME-Version: 1.0 X-Mail-Scanned: Criba 2.0 + Clamd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Mailman-Approved-At: Thu, 03 Dec 2015 11:50:22 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) This message is in MIME format. --=__fuZvlMF8sOewK5jCcxqgL4 Content-Type: text/plain; charset=utf-8; format=flowed; DelSp=Yes Content-Description: Missatge de text Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Dear GNU, I am using the sort command in=C2=A0 (GNU coreutils) version 8.13 I noticed the reverse option is not correctly applied if it has to sort by more than one column. This behaviour is corrected by forcing again the type of sort. This happens with -n and -g. Example: echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2r 930 7.83 930 77.52 930 54.09 echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2rn 930 77.52 930 54.09 930 7.83 Kind Regards, Adri=C3=A0 and Deimos -------------------------------------------------------- o o o | Adri=C3=A0 Rovira Garcia o o o | Research group of Astronomy and GEomatics (gAGE) o o o | Universitat Polit=C3=A8cnica de Catalunya (UPC) -------------------------------------------------------- gAGE / UPC Departament de Matem=C3=A0tiques C/. Jordi Girona 1-3 Campus Nord UPC Building C3, Office 211 08034 Barcelona, Spain Tel:+34 93 401 25 31 Fax:+34 93 401 59 81 http://www.gage.upc.edu --=__fuZvlMF8sOewK5jCcxqgL4 Content-Type: text/html; charset=utf-8 Content-Description: Missatge HTML Content-Disposition: inline

Dear GNU,

I am using the sort command in  (GNU coreutils) version 8.13

I noticed the reverse option is not correctly applied if it has to sort by more than one column.

This behaviour is corrected by forcing again the type of sort.

This happens with -n and -g.

Example:

echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2r
930 7.83
930 77.52
930 54.09
echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2rn
930 77.52
930 54.09
930 7.83

Kind Regards,

Adrià and Deimos

--------------------------------------------------------
o o o | Adrià Rovira Garcia
o o o | Research group of Astronomy and GEomatics (gAGE)
o o o | Universitat Politècnica de Catalunya (UPC)
--------------------------------------------------------

gAGE / UPC
Departament de Matemàtiques
C/. Jordi Girona 1-3
Campus Nord UPC
Building C3, Office 211
08034 Barcelona, Spain
Tel:+34 93 401 25 31
Fax:+34 93 401 59 81
http://www.gage.upc.edu
--=__fuZvlMF8sOewK5jCcxqgL4-- From unknown Tue Jun 17 22:27:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#22084: Potential Bug in sort -r Resent-From: Assaf Gordon Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Thu, 03 Dec 2015 17:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 22084 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: =?UTF-8?Q?Adri=C3=A0?= Rovira , 22084@debbugs.gnu.org Cc: Deimos Ibanez Segura Received: via spool by 22084-submit@debbugs.gnu.org id=B22084.144916303429598 (code B ref 22084); Thu, 03 Dec 2015 17:18:02 +0000 Received: (at 22084) by debbugs.gnu.org; 3 Dec 2015 17:17:14 +0000 Received: from localhost ([127.0.0.1]:37708 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4XVJ-0007hK-Ol for submit@debbugs.gnu.org; Thu, 03 Dec 2015 12:17:14 -0500 Received: from mail-qk0-f171.google.com ([209.85.220.171]:36431) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4XVH-0007hC-LF for 22084@debbugs.gnu.org; Thu, 03 Dec 2015 12:17:12 -0500 Received: by qkda6 with SMTP id a6so32748667qkd.3 for <22084@debbugs.gnu.org>; Thu, 03 Dec 2015 09:17:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-type:content-transfer-encoding; bh=KnMF0DSEZJrOePkr1IE6PETwGsfw5ZVy6kPTu2WAkV0=; b=l3gxwvW5kw6bePLvFYJY7f0SL6LBZwGqQXR515uMDvoe/L4hQlHu+YvkfOufA6ryAP ZcrEi0lg3l8UyVVVnhwWj+Epq7w5HaMOHHxYvNPc4N3L5ybXSGUMkVElpAGUEsNe0YKl xguZ4ndkGaPGcbC2LIpdM5OeLVigOP7JM4btM5dlPTgJRBi6/6/cAhTK9+bJoIoGoR3J C5llj0WxjGlFzYpKQVpT68yo1wiYvdimE4sjQd2le8rGkHrd6yiBYdkbXK+yCfNYB4SY vjrBSgdBlJGPmP7F1tntYMxIcw3SX5XI7En3xFBnpQXr2hdqZrNbykYIBzbDgqrJyDUF sdvg== X-Received: by 10.55.72.23 with SMTP id v23mr12301668qka.24.1449163031038; Thu, 03 Dec 2015 09:17:11 -0800 (PST) Received: from disco.erlich.nygenome.org ([69.74.14.178]) by smtp.googlemail.com with ESMTPSA id 64sm3663408qhh.19.2015.12.03.09.17.10 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 03 Dec 2015 09:17:10 -0800 (PST) References: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> From: Assaf Gordon Message-ID: <5660794C.7080809@gmail.com> Date: Thu, 3 Dec 2015 12:18:04 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Score: -0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.7 (/) tag 22084 notabug close 22084 stop Hello Adrià and Deimos, On 12/03/2015 07:30 AM, Adrià Rovira wrote: > I noticed the reverse option is not correctly applied if it has to sort by more than one column. This is not a bug, but simply a usage issue. The "sort --help" page states: " ... OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key. " That is, 1. If you use global sorting option (-n), and a key option WITHOUT ordering option (e.g. '-k1,1') - the global sorting option is in effect. 2. If you use *any* sorting option in the key specification (e.g. '-k1,1r'), it overrides the global sorting option, thus the order in effect is just 'reverse' (implying: alphabetical order). These examples should demonstrate it: # Input data $ printf "1\n07\n2\n" 1 07 2 # Alphabetical (ascii) sort, # character "0" comes before "1" $ printf "1\n07\n2\n" | sort 07 1 2 # Numerical sort, value 7 comes after 2 $ printf "1\n07\n2\n" | sort -n 1 2 07 # Global option (-n), key without ordering option: # numeric sort in effect $ printf "1\n07\n2\n" | sort -n -k1,1 1 2 07 # Global option (-n), key with any ordering option # (in this case 'b' = ignore leading blanks) # global numeric ordering is ignore $ printf "1\n07\n2\n" | sort -n -k1b,1 07 1 2 # Adding the numeric ordering to the key - # takes effect $ printf "1\n07\n2\n" | sort -n -k1bn,1 1 2 07 regards, - Assaf From debbugs-submit-bounces@debbugs.gnu.org Thu Dec 03 12:19:48 2015 Received: (at control) by debbugs.gnu.org; 3 Dec 2015 17:19:48 +0000 Received: from localhost ([127.0.0.1]:37714 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4XXo-0007l8-01 for submit@debbugs.gnu.org; Thu, 03 Dec 2015 12:19:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55147) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4XXk-0007ks-AR; Thu, 03 Dec 2015 12:19:45 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 9B2611204B1; Thu, 3 Dec 2015 17:19:43 +0000 (UTC) Received: from [10.3.113.162] (ovpn-113-162.phx2.redhat.com [10.3.113.162]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tB3HJgXa022170; Thu, 3 Dec 2015 12:19:43 -0500 Subject: Re: bug#22084: Potential Bug in sort -r To: =?UTF-8?Q?Adri=c3=a0_Rovira?= , 22084-done@debbugs.gnu.org References: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> From: Eric Blake Openpgp: url=http://people.redhat.com/eblake/eblake.gpg X-Enigmail-Draft-Status: N1110 Organization: Red Hat, Inc. Message-ID: <566079A8.8080901@redhat.com> Date: Thu, 3 Dec 2015 10:19:36 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9" X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: control Cc: Deimos Ibanez Segura , GNU bug control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable tag 22084 notabug thanks On 12/03/2015 05:30 AM, Adri=C3=A0 Rovira wrote: > Dear GNU, >=20 > I am using the sort command in (GNU coreutils) version 8.13 >=20 > I noticed the reverse option is not correctly applied if it has to sort= by > more than one column. >=20 > This behaviour is corrected by forcing again the type of sort. >=20 > This happens with -n and -g. >=20 > Example: >=20 > echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2r First, a couple of notes: 'echo -e' is not portable; you should consider using printf(1) instead. Then, you are not specifying the keys you thought. Let's look at this with sort's --debug option: $ printf '930 7.83\n930 77.52\n930 54.09\n' | sort -n -k1 -k2r --debug sort: using =E2=80=98en_US.UTF-8=E2=80=99 sorting rules sort: key 1 is numeric and spans multiple fields sort: leading blanks are significant in key 2; consider also specifying '= b' 930 7.83 ___ _____ ________ 930 77.52 ___ ______ _________ 930 54.09 ___ ______ _________ You probably want to use the -k1,1 notation and an explicit -b to ensure that your field usage is sane: $ printf '930 7.83\n930 77.52\n930 54.09\n' | sort -n -k1,1 -k2b,2r --deb= ug sort: using =E2=80=98en_US.UTF-8=E2=80=99 sorting rules 930 7.83 ___ ____ ________ 930 77.52 ___ _____ _________ 930 54.09 ___ _____ _________ Now, on to your actual report. Here is what 'info sort' says: A position in a sort field specified with =E2=80=98-k=E2=80=99 may hav= e any of the option letters =E2=80=98MbdfghinRrV=E2=80=99 appended to it, in which cas= e no global ordering options are inherited by that particular field. The =E2=80=98-b= =E2=80=99 option may be independently attached to either or both of the start and end positions of a field specification, and if it is inherited from the global options it will be attached to both. If input lines can contain leading or adjacent blanks and =E2=80=98-t=E2=80=99 is not used, then =E2= =80=98-k=E2=80=99 is typically combined with =E2=80=98-b=E2=80=99 or an option that implicitly ignores l= eading blanks (=E2=80=98Mghn=E2=80=99) as otherwise the varying numbers of leading blan= ks in fields can cause confusing results. Or, referring to POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html The keydef argument is a restricted sort key field definition. The format of this definition is: field_start[type][,field_end[type]] where field_start and field_end define a key field restricted to a portion of the line (see the EXTENDED DESCRIPTION section), and type is a modifier from the list of characters 'b', 'd', 'f', 'i', 'n', 'r'. The 'b' modifier shall behave like the -b option, but shall apply only to the field_start or field_end to which it is attached. The other modifiers shall behave like the corresponding options, but shall apply only to the key field to which they are attached; they shall have this effect if specified with field_start, field_end, or both. If any modifier is attached to a field_start or to a field_end, no option shall apply to either. So the moment you add 'r' to a particular -k, that key is no longer benefiting from the global -n option, and you are correct that you then have to re-specify 'n' on a per-'-k' basis, as you did here: > echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2rn > 930 77.52 > 930 54.09 > 930 7.83 Since this is the documented behavior, and matches the POSIX requirement, there is no bug. I'm thus closing out this report; however, feel free to add further comments to the thread. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJWYHmoAAoJEKeha0olJ0NqT3YH/0efEL0kXv4gTqrZwZ0S2jqJ 8lA5apty0gnDLCWREx/M5YP2PhsmXxQOI/FvHHGEFDwsEwxl9za/LoyMXEtNDJP8 BgALnuPxNA1L7YNMbvVmuOpvoi7YbLmJ1209EKN/9eiwVR7t5KMsd9HQHqU6bvjU PnzOfKeDhdnWdFfp2yK86gHq8b+D4ksfvMJdHnp4icCuKuVdCU0T1BOjvVtUQTW8 20VobVBbl0y2x/b54/aoKxY48z6XWdrOrzzVPU3LdFGg16asKOwiiEfD/pg0vqi5 dojqr4DGdPXbDf+UEOmyDX6Afb/StFyuhkxyKh/iDun6CrOHWx/Fv+KKEOjQqMo= =oEws -----END PGP SIGNATURE----- --R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9-- From unknown Tue Jun 17 22:27:20 2025 MIME-Version: 1.0 X-Mailer: MIME-tools 5.503 (Entity 5.503) X-Loop: help-debbugs@gnu.org From: help-debbugs@gnu.org (GNU bug Tracking System) To: =?UTF-8?Q?Adri=C3=A0?= Rovira Subject: bug#22084: closed (Re: bug#22084: Potential Bug in sort -r) Message-ID: References: <566079A8.8080901@redhat.com> <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> X-Gnu-PR-Message: they-closed 22084 X-Gnu-PR-Package: coreutils X-Gnu-PR-Keywords: notabug Reply-To: 22084@debbugs.gnu.org Date: Thu, 03 Dec 2015 17:20:05 +0000 Content-Type: multipart/mixed; boundary="----------=_1449163205-29875-1" This is a multi-part message in MIME format... ------------=_1449163205-29875-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #22084: Potential Bug in sort -r which was filed against the coreutils package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 22084@debbugs.gnu.org. --=20 22084: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D22084 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1449163205-29875-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 22084-done) by debbugs.gnu.org; 3 Dec 2015 17:19:48 +0000 Received: from localhost ([127.0.0.1]:37712 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4XXn-0007l6-ER for submit@debbugs.gnu.org; Thu, 03 Dec 2015 12:19:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55147) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4XXk-0007ks-AR; Thu, 03 Dec 2015 12:19:45 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 9B2611204B1; Thu, 3 Dec 2015 17:19:43 +0000 (UTC) Received: from [10.3.113.162] (ovpn-113-162.phx2.redhat.com [10.3.113.162]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tB3HJgXa022170; Thu, 3 Dec 2015 12:19:43 -0500 Subject: Re: bug#22084: Potential Bug in sort -r To: =?UTF-8?Q?Adri=c3=a0_Rovira?= , 22084-done@debbugs.gnu.org References: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> From: Eric Blake Openpgp: url=http://people.redhat.com/eblake/eblake.gpg X-Enigmail-Draft-Status: N1110 Organization: Red Hat, Inc. Message-ID: <566079A8.8080901@redhat.com> Date: Thu, 3 Dec 2015 10:19:36 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9" X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: 22084-done Cc: Deimos Ibanez Segura , GNU bug control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable tag 22084 notabug thanks On 12/03/2015 05:30 AM, Adri=C3=A0 Rovira wrote: > Dear GNU, >=20 > I am using the sort command in (GNU coreutils) version 8.13 >=20 > I noticed the reverse option is not correctly applied if it has to sort= by > more than one column. >=20 > This behaviour is corrected by forcing again the type of sort. >=20 > This happens with -n and -g. >=20 > Example: >=20 > echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2r First, a couple of notes: 'echo -e' is not portable; you should consider using printf(1) instead. Then, you are not specifying the keys you thought. Let's look at this with sort's --debug option: $ printf '930 7.83\n930 77.52\n930 54.09\n' | sort -n -k1 -k2r --debug sort: using =E2=80=98en_US.UTF-8=E2=80=99 sorting rules sort: key 1 is numeric and spans multiple fields sort: leading blanks are significant in key 2; consider also specifying '= b' 930 7.83 ___ _____ ________ 930 77.52 ___ ______ _________ 930 54.09 ___ ______ _________ You probably want to use the -k1,1 notation and an explicit -b to ensure that your field usage is sane: $ printf '930 7.83\n930 77.52\n930 54.09\n' | sort -n -k1,1 -k2b,2r --deb= ug sort: using =E2=80=98en_US.UTF-8=E2=80=99 sorting rules 930 7.83 ___ ____ ________ 930 77.52 ___ _____ _________ 930 54.09 ___ _____ _________ Now, on to your actual report. Here is what 'info sort' says: A position in a sort field specified with =E2=80=98-k=E2=80=99 may hav= e any of the option letters =E2=80=98MbdfghinRrV=E2=80=99 appended to it, in which cas= e no global ordering options are inherited by that particular field. The =E2=80=98-b= =E2=80=99 option may be independently attached to either or both of the start and end positions of a field specification, and if it is inherited from the global options it will be attached to both. If input lines can contain leading or adjacent blanks and =E2=80=98-t=E2=80=99 is not used, then =E2= =80=98-k=E2=80=99 is typically combined with =E2=80=98-b=E2=80=99 or an option that implicitly ignores l= eading blanks (=E2=80=98Mghn=E2=80=99) as otherwise the varying numbers of leading blan= ks in fields can cause confusing results. Or, referring to POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html The keydef argument is a restricted sort key field definition. The format of this definition is: field_start[type][,field_end[type]] where field_start and field_end define a key field restricted to a portion of the line (see the EXTENDED DESCRIPTION section), and type is a modifier from the list of characters 'b', 'd', 'f', 'i', 'n', 'r'. The 'b' modifier shall behave like the -b option, but shall apply only to the field_start or field_end to which it is attached. The other modifiers shall behave like the corresponding options, but shall apply only to the key field to which they are attached; they shall have this effect if specified with field_start, field_end, or both. If any modifier is attached to a field_start or to a field_end, no option shall apply to either. So the moment you add 'r' to a particular -k, that key is no longer benefiting from the global -n option, and you are correct that you then have to re-specify 'n' on a per-'-k' basis, as you did here: > echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2rn > 930 77.52 > 930 54.09 > 930 7.83 Since this is the documented behavior, and matches the POSIX requirement, there is no bug. I'm thus closing out this report; however, feel free to add further comments to the thread. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJWYHmoAAoJEKeha0olJ0NqT3YH/0efEL0kXv4gTqrZwZ0S2jqJ 8lA5apty0gnDLCWREx/M5YP2PhsmXxQOI/FvHHGEFDwsEwxl9za/LoyMXEtNDJP8 BgALnuPxNA1L7YNMbvVmuOpvoi7YbLmJ1209EKN/9eiwVR7t5KMsd9HQHqU6bvjU PnzOfKeDhdnWdFfp2yK86gHq8b+D4ksfvMJdHnp4icCuKuVdCU0T1BOjvVtUQTW8 20VobVBbl0y2x/b54/aoKxY48z6XWdrOrzzVPU3LdFGg16asKOwiiEfD/pg0vqi5 dojqr4DGdPXbDf+UEOmyDX6Afb/StFyuhkxyKh/iDun6CrOHWx/Fv+KKEOjQqMo= =oEws -----END PGP SIGNATURE----- --R3EwHr29gNLRo6SxIAKtie1wh4ntv7Ce9-- ------------=_1449163205-29875-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 3 Dec 2015 16:50:44 +0000 Received: from localhost ([127.0.0.1]:37698 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4X5N-00074m-IT for submit@debbugs.gnu.org; Thu, 03 Dec 2015 11:50:44 -0500 Received: from eggs.gnu.org ([208.118.235.92]:56924) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4UUd-00019b-Pc for submit@debbugs.gnu.org; Thu, 03 Dec 2015 09:04:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a4UUZ-0004lu-KC for submit@debbugs.gnu.org; Thu, 03 Dec 2015 09:04:19 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,HTML_MESSAGE autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:58749) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4UUZ-0004lp-HW for submit@debbugs.gnu.org; Thu, 03 Dec 2015 09:04:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38665) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4UUT-0000uX-Ix for bug-coreutils@gnu.org; Thu, 03 Dec 2015 09:04:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a4UUP-0004kk-HJ for bug-coreutils@gnu.org; Thu, 03 Dec 2015 09:04:09 -0500 Received: from dash.upc.es ([147.83.2.50]:38925) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4UUP-0004k9-7F for bug-coreutils@gnu.org; Thu, 03 Dec 2015 09:04:05 -0500 Received: from dors2.upc.edu (dors2.upc.edu [147.83.2.222]) by dash.upc.es (8.14.1/8.13.1) with ESMTP id tB3CU6p6028230; Thu, 3 Dec 2015 13:30:07 +0100 Received: from dors2.upc.edu (localhost [127.0.0.1]) by dors2.upc.edu (8.14.4/8.14.4/Debian-4.1ubuntu1) with ESMTP id tB3CU6ws051103; Thu, 3 Dec 2015 13:30:06 +0100 Received: (from www-data@localhost) by dors2.upc.edu (8.14.4/8.14.4/Submit) id tB3CU6C1051102; Thu, 3 Dec 2015 13:30:06 +0100 X-Authentication-Warning: dors2.upc.edu: www-data set sender to adria.rovira@upc.edu using -f Received: from gage83.upc.edu (gage83.upc.edu [147.83.104.83]) by correu.upc.edu (Horde Framework) with HTTP; Thu, 03 Dec 2015 13:30:06 +0100 Date: Thu, 03 Dec 2015 13:30:06 +0100 Message-ID: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> From: =?utf-8?b?QWRyacOg?= Rovira To: bug-coreutils@gnu.org Subject: Potential Bug in sort -r User-Agent: Horde Application Framework 5 Content-Type: multipart/alternative; boundary="=__fuZvlMF8sOewK5jCcxqgL4" MIME-Version: 1.0 X-Mail-Scanned: Criba 2.0 + Clamd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Thu, 03 Dec 2015 11:50:22 -0500 Cc: Deimos Ibanez Segura X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -5.0 (-----) This message is in MIME format. --=__fuZvlMF8sOewK5jCcxqgL4 Content-Type: text/plain; charset=utf-8; format=flowed; DelSp=Yes Content-Description: Missatge de text Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Dear GNU, I am using the sort command in=C2=A0 (GNU coreutils) version 8.13 I noticed the reverse option is not correctly applied if it has to sort by more than one column. This behaviour is corrected by forcing again the type of sort. This happens with -n and -g. Example: echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2r 930 7.83 930 77.52 930 54.09 echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2rn 930 77.52 930 54.09 930 7.83 Kind Regards, Adri=C3=A0 and Deimos -------------------------------------------------------- o o o | Adri=C3=A0 Rovira Garcia o o o | Research group of Astronomy and GEomatics (gAGE) o o o | Universitat Polit=C3=A8cnica de Catalunya (UPC) -------------------------------------------------------- gAGE / UPC Departament de Matem=C3=A0tiques C/. Jordi Girona 1-3 Campus Nord UPC Building C3, Office 211 08034 Barcelona, Spain Tel:+34 93 401 25 31 Fax:+34 93 401 59 81 http://www.gage.upc.edu --=__fuZvlMF8sOewK5jCcxqgL4 Content-Type: text/html; charset=utf-8 Content-Description: Missatge HTML Content-Disposition: inline

Dear GNU,

I am using the sort command in  (GNU coreutils) version 8.13

I noticed the reverse option is not correctly applied if it has to sort by more than one column.

This behaviour is corrected by forcing again the type of sort.

This happens with -n and -g.

Example:

echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2r
930 7.83
930 77.52
930 54.09
echo -e "930 7.83\n930 77.52\n930 54.09" | sort -n -k1 -k2rn
930 77.52
930 54.09
930 7.83

Kind Regards,

Adrià and Deimos

--------------------------------------------------------
o o o | Adrià Rovira Garcia
o o o | Research group of Astronomy and GEomatics (gAGE)
o o o | Universitat Politècnica de Catalunya (UPC)
--------------------------------------------------------

gAGE / UPC
Departament de Matemàtiques
C/. Jordi Girona 1-3
Campus Nord UPC
Building C3, Office 211
08034 Barcelona, Spain
Tel:+34 93 401 25 31
Fax:+34 93 401 59 81
http://www.gage.upc.edu
--=__fuZvlMF8sOewK5jCcxqgL4-- ------------=_1449163205-29875-1-- From unknown Tue Jun 17 22:27:20 2025 X-Loop: help-debbugs@gnu.org Subject: bug#22084: Potential Bug in sort -r Resent-From: Adria Rovira Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Thu, 03 Dec 2015 17:30:06 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 22084 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: notabug To: Assaf Gordon , 22084@debbugs.gnu.org Cc: Deimos Ibanez Segura Received: via spool by 22084-submit@debbugs.gnu.org id=B22084.144916375430691 (code B ref 22084); Thu, 03 Dec 2015 17:30:06 +0000 Received: (at 22084) by debbugs.gnu.org; 3 Dec 2015 17:29:14 +0000 Received: from localhost ([127.0.0.1]:37723 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4Xgv-0007yr-DD for submit@debbugs.gnu.org; Thu, 03 Dec 2015 12:29:14 -0500 Received: from dash.upc.es ([147.83.2.50]:36546) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1a4Xga-0007y8-9e for 22084@debbugs.gnu.org; Thu, 03 Dec 2015 12:29:11 -0500 Received: from ackerman2.upc.es (ackerman2.upc.es [147.83.2.244]) by dash.upc.es (8.14.1/8.13.1) with ESMTP id tB3HSoQK032717 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=FAIL); Thu, 3 Dec 2015 18:28:50 +0100 Received: from [147.83.104.83] (gage83.upc.edu [147.83.104.83]) (authenticated bits=0) by ackerman2.upc.es (8.14.4/8.14.4) with ESMTP id tB3HSoLp032565 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Thu, 3 Dec 2015 18:28:50 +0100 References: <20151203133006.Horde.-fy8XGIuPwFgYXBbRjNVwUT@correu.upc.edu> <5660794C.7080809@gmail.com> From: Adria Rovira Message-ID: <56607BCB.4060600@upc.edu> Date: Thu, 3 Dec 2015 18:28:43 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <5660794C.7080809@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Greylist: Default is to whitelist mail, not delayed by milter-greylist-4.4.3 (ackerman2.upc.es [147.83.2.244]); Thu, 03 Dec 2015 18:28:50 +0100 (CET) X-Scanned-By: MIMEDefang 2.70 on 147.83.2.244 X-Mail-Scanned: Criba 2.0 + Clamd X-Spam-Score: -2.3 (--) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) Dear Assaf, Thank You very much!! We didn't read carefully enough the manual. Best Regards, Adrià On 12/03/2015 06:18 PM, Assaf Gordon wrote: > tag 22084 notabug > close 22084 > stop > > Hello Adrià and Deimos, > > > On 12/03/2015 07:30 AM, Adrià Rovira wrote: >> I noticed the reverse option is not correctly applied if it has to >> sort by more than one column. > > This is not a bug, but simply a usage issue. > The "sort --help" page states: > " ... OPTS is one or more single-letter ordering options [bdfgiMhnRrV], > which override global ordering options for that key. " > > That is, > 1. If you use global sorting option (-n), and a key option WITHOUT > ordering option (e.g. '-k1,1') - the global sorting option is in > effect. > > 2. If you use *any* sorting option in the key specification > (e.g. '-k1,1r'), it overrides the global sorting option, > thus the order in effect is just 'reverse' (implying: alphabetical > order). > > These examples should demonstrate it: > > # Input data > $ printf "1\n07\n2\n" > 1 > 07 > 2 > # Alphabetical (ascii) sort, > # character "0" comes before "1" > $ printf "1\n07\n2\n" | sort > 07 > 1 > 2 > # Numerical sort, value 7 comes after 2 > $ printf "1\n07\n2\n" | sort -n > 1 > 2 > 07 > # Global option (-n), key without ordering option: > # numeric sort in effect > $ printf "1\n07\n2\n" | sort -n -k1,1 > 1 > 2 > 07 > # Global option (-n), key with any ordering option > # (in this case 'b' = ignore leading blanks) > # global numeric ordering is ignore > $ printf "1\n07\n2\n" | sort -n -k1b,1 > 07 > 1 > 2 > # Adding the numeric ordering to the key - > # takes effect > $ printf "1\n07\n2\n" | sort -n -k1bn,1 > 1 > 2 > 07 > > > regards, > - Assaf > > -- -------------------------------------------------------- o o o | Adrià Rovira Garcia o o o | Research group of Astronomy and GEomatics (gAGE) o o o | Universitat Politècnica de Catalunya (UPC) -------------------------------------------------------- gAGE / UPC Departament de Matemàtiques C/. Jordi Girona 1-3 Campus Nord UPC Building C3, Office 211 08034 Barcelona, Spain Tel:+34 93 401 25 31 Fax:+34 93 401 59 81 http://www.gage.upc.edu