Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

sort (Unix)

From Wikipedia, the free encyclopedia
Standard UNIX utility
sort
Thesort command
Original authorKen Thompson (AT&T Bell Laboratories)
DevelopersVariousopen-source andcommercial developers
Initial releaseNovember 3, 1971; 54 years ago (1971-11-03)
Written inC
Operating systemMultics,Unix,Unix-like,V,Plan 9,Inferno,MSX-DOS,IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils:GPLv3+
Plan 9:MIT License

Incomputing,sort is a standardcommand line program ofUnix andUnix-likeoperating systems, that prints the lines of its input or concatenation of allfiles listed in itsargument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is the default field separator. The command supports a number ofcommand-line options that can vary by implementation. For instance the "-r" flag will reverse the sort order. Sort ordering is affected by the environment'slocale settings.[1]

History

[edit]

Asort command that invokes a general sort facility was first implemented withinMultics.[2] Later, it appeared inVersion 1 Unix. This version was originally written byKen Thompson atAT&T Bell Laboratories. ByVersion 4 Thompson had modified it to usepipes, but sort retained an option to name the output file because it was used to sort a file in place. InVersion 5, Thompson invented "-" to representstandard input.[3]

sort is part ofX/Open Portability Guide Issue 2 (1987). From there it was inherited intoPOSIX.[4]

The version ofsort bundled inGNUcoreutils was written by Mike Haertel and Paul Eggert.[1] This implementation employs themergesort algorithm. It offers an option to sort in parallel, though performance gain diminishes after 8 threads.[5]GNU parallel also provides a wrapper to perform parallel invocations ofsort with similar performance-gain characteristics: on a 48-core system, the speedup is about 3×.[6]

Thesort command has also been ported to theIBM i operating system, being accessible from the POSIX-compatibleQshell.[7]

Non-POSIX ports

[edit]

Similar commands are available on many other operating systems, for example asort command is part ofASCII'sMSX-DOS2 Tools forMSX-DOS version 2.[8]

The "uutils" project provides a cross-platform implementation ofsort written inRust, with support for all of GNU coreutil's options. It uses thepar_sort_by orpar_sort_unstable_by function of Rayon, the Rust multi-threading library, implementing either an adaptive mergesort inspired bytimsort or a variation ofpattern-defeating quicksort.[9]

Syntax

[edit]
sort [OPTION]... [FILE]...

With noFILE, or whenFILE is-, the command reads fromstandard input.

Parameters

[edit]

In the table below, "Short" indicates only support for the one-letter (short) form of the option. Long options are originally a GNU extension and is not part of any version of SUS or POSIX. It has since also been adopted by FreeBSD.

NameDescriptionSUS/POSIXPlan 9InfernoFreeBSDLinuxMSX-DOSIBM i
-b,
--ignore-leading-blanks
roIgnores leading blanks.ShortShortNoYesYesNoShort
-c,
--check
Check that input file is sorted.NoShortNoYesYesNoShort
-C,
--check=<silent|quiet>
Like -c, but does not report the first bad line.NoNoNoYesYesNoNo
-d,
--dictionary-order
Considers only blanks and alphanumeric characters.ShortShortNoYesYesNoShort
-f,
--ignore-case
Fold lower case to upper case characters.ShortShortNoYesYesNoShort
-g,
--general-numeric-sort,
--sort=general-numeric
Compares according to general numerical value.ShortShortNoYesYesNoNo
-h,
--human-numeric-sort,
--sort=human-numeric
Compare human readable numbers (e.g., 2K 1G).ShortNoNoYesYesNoNo
-i,
--ignore-nonprinting
Considers only printable characters.ShortShortNoYesYesNoShort
-k,
--key=POS1[,POS2]
Start a key atPOS1 (origin 1), end it atPOS2 (default end of line)NoNoNoYesYesNoNo
-mMerge only; input files are assumed to be presorted.NoShortNoYesYesNoShort
-M,
--month-sort,
--sort=month
Compares (unknown) < 'JAN' < ... < 'DEC'.ShortShortNoYesYesNoNo
-n,
--numeric-sort,
--sort=numeric
Compares according to string numerical value.ShortShortShortYesYesNoShort
-oOUTPUTUsesOUTPUT file instead of standard output.NoShortNoYesYesNoShort
-r,
--reverse
Reverses the result of comparisons.ShortShortShortYesYesNoShort
-R,
--random-sort,
--sort=random
Shuffles, but groups identical keys. See also:shufNoNoNoYesYesNoNo
-sStabilizes sort by disabling last-resort comparison.NoNoNoYesYesNoNo
-Ssize,
--buffer-size=size
Usesize for the maximum size of the memory buffer.NoNoNoYesNoNoNo
-tchar,
--field-separator=char
Useschar instead of non-blank to blank transition. In other words, 'Tab character' separating fields ischar.NoShortNoYesYesNoShort
-Tdir,
--temporary-directory=dir
Usesdir for temporaries.NoShortNoYesYesNoNo
-u,
--unique
Unique processing to suppress all but one in each set of lines having equal keys.NoShortNoYesYesNoShort
-V,
--version-sort
Natural sort of (version) numbers within textNoNoNoYesYesNoNo
-wLike -i, but ignore only tabs and spaces.NoYesNoNoNoNoNo
-z,
--zero-terminated
End lines with 0 byte, not newlineNoNoNoYesYesNoNo
--helpDisplay help and exitNoNoNoYesYesNoNo
--versionOutput version information and exitNoNoNoYesYesNoNo
/RReverses the result of comparisons.NoNoNoNoNoYesNo
/SSpecify the number of digits to determine how many digits of each line should be judged.NoNoNoNoNoYesNo
/ASort by ASCII code.NoNoNoNoNoYesNo
/HInclude hidden files when using wild cards.NoNoNoNoNoYesNo

