1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-10 06:03:31 +02:00

sort: add --sort=... option.

* src/sort.c (SORT_OPTION): New enum.
(sort_args, sort_types): Define.
(usage, long_options, main): New option --sort.
* tests/sort/Test.pm: Test it.
* doc/coreutils.texi (sort invocation): Document --sort option.
* NEWS: Mention this.
This commit is contained in:
Andreas Schwab
2008-02-20 08:36:56 +01:00
committed by Jim Meyering
parent c78039b87d
commit cb3147d298
4 changed files with 37 additions and 2 deletions

5
NEWS
View File

@@ -21,6 +21,11 @@ GNU coreutils NEWS -*- outline -*-
join now verifies that the inputs are in sorted order. This check can
be turned off with the --nocheck-order option.
sort accepts the new option --sort=WORD, where WORD can be one of
general-numeric, month, numeric or random. These are equivalent to the
options --general-numeric-sort/-g, --month-sort/-M, --numeric-sort/-n
and --random-sort/-R, resp.
** Improvements
ls --color no longer outputs unnecessary escape sequences

View File

@@ -3536,8 +3536,10 @@ The @env{LC_CTYPE} locale determines character types.
@item -g
@itemx --general-numeric-sort
@itemx --sort=general-numeric
@opindex -g
@opindex --general-numeric-sort
@opindex --sort
@cindex general numeric sort
@vindex LC_NUMERIC
Sort numerically, using the standard C function @code{strtod} to convert
@@ -3580,8 +3582,10 @@ This option has no effect if the stronger @option{--dictionary-order}
@item -M
@itemx --month-sort
@itemx --sort=month
@opindex -M
@opindex --month-sort
@opindex --sort
@cindex months, sorting by
@vindex LC_TIME
An initial string, consisting of any amount of blanks, followed
@@ -3594,8 +3598,10 @@ can change this.
@item -n
@itemx --numeric-sort
@itemx --sort=numeric
@opindex -n
@opindex --numeric-sort
@opindex --sort
@cindex numeric sort
@vindex LC_NUMERIC
Sort numerically. The number begins each line and consists
@@ -3623,8 +3629,10 @@ appear earlier in the output instead of later.
@item -R
@itemx --random-sort
@itemx --sort=random
@opindex -R
@opindex --random-sort
@opindex --sort
@cindex random sort
Sort by hashing the input keys and then sorting the hash values.
Choose the hash function at random, ensuring that it is free of

View File

@@ -1,5 +1,5 @@
/* sort - sort lines of text (with all kinds of options).
Copyright (C) 1988, 1991-2007 Free Software Foundation, Inc.
Copyright (C) 1988, 1991-2008 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -329,6 +329,9 @@ Ordering options:\n\
-n, --numeric-sort compare according to string numerical value\n\
-R, --random-sort sort by random hash of keys\n\
--random-source=FILE get random bytes from FILE (default /dev/urandom)\n\
--sort=WORD sort according to WORD:\n\
general-numeric -g, month -M, numeric -n,\n\
random -R\n\
-r, --reverse reverse the result of comparisons\n\
\n\
"), stdout);
@@ -391,7 +394,8 @@ enum
{
CHECK_OPTION = CHAR_MAX + 1,
COMPRESS_PROGRAM_OPTION,
RANDOM_SOURCE_OPTION
RANDOM_SOURCE_OPTION,
SORT_OPTION
};
static char const short_options[] = "-bcCdfgik:mMno:rRsS:t:T:uy:z";
@@ -411,6 +415,7 @@ static struct option const long_options[] =
{"numeric-sort", no_argument, NULL, 'n'},
{"random-sort", no_argument, NULL, 'R'},
{"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
{"sort", required_argument, NULL, SORT_OPTION},
{"output", required_argument, NULL, 'o'},
{"reverse", no_argument, NULL, 'r'},
{"stable", no_argument, NULL, 's'},
@@ -434,6 +439,16 @@ static char const check_types[] =
};
ARGMATCH_VERIFY (check_args, check_types);
static char const *const sort_args[] =
{
"general-numeric", "month", "numeric", "random", NULL
};
static char const sort_types[] =
{
'g', 'M', 'n', 'R'
};
ARGMATCH_VERIFY (sort_args, sort_types);
/* The set of signals that are caught. */
static sigset_t caught_signals;
@@ -2902,6 +2917,9 @@ main (int argc, char **argv)
files[nfiles++] = optarg;
break;
case SORT_OPTION:
c = XARGMATCH ("--sort", optarg, sort_args, sort_types);
/* Fall through. */
case 'b':
case 'd':
case 'f':

View File

@@ -274,6 +274,7 @@ my @tv = (
["incompat4", '-c -o /dev/null', '', '', 2],
["incompat5", '-C -o /dev/null', '', '', 2],
["incompat6", '-cC', '', '', 2],
["incompat7", '--sort=random -n', '', '', 2],
# -t '\0' is accepted, as of coreutils-5.0.91
['nul-tab', "-k2,2 -t '\\0'", "a\0z\01\nb\0y\02\n", "b\0y\02\na\0z\01\n", 0],
@@ -289,6 +290,9 @@ my @tv = (
# Exercise the code that enlarges the line buffer. See the thread here:
# http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/11006
['realloc-buf', '-S1', 'a'x4000 ."\n", 'a'x4000 ."\n", 0],
["sort-numeric", '--sort=numeric', ".01\n0\n", "0\n.01\n", 0],
["sort-gennum", '--sort=general-numeric', "1e2\n2e1\n", "2e1\n1e2\n", 0],
);
sub test_vector