sort: fix -kN,M handling (was including last separator into the comparison)
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 19 Sep 2015 20:06:40 +0000 (22:06 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 19 Sep 2015 20:06:40 +0000 (22:06 +0200)
Testcase:

$ printf '%s\n' a/a:a a:b | sort -t: -k1,1
a:b
a/a:a

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/sort.c
testsuite/sort.tests

index 1cb4c3e3ff5ae9e7e9ed51cdfaa529e5c313707b..36f02543b7fb9fc951a899e37a37ce80d07c94d6 100644 (file)
 //usage:     "\n       -f      Ignore case"
 //usage:     "\n       -g      General numerical sort"
 //usage:     "\n       -i      Ignore unprintable characters"
-//usage:     "\n       -k      Sort key"
 //usage:     "\n       -M      Sort month"
 //usage:       )
+//-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G)
 //usage:     "\n       -n      Sort numbers"
 //usage:       IF_FEATURE_SORT_BIG(
 //usage:     "\n       -o      Output to file"
-//usage:     "\n       -k      Sort by key"
-//usage:     "\n       -t CHAR Key separator"
+//usage:     "\n       -t CHAR Field separator"
+//usage:     "\n       -k N[,M] Sort by Nth field"
 //usage:       )
 //usage:     "\n       -r      Reverse sort order"
 //usage:       IF_FEATURE_SORT_BIG(
@@ -143,6 +143,9 @@ static char *get_key(char *str, struct sort_key *key, int flags)
                                        }
                                }
                        }
+                       /* Remove last delim: "abc:def:" => "abc:def" */
+                       if (key_separator && j && end != 0)
+                               end--;
                }
                if (!j) start = end;
        }
@@ -163,7 +166,8 @@ static char *get_key(char *str, struct sort_key *key, int flags)
                if (start > len) start = len;
        }
        /* Make the copy */
-       if (end < start) end = start;
+       if (end < start)
+               end = start;
        str = xstrndup(str+start, end-start);
        /* Handle -d */
        if (flags & FLAG_d) {
index 68fa3e405c88c36cbef2e40e717f212a98fdc812..c4b22346421e7ccefcd6d1204a233bfd36906be1 100755 (executable)
@@ -98,6 +98,14 @@ testing "sort with non-default leading delim 3" "sort -n -k3 -t/ input" "\
 //b/1
 " ""
 
+testing "sort with non-default leading delim 4" "sort -t: -k1,1 input" "\
+a:b
+a/a:a
+" "\
+a/a:a
+a:b
+" ""
+
 testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
 a c
 " "\