Softpanorama
May the source be with you, but remember the KISS principle ;-)

Contents Bulletin Scripting in shell and Perl Network troubleshooting History Humor

sort command

News Recommended Links Sorting algorithms Solaris sort options GNU sort options Sorting key definitions Examples Exit Status
uniq cut AWK cat grep Perl sort    
Sysadmin Horror Stories Sorting Algorithms Insertion sort Quicksort Mergesort Humor Random Findings 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 large files. I successfully sorted proxy log files with the size over 10G using Solaris 9 sort implementation 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. In case of single file sort can process file "in place".

Sorting can be controlled by several option:

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 correct

sort -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. You will be surprised how often the result is not what you want.

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 sort are

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):

  1. Sort the file and display the sorted file, one page at a time. (If you prefer the standard command "more", you can use this instead of "less". "less" is an enhanced version of "more" - for example, it allows you to move backwards and forwards in the file; to exit "less" , use "q".)

    sort file | less

  2. Read and sorts infile and display the result on standard output, which has been redirected to outfile:
    sort infile > outfile # sorts infile and writes the results to outfile
  3. Write sorted output directly to outfile.

    sort -o outfile infile # same as in 1, but using an option -o
  4. Read and sort infile "in place" writing to the same file

    sort -o infile infile # sort file "in place"
  5. Sort the file and pipe it to uniq command with the number of identical keys counter printed (-c option in uniq)

    sort infile | uniq -c
  6. Pipe the output of the who command to the input of the sort command:

    who | sort
  7. Classic "number of instances" analysis of log files:

    cat messages | awk '{"select the keywords"}' | sort | uniq -c | sort -nr

    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

  8. Sort the file, then prepend line numbers to each line (not many Unix adminitrators know that cat can be used to number lines):

    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 most important options

The following list describes the options and their arguments that may be used to control how sort functions.

Ordering Options

Old-style Sort Key Options

+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.

Top updates

Bulletin Latest Past week Past month
Google Search

Old News ;-)

[Jul 14, 2007] My SysAd Blog -- UNIX Sort Files by Their Filesizes

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

[Jul 14, 2007] Learn Unix The sort command

ps -ef | sort

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.

[Feb 15, 2007] UNIX Disk Usage Simplifying Analysis with sort

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
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

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:
# du -s * | sort -nr | head -5
13984  Lynx
10464  IBM
3092   Gator
412    bin
196    DEMO

[Feb 14, 2007] UNIX Power Tools, 3rd Edition Examples!

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]

[Feb 11, 2007] Learn Unix The sort command

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.

[Feb 10, 2007] using -t option with unix sort

Daniel Malaby dan at malaby.com
Thu Jul 14 08:02:47 GMT 2005
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

[Jan 30, 2007] lf131, UNIX Basics GNU file utilities

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/bash

It 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/bash

You 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:700

If 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:356700
which 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:700

Basic 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 field

The 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

[Jan 22, 2007] Oracle UNIX pipe command

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)
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)
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:

root> ls -al|awk '{ print $3 }'|sort -u

marion
oracle
root

[Jul 3, 2005] anagram -- an interesting use of sort (sorting letters)

% awk '{ print NF " " $0}' < out | sort -n | tail

[Sep 16 2004] loganalysis 2004-09 Re [logs] Faster unix 'sort' replacement

From: Ed Schmollinger (schmolli@private)
Date: Thu Sep 16 2004 - 09:14:32 PDT

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

[Sep 20, 2004] Faster unix 'sort' replacement

Russell Fulton r.fulton at auckland.ac.nz
Mon Sep 20 13:13:59 MDT 2004

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

Recommended Links

Softpanorama Top Visited

Softpanorama Recommended

Internal

External

GNU sort command options

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.

Solaris sort command options

The following options alter the default behavior:

/usr/bin/sort

-c
Check that the single input file is ordered as specified by the arguments and the collating sequence of the current locale. The exit code is set and no output is produced unless the file is out of sort.
/usr/xpg4/bin/sort
-c
Same as /usr/bin/sort except no output is produced under any circumstances.
-m
Merge only. The input files are assumed to be already sorted.
-u
Unique: suppress all but one in each set of lines having equal keys. If used with the -c option, check that there are no lines with duplicate keys in addition to checking that the input file is sorted.
-o output
Specify the name of an output file to be used instead of the standard output. This file can be the same as one of the input files.
-T directory
The directory argument is the name of a directory in which to place temporary files.
-y kmem
The amount of main memory initially used by sort. If this option is omitted, sort begins using a system default memory size, and continues to use more space as needed. If kmem is present, sort will start using that number of Kbytes of memory, unless the administrative minimum or maximum is exceeded, in which case the corresponding extremum will be used. Thus, -y 0 is guaranteed to start with minimum memory. -y with no kmem argument starts with maximum memory.
-z recsz
(obsolete). This option was used to prevent abnormal termination when lines longer than the system-dependent default buffer size are encountered. Because sort automatically allocates buffers large enough to hold the longest line, this option has no effect.
Ordering Options
The following options override the default ordering rules. When ordering options appear independent of any key field specifications, the requested field ordering rules are applied globally to all sort keys. When attached to a specific key (see Sort Key Options), the specified ordering options override all global ordering options for that key. In the obsolescent forms, if one or more of these options follows a +pos1 option, it will affect only the key field specified by that preceding option.
-d
``Dictionary'' order: only letters, digits, and blanks (spaces and tabs) are significant in comparisons.
-f
Fold lower-case letters into upper case.
-i
Ignore non-printable characters.
-M
Compare as months. The first three non-blank characters of the field are folded to upper case and compared. For example, in English the sorting order is "JAN" < "FEB" < ... < "DEC". Invalid fields compare low to "JAN". The - M option implies the -b option (see below).
-n
Restrict the sort key to an initial numeric string, consisting of optional blank characters, optional minus sign, and zero or more digits with an optional radix character and thousands separators (as defined in the current locale), which will be sorted by arithmetic value. An empty digit string is treated as zero. Leading zeros and signs on zeros do not affect ordering.
-r
Reverse the sense of comparisons.
Field Separator Options
The treatment of field separators can be altered using the following options:
-b
Ignore leading blank characters when determining the starting and ending positions of a restricted sort key. If the -b option is specified before the first sort key option, it is applied to all sort key options. Otherwise, the -b option can be attached independently to each -k field_start, field_end, or +pos1 or -pos2 option-argument (see below).
-t char
Use char as the field separator character. char is not considered to be part of a field (although it can be included in a sort key). Each occurrence of char is significant (for example, <char><char> delimits an empty field). If - t is not specified, blank characters are used as default field separators; each maximal non-empty sequence of blank characters that follows a nonblank character is a field separator.
Sort Key Options

Sort keys can be specified using the options:
-k keydef
The keydef argument is a restricted sort key field definition. The format of this definition is: -k field_start [ type ] [ ,field_end [ type ] ] where:
field_start and field_end
define a key field restricted to a portion of the line.
type
is a modifier from the list of characters bdfiMnr. The b modifier behaves like the -b option, but applies only to the field_start or field_end to which it is attached and characters within a field are counted from the first nonblank character in the field. (This applies separately to first_character and last_character.) The other modifiers behave like the corresponding options, but apply only to the key field to which they are attached. They 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 applies to either.
When there are multiple key fields, later keys are compared only after all earlier keys compare equal. Except when the -u option is specified, lines that otherwise compare equal are ordered as if none of the options -d, -f, -i, -n or - k were present (but with -r still in effect, if it was specified) and with all bytes in the lines significant to the comparison.

The notation:

-k field_start[type][,field_end[type]]
defines a key field that begins at field_start and ends at field_end inclusive, unless field_start falls beyond the end of the line or after field_end, in which case the key field is empty. A missing field_end means the last character of the line.

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:

undefined
(z==0 & U contains b & -t is present)
-k w+1.x+1T,y.0U
(z==0 otherwise)
-k w+1.x+1T,y+1.zU
(z > 0)
Implementations support at least nine occurrences of the sort keys (the -k option and obsolescent +pos1 and -pos2) which are significant in command line order. If no sort key is specified, a default sort key of the entire line is used.

Exit status

The following exit values are returned:

Random Findings

anagram -- an interesting use of sort

% awk '{ print NF " " $0}' < out | sort -n | tail




Etc

Society

Groupthink : Understanding Micromanagers and Control Freaks : Toxic Managers : BureaucraciesHarvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Two Party System as Polyarchy : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

Skeptical Finance : John Kenneth Galbraith : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotes : Oscar Wilde : Talleyrand : Somerset Maugham : War and Peace : Marcus Aurelius : Eric Hoffer : Kurt Vonnegut : Otto Von Bismarck : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Oscar Wilde : Bernard Shaw : Mark Twain Quotes

Bulletin:

Vol 26, No.1 (January, 2013) Object-Oriented Cult : Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks: The efficient markets hypothesis : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Vol 23, No.10 (October, 2011) An observation about corporate security departments : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

 

The Last but not Least


Copyright © 1996-2014 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. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine. This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting hosting of this site with different providers to distribute and speed up access. Currently there are two functional mirrors: softpanorama.info (the fastest) and softpanorama.net.

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: July 07, 2013