Examples

[edit]

Sort a file in alphabetical order

[edit]
$catphonebookSmith, Brett     555-4321Doe, John        555-1234Doe, Jane        555-3214Avery, Cory      555-4132Fogarty, Suzie   555-2314
$sortphonebookAvery, Cory      555-4132Doe, Jane        555-3214Doe, John        555-1234Fogarty, Suzie   555-2314Smith, Brett     555-4321

Sort by number

[edit]

The-n option makes the program sort according to numerical value. Thedu command produces output that starts with a number, the file size, so its output can be piped tosort to produce a list of files sorted by (ascending) file size:

$du/bin/*|sort-n4       /bin/domainname24      /bin/ls102     /bin/sh304     /bin/csh

Thefind command with thels option prints file sizes in the 7th field, so a list of theLaTeX files sorted by file size is produced by:

$find.-name"*.tex"-ls|sort-k7n

Columns or fields

[edit]

Use the-k option to sort on a certain column. For example, use "-k 2" to sort on the second column. In old versions of sort, the+1 option made the program sort on the second column of data (+2 for the third, etc.). This usage is deprecated.

$catzipcodeAdam  12345Bob   34567Joe   56789Sam   45678Wendy 23456
$sort-k2nzipcodeAdam  12345Wendy 23456Bob   34567Sam   45678Joe   56789

Sort on multiple fields

[edit]

The-k m,n option lets you sort on a key that is potentially composed of multiple fields (start at columnm, end at columnn):

$catquotafred 2000bob 1000an 1000chad 1000don 1500eric 500
$sort-k2,2n-k1,1quotaeric 500an 1000bob 1000chad 1000don 1500fred 2000

Here the first sort is done using column 2.-k2,2n specifies sorting on the key starting and ending with column 2, and sorting numerically. If-k2 is used instead, the sort key would begin at column 2 and extend to the end of the line, spanning all the fields in between.-k1,1 dictates breaking ties using the value in column 1, sorting alphabetically by default. Note that bob, and chad have the same quota and are sorted alphabetically in the final output.

Sorting a pipe delimited file

[edit]
$sort-k2,2,-k1,1-t'|'zipcodeAdam|12345Wendy|23456Sam|45678Joe|56789Bob|34567

Sorting a tab delimited file

[edit]

Sorting a file withtab separated values requires atab character to be specified as the column delimiter. This illustration uses the shell's dollar-quote notation[10][11]to specify the tab as aC escape sequence.

$sort-k2,2-t$'\t'phonebookDoe, John555-1234Fogarty, Suzie555-2314Doe, Jane555-3214Avery, Cory555-4132Smith, Brett555-4321

Sort in reverse

[edit]

The-r option just reverses the order of the sort:

$sort-rk2nzipcodeJoe   56789Sam   45678Bob   34567Wendy 23456Adam  12345

Sort in random

[edit]

The GNU implementation has a-R --random-sort option based on hashing; this is not a full random shuffle because it will sort identical lines together. A true random sort is provided by the Unix utilityshuf.

Sort by version

[edit]

The GNU implementation has a-V --version-sort option which is a natural sort of (version) numbers within text. Two text strings that are to be compared are split into blocks of letters and blocks of digits. Blocks of letters are compared alpha-numerically, and blocks of digits are compared numerically (i.e., skipping leading zeros, more digits means larger, otherwise the leftmost digits that differ determine the result). Blocks are compared left-to-right and the first non-equal block in that loop decides which text is larger. This happens to work for IP addresses, Debian package version strings and similar tasks where numbers of variable length are embedded in strings.

See also

[edit]

References

[edit]
  1. ^absort(1) – Linux UserManual – User Commands from Manned.org
  2. ^"Multics Commands".www.multicians.org.
  3. ^McIlroy, M. D. (1987).A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986(PDF) (Technical report). CSTR. Bell Labs. 139.
  4. ^sort – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  5. ^"Sort invocation (GNU Coreutils 9.8)".
  6. ^"NAME — GNU Parallel 20250922 documentation".www.gnu.org.
  7. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM. Retrieved2020-09-05.
  8. ^"MSX-DOS2 Tools User's Manual - MSX-DOS2 TOOLS ユーザーズマニュアル". April 1, 1993 – via Internet Archive.
  9. ^"ParallelSliceMut in rayon::slice - Rust".docs.rs.
  10. ^"The GNU Bash Reference Manual, for Bash, Version 4.2: Section 3.1.2.4 ANSI-C Quoting". Free Software Foundation, Inc. 28 December 2010. Retrieved1 February 2013.Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
  11. ^Fowler, Glenn S.;Korn, David G.; Vo, Kiem-Phong."KornShell FAQ".Archived from the original on 2013-05-27. Retrieved3 March 2015.The $'...' string literal syntax was added to ksh93 to solve the problem of entering special characters in scripts. It uses ANSI-C rules to translate the string between the '...'.

Further reading

[edit]

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Processes
User environment
Text processing
Shell builtins
Networking
Searching
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Retrieved from "https://en.wikipedia.org/w/index.php?title=Sort_(Unix)&oldid=1322362393"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp