|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| News | Recommended Links | Sorting algorithms | Solaris sort options | GNU sort options | Sorting key definitions | Examples | Exit Status |
| uniq | cut | AWK | cat | grep | Perl sort | Humor | Etc |
The Unix sort command sorts ASCII files. It is not always wise to use it as options are obscure and specifying delimiters and sorting keys can be a nightmare. In a sense Unix sort can serve as a good example of programming of utilities in early 70th of the last century :-).
For small sets a good alternative is Perl sort function which is more modern and more flexible. I would say that in case of complex keys and for small files Perl-based solution is almost always superior. But for large files (for example logs) Unix sort command might be the only tool that is able to handle jobs.
Unix sort can sort pretty large files. I successfully sorted proxy log files with the size over 10G using Solaris sort on a pretty old V210 with 80G 10RPM drives and single 1.34GHz CPU.
Lines need not to have the same length or the number of fields to be sorted successfully. All you need is the presence of the key.
The input can be from files or the standard input. In case of files Unix sort can accept as input one or several files. In the latter case all input files are merged. As for output, there is always a single file to be written -- sorted sequence of all input records:
The output can be a file or the standard output.
Data can be sorted in several ways:
By default fields in each record (line of input) are delimited by blanks, but you can specify a different delimiter using option -t. You can also sort character columns (see below).
Selection of sorting keys is similar to selection of fields in cut and is extremely obscure. We have a separate page addressing this issue in depth (see UNIX sort keys definition for more details). Be careful and always test your sorting keys on small sample before sorting a large file. As a rule of thump you can assume that no specification works from the first time. So testing it on a small sample is of paramount importance.
|
We have a separate page addressing sort keys definition for more details. |
The most common mistake is to forget to use -n option for sorting numeric fields. Also specifying delimiter (option -t) with an unquoted character after it can be a source of problems; it's better to use single quotes around the character that you plan to use as a delimiter. for example -t ':'
|
The most common mistake is to forget to use -n option for sorting numeric fields |
Here is a standard example of usage of the sort utility, sorting /etc/passwd file (user database) by UID (the third colon-separated field in the passwd file structure):
sort -t ':' +2 /etc/passwd # incorrect result, the field is numeric
sort -n -t ':' +2 /etc/passwd # order of the numbers is now correctsort -t ':' -k 3,3n /etc/passwd
sort -t ':' +2 -3n /etc/passwd
sort -n -t ':' -k 3,3 /etc/passwd
See Sorting key definitions and Examples for more details. Generally you will be surprised how often the result is not what you want due to the obscurity of the definitions
|
Be careful and always test your sorting
keys on a small sample before sorting the whole file. |
By default sort sorts the file in ascending order using the entire line as a sorting key. Please note that a lot of WEB resources interpret this sort utility behavior incorrectly (most often they state that by default sorting is performed on the first key).
The three most important options of Unix soft are -n (numeric keys sorting), +n (sorting using n-th field, counting from zero) and -r (sort in reverse). For example:
| Sort the entire lines as a key: | sort |
| Sort in numeric order: | sort -n |
| Sort on n+1 st field (old style key definition): | sort +n |
Comparisons are based on one or more sort keys extracted from each line of input. Again, please remember that by default, there is one sort key, the entire input line.
Lines are ordered according to the collating sequence of the current locale. By changing locale you can change the behavior of the sort.
In Solaris there are two variants of sort: System V version and BSD version. Both have identical options:
The sort command can (and should) be used in pipes or have its output redirected as desired. Here are some practically important examples that illustrates using of this utility (for more examples please look into our sort examples collection page):
sort file | less
In simple cases cut can be used instead of AWK. For example the following example couts distinc visitors from HTTP logs (assuming this is the first field in the logs):
cat http.log | cut -d " " -f
1 | sort | uniq -c | sort -nr
sort -n file | cat -n
This can be useful if you want to count the number of lines in which
the first entry is in a given range: simply subtract the line numbers
corresponding to the beginning and end of the range.
As I mentioned about by default the sort command uses entire lines as a key. It compares the characters starting with the first, until non-matching character or the end of the shortest line. Leading blanks (spaces and tabs) are considered valid characters to compare. Thus a line beginning with a space precedes a line beginning with the letter A. If you do not want this effect you need to delete leading spaces beforehand.
Multiple sort keys may be used on the same command line. If two lines have the same value in the same field, sort uses the next set of sort keys to resolve the equal comparison. For example,
sort +4 -5 +1 -2 infile
means to sort based on field 5 (+4 -5). If two lines have the same value in field 5, sort those two lines based on field 2 (+1 -2).
Beside sorting Unix sort is useful for merging files (option -m). It can also checked whether the file is sorted or not (option -c). It can also suppress duplicates (option -u):
In case Unix sort does not produce the required results you might want to look into Perl built-in function. If it is too slow more memory can be specified on invocation.
The following list describes the options and their arguments that may be used to control how sort functions.
sort: disorder: This line not in sorted order.
| +pos1 | Specifies the beginning position of the input line used for field comparison. If pos1 is not specified then comparison begins at the beginning of the line. The pos1 position has the notation of f.c. The f specifies the number of fields to skip. The c specifies the number of characters to skip. For example, 3.2 is interpreted as skip three fields and two characters before performing comparisons. Omitting the .c portion is equivalent to specifying .0. Field one is referred to as position 0. If f is set to 0 then character positions are used for comparison. |
| -pos2 | Specifies the ending position of the input line used for field comparison. If pos2 is not specified then comparison is done through the end of the line. The pos2 position has the notation of f.c. The f specifies to compare through field f. The c specifies the number of characters to compare through after field f. For example, -4.3 is interpreted as compare through three characters after the end of field four. Omitting the .c portion is equivalent to specifying .0. |
| -b | Ignores leading blanks when using the restricted sort key positions (+pos1 and -pos2). If the -b option is placed before the first +pos1 argument, then it applies to all +pos1 arguments. The -b option can be attached to each pos string to affect only that field. |
| -t c | Use character c as the field separator. Multiple adjacent c's in the records are interpreted empty fields surrounded by separators. If you need to use multiple characters as a separator you need to convert record so that they are represented by a single character using sed or AWK. You can use nonprintable characters as a separator to ensure their uniqueness. |
|
|||||||
Here's a convenient way of finding those space hogs in your home directory (can be any directory). For me, those large files are usually a result of mkfile event (testing purposes) and can be promptly deleted. Here's an example of its use.
#cd /export/home/esofthub
#ls -l | sort +4n | awk '{print $5 "\t" $9}'
Find recursively (a little awkward)
#ls -lR | sort +4n | awk '{print $5 "\t" $9}' | more
ps -ef | sortThis command pipeline sorts the output of the "ps -ef" command. Because no arguments are supplied to the sort command, the output is sorted in alphabetic order by the first column of the ps -ef output (i.e., the output is sorted alphabetically by username).
ls -al | sort +4n
This command performs a numeric sort on the fifth column of the "ls -al" output. This results in a file listing where the files are listed in ascending order, from smallest in size to largest in size.
ls -al | sort +4n | more
The same command as the previous, except the output is piped into the more command. This is useful when the output will not all fit on one screen.
ls -al | sort +4nr
This command reverses the order of the numeric sort, so files are listed in descending order of size, with the largest file listed first, and the smallest file listed last.
The output of du has been very informative, but it's difficult to scan a listing to ascertain the four or five largest directories, particularly as more and more directories and files are included in the output. The good news is that the Unix sort utility is just the tool we need to sidestep this problem.
# du -s * | sort -nr
One final concept and we're ready to move along. If you want to only see the five largest files or directories in a specific directory, all that you'd need to do is pipe the command sequence to head:
13984 Lynx
10464 IBM
3092 Gator
412 bin
196 DEMO
84 etcpasswd
76 CBO_MAIL
48 elance
36 CraigsList
16 Exchange
4 gettermsheet.sh
4 getstocks.sh
4 getmodemdriver.sh
4 buckaroo
4 browse.sh
4 badjoke.rot13
4 badjoke
0 gif.gif# du -s * | sort -nr | head -5 13984 Lynx 10464 IBM 3092 Gator 412 bin 196 DEMO
The ! command (pronounced "bang") creates a temporary file to be used with a program that requires a filename in its command line. This is useful with shells that don't support process substitution. For example, to diff two files after sorting them, you might do:
diff `! sort file1` `! sort file2`commer
commer is a shell script that uses comm to compare two sorted files; it processes comm's output to make it easier to read. (See article 11.9.)
[Overview] [List]lensort
lensort sorts lines from shortest to longest. (See article 22.7.)
[Overview] [List]namesort
The namesort program sorts a list of names by the last name. (See article 22.8.) See also namesort.pl.
[Overview] [List]namesort.pl
The namesort.pl script uses the Perl module Lingua::EN::NameParse to sort a list of names by the last name. (See article 22.8.) See also namesort.
[Overview] [List]
This command pipeline sorts the output of the "ps -ef" command. Because no arguments are supplied to the sort command, the output is sorted in alphabetic order by the first column of the ps -ef output (i.e., the output is sorted alphabetically by username).
ls -al | sort +4n
This command performs a numeric sort on the fifth column of the "ls -al" output. This results in a file listing where the files are listed in ascending order, from smallest in size to largest in size.
ls -al | sort +4n | more
The same command as the previous, except the output is piped into the more command. This is useful when the output will not all fit on one screen.
ls -al | sort +4nr
This command reverses the order of the numeric sort, so files are listed in descending order of size, with the largest file listed first, and the smallest file listed last.
Daniel Malaby dan at malaby.com
Thu Jul 14 08:02:47 GMT 2005
- Previous message: DHCP assigned unregistered IP address
- Next message: using -t option with unix sort ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi All, I am trying to sort a tab delimited file with sort. The problem I am having is with the -t option. I do not know how to pass a tab. Things I have tried: sort -t \t sort -t '\t' sort -t "\t" sort -t 0x09 sort -t '0x09' sort -t "0x09" sort -t ^I sort -t '^I' sort -t "^I" Any suggestions would be much appreciated. Thanks
Daniel Malaby dan at malaby.com Thu Jul 14 17:48:05 GMT 2005
Nelis Lamprecht wrote: > On 7/14/05, Nelis Lamprecht <nlamprecht at gmail.com> wrote: > >>On 7/14/05, Daniel Malaby <dan at malaby.com> wrote: >> >>>Hi All, >>> >>>I am trying to sort a tab delimited file with sort. The problem I am >>>having is with the -t option. I do not know how to pass a tab. >> >><snip> >> >>>sort -t \t >> >></snip> >> >>>Any suggestions would be much appreciated. >> >>remove the space between -t and \t and it should work > > > actually scratch that, it works either way. can you give a sample of the data ? > > Regards, > Nelis The sample data has 9 fields, I am trying to sort on the fifth field, here is what I have tried. sort -t\t +4 -5 -o test.txt sample.txt I did try removing the space and it did not work, I have also tried removing the -5. I think the spaces in the third field are confusing sort. BTW this is being done on a PC running FBSD 4.11 prerelease #1 Thanks for your help and suggestions. -------------- next part -------------- E002 19085 GENERAL DYNAMICS 5031802 E-GL/VX/B/R1.0 SFT CD, GL VXWORKS BOREALIS R1.0 06/30/05 1 $995.00 $995.00 E016 19096 TGA INGENIERIA Y ELECTRONICS S 5881-2 E-AD600729C501 ARGUS PMC,2 DVI 16MB PERCHAN USB A/V 12/01/05 30 $2,312.00 $69,360.00 E016 19096 TGA INGENIERIA Y ELECTRONICS S 5881-2 E-DDX/SO/R4.0 SFT CD, DDX SOL 2.6-9 BOREALIS R4.0 12/01/05 30 $74.00 $2,220.00 E016 19096 TGA INGENIERIA Y ELECTRONICS S 5881-2 E-VIN/SO/R1.0 SFT CD, VID CAP SOL 2.6-9 BOREALIS R1.0 12/01/05 30 $74.00 $2,220.00 E021 19093 GANYMED COMPUTER GMBH 7103879 E-AD90073913011 GARNET PMC RIO8 C2, REAR I/O 16MB 07/19/05 2 $1,848.00 $3,696.00 E024 19080 DRS LAUREL TECHNOLOGIES 94358 E-AC7007121115A ECLIPSE3 PMC, VGA 16MB Q70 08/18/05 1 $846.00 $846.00 E024 19080 DRS LAUREL TECHNOLOGIES 94358 E-AC7007121115A ECLIPSE3 PMC, VGA 16MB Q70 10/19/05 19 $846.00 $16,074.00 E024 19080 DRS LAUREL TECHNOLOGIES 94358 E-AC7007121115A ECLIPSE3 PMC, VGA 16MB Q70 09/20/05 2 $846.00 $1,692.00 E024 19080 DRS LAUREL TECHNOLOGIES 94358 E-AC7007121115A ECLIPSE3 PMC, VGA 16MB Q70 11/17/05 7 $846.00 $5,922.00
Let´s assume that we want to sort /etc/passwd using the geco field. To achieve this, we will use sort, the unix sorting tool
$ sort -t: +4 /etc/passwd murie:x:500:500:Manuel Muriel Cordero:/home/murie:/bin/bash practica:x:501:501:Usuario de practicas para Ksh:/home/practica:/bin/ksh wizard:x:502:502:Wizard para nethack:/home/wizard:/bin/bash root:x:0:0:root:/root:/bin/bashIt is very easy to see that the file has been sorted, but using the ASCII table order. If we don´t want to make a difference among capital letter, we can use:
$ sort -t: +4f /etc/passwd murie:x:500:500:Manuel Muriel Cordero:/home/murie:/bin/bash root:x:0:0:root:/root:/bin/bash practica:x:501:501:Usuario de practicas para Ksh:/home/practica:/bin/ksh wizard:x:502:502:Wizard para nethack:/home/wizard:/bin/bash-t is the option to select the field separator. +4 stands for the number of field to jump before ordering the lines, and f means to sort regardless of upper and lowercase.
A much more complicated sort can be achieved. For example, we can sort using the shell in a first step then sort using the geco:
$ sort -t: +6r +4f /etc/passwd practica:x:501:501:Usuario de practicas para Ksh:/home/practica:/bin/ksh murie:x:500:500:Manuel Muriel Cordero:/home/murie:/bin/bash root:x:0:0:root:/root:/bin/bash wizard:x:502:502:Wizard para nethack:/home/wizard:/bin/bashYou have a file with some people you lend money and the amount of money you gave them. Take ´deudas.txt´ as an example:
Son Goku:23450 Son Gohan:4570 Picolo:356700 Ranma 1/2:700If you want to know the first one to ´visit´, you need a sorted list.
Just type$ sort +1 deudas Ranma 1/2:700 Son Gohan:4570 Son Goku:23450 Picolo:356700which is not the desired result because the number of fields is not the same across the file. The solution is the ´n´ option:$ sort +1n deudas Picolo:356700 Son Goku:23450 Son Gohan:4570 Ranma 1/2:700Basic options for sort are
+n.m jumps over the first n fields and the next m characters before begin the sort
-n.m stops the sorting when arriving to the m-th character of the n-th fieldThe following are modification parameters:
-b jumps over leading whitespaces
-d dictionary sort (just using letters, numbers and whitespace)
-f ignores case distinction
-n sort numerically
-r reverse order
For example, suppose we want to list the distinct file owners in a directory. To do this, we must perform three discrete tasks:
1. We must list all files in the directory (ls –al)Using the pipe command, we can tie these three functions together into a single UNIX command, piping the output from one command as sending it as input to the next UNIX command:
2. We must parse this output and extract the file owner from the fourth column of the output. (awk ‘{ print $3 }’)
3. We must then take the list of file owners and remove duplicate entries (sort –u)
root> ls -al|awk '{ print $3 }'|sort -u
marion
oracle
root
% awk '{ print NF " " $0}' < out | sort -n | tail
From: Ed Schmollinger (schmolli@private)
Date: Thu Sep 16 2004 - 09:14:32 PDT
- Previous message: Marcus J. Ranum: "Re: [logs] Faster unix 'sort' replacement?"
- In reply to: Mike Blomgren: "[logs] Faster unix 'sort' replacement?"
- Next in thread: Mike Blomgren: "RE: [logs] Faster unix 'sort' replacement?"
- Reply: Mike Blomgren: "RE: [logs] Faster unix 'sort' replacement?"
- Reply: cadams@private: "Re: [logs] Faster unix 'sort' replacement?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
On Thu, Sep 16, 2004 at 12:33:12AM +0200, Mike Blomgren wrote: > I'm having trouble with 'sort' taking alot of cpu-time on a Solaris machine, > and I'm wondering if anyone knows of a replacement for the gnu 'sort' > command, which is faster and will compile on Solaris and preferably Linux > too? > > I'm using sort in the standard 'cat <file> | awk '{"compute..."}' | sort | > uniq -c | sort -n -r' type analysis. You can get rid of the multiple sorts/uniq thing by doing it all at once: --- CUT HERE --- #!/usr/bin/perl -wT use strict; my %msg = (); while (<>) { chomp; $msg{$_} = $msg{$_} ? $msg{$_} + 1 : 1; } for(sort { $msg{$a} <=> $msg{$b} } keys %msg) { print "$msg{$_}\t$_\n"; } --- CUT HERE --- I've found that for my datasets, the awk/sed stage is what constitues the bulk of the bottleneck. You may want to look at optimizing that part as well. -- Ed Schmollinger - schmolli@private
Russell Fulton r.fulton at auckland.ac.nz
Mon Sep 20 13:13:59 MDT 2004
- Previous message: [logs] Faster unix 'sort' replacement?
- Next message: [logs] Faster unix 'sort' replacement?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Fri, 2004-09-17 at 05:59, Mike Blomgren wrote: > Thanks for the tip - I'll have to try that one with perl doing the sort > instead of gnu sort. I have been somewhat reluctant to use perl since I find > it has a severe performance impact in some cases - but that may be related > to my regexp's and not the sorting. For a fact though, I do know that using > associative arrays is a good way to consume memory in a hurry. And thus > causing the os to start swapping memory to disk, which is not very > beneficial for speed, to say the least... If you are short of memory sort may be swapping stuff out to disk and hence your performance problems. It depends on the implementations but some sorts are smart enough to work out how much memory is really available and then do sort & merges with in this. This is much better than sorts that simply assume that virtual memory is endless and cause the OS to thrash madly but is much slower than doing the whole thing in memory. This will not show up as OS level swapping though, just as lots of disk activity during the sort. -- Russell Fulton, Information Security Officer, The University of Auckland New Zealand
Internal
External
docs.sun.com man pages section 1 User Commands Sun man page
Write sorted concatenation
of all FILE(s) to standard output.
Mandatory arguments to
long options are mandatory for short options
too. Ordering options:
-b, --ignore-leading-blanks
ignore leading blanks
-d, --dictionary-order
consider only blanks and alphanumeric characters
-f, --ignore-case
fold lower case to upper case characters
-g, --general-numeric-sort
compare according to general numerical value
-i, --ignore-nonprinting
consider only printable characters
-M, --month-sort
compare (unknown) < `JAN' < ... < `DEC'
-n, --numeric-sort
compare according to string numerical value
-r, --reverse
reverse the result of comparisons
Other options:
-c, --check
check whether input is sorted; do not sort
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS2 (origin
1)
-m, --merge
merge already sorted files; do not sort
-o, --output=FILE write
result to FILE instead of standard output
-s, --stable stabilize
sort by disabling last-resort comparison
-S, --buffer-size=SIZE
use SIZE for main memory buffer
-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
-T, --temporary-directory=DIR
use DIR for temporaries, not $TMPDIR or /tmp; multiple options
specify multiple directories
-u, --unique with
-c, check for strict ordering; without -c, output only the
first of an equal run
-z, --zero-terminated
end lines with 0 byte, not newline
--help display this help and exit
--version output
version information and exit
POS is F[.C][OPTS], where F is the
field number and C the character
position in the field.
OPTS is one or more single-letter ordering
options, which override global ordering
options for that key. If no
key is given, use the entire line as
the key.
SIZE may be followed by the following
multiplicative suffixes: % 1% of
memory, b 1, K 1024 (default), and
so on for M, G, T, P, E, Z, Y.
With no FILE, or when FILE is -, read
standard input.
*** WARNING *** The locale specified
by the environment affects sort
order. Set LC_ALL=C to get the
traditional sort order that uses native
byte values.
/usr/bin/sort
The notation:
A field comprises a maximal sequence of nonseparating characters and, in the absence of option -t, any preceding field separator.
The field_start portion of the keydef optionargument has the form:
field_number[.first_character]
Fields and characters within fields are numbered starting with 1. field_number and first_character, interpreted as positive decimal integers, specify the first character to be used as part of a sort key. If .first_character is omitted, it refers to the first character of the field.
The field_end portion of the keydef optionargument has the form:
field_number[.last_character]
The field_number is as described above for field_start. last_character, interpreted as a non-negative decimal integer, specifies the last character to be used as part of the sort key. If last_character evaluates to zero or .last_character is omitted, it refers to the last character of the field specified by field_number.
If the -b option or b type modifier is in effect, characters within a field are counted from the first non-blank character in the field. (This applies separately to first_character and last_character.)
[+pos1[-pos2]]
(obsolete). Provide functionality equivalent to the -k keydef option.
pos1 and pos2 each have the form m.n optionally followed by one or more of the flags bdfiMnr. A starting position specified by +m.n is interpreted to mean the n+1st character in the m+1st field. A missing .n means .0, indicating the first character of the m+1st field. If the b flag is in effect n is counted from the first non-blank in the m+1st field; +m.0b refers to the first nonblank character in the m+1st field.
A last position specified by -m.n is interpreted to mean the nth character (including separators) after the last character of the mth field. A missing .n means .0, indicating the last character of the mth field. If the b flag is in effect n is counted from the last leading blank in the m+1st field; -m.1b refers to the first non-blank in the m+1st field.
The fully specified +pos1 - pos2 form with type modifiers
T and U:
+w.xT -y.zU
is equivalent to:
anagram -- an interesting use of sort
% awk '{ print NF " " $0}' < out | sort -n | tail
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Created May 16, 2006. Last modified: September 15, 2008