Sie sind auf Seite 1von 36

About sort

Sorts the lines in a text file.


Syntax
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
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# ! ... ! "DE#.
-h, --human-
numeric-sort
Compare human readable numbers (e.g., $!"$, $#$$.
-n, --numeric-sort Compare according to string numerical value.
-%, --random-sort Sort by random hash of keys.
--random-
source&FILE
%et random bytes from FILE.
-r, --re'erse &everse the result of comparisons.
--sort&WORD
Sort according to WORD' general(numeric -g, human(numeric -h, month
-M, numeric -n, random -%, version -(.
-(, --'ersion-sort )atural sort of (version numbers within text.
Other Options
--batch-si)e&NMERGE *erge at most NMERGE inputs at once+ for more use temp files.
-c, --check,
--check&diagnose-first
Check for sorted input+ do not sort.
-, --check&quiet,
--check&silent
,ike -c, but do not report first bad line.
--compress-
program&PROG
Compress temporaries with PROG+ decompress them with PROG -d.
--debug
-nnotate the part of the line used to sort, and warn about
.uestionable usage to stderr.
--files*-from&F &ead input from the files specified by )/,(terminated names in file
F+ If F is ( then read names from standard input.
-k, --key&POS10,POS21
Start a key at POS1 (origin 2, end it at POS2 (default end of line.
See POS syntax below.
-m, --merge *erge already sorted files+ do not sort.
-o, --output&FILE 3rite result to FI,4 instead of standard output.
-s, --stable Stabili5e sort by disabling last(resort comparison.
-t, --field-separator&SEP /se SEP instead of non(blank to blank transition.
-+, --temporary-
directory&DIR
/se DIR for temporaries, not ,+M-D.% or /tmp+ multiple options
specify multiple directories.
--parallel&N Change the number of sorts run concurrently to N.
-u, --uni0ue
3ith -c, check for strict ordering+ without -c, output only the first of
an e.ual run.
-), --)ero-terminated 4nd lines with 6 byte, not newline.
--help 7isplay a help message, and exit.
--'ersion 7isplay version information, and exit.
POS takes the form F01C10OPS1, where F is the field number and C the character position in the
field+ both are origin #. If neither -t nor -b is in effect, characters in a field are counted from the
beginning of the preceding whitespace. OPS 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.
SI!E may be followed by the following multiplicative suffixes'
2 28 of memory
b 2
" 269: (default
...and so on for M, $, +, -, E, 3, 4.
3ith no FILE, or when FILE is a dash ($-$, sort reads from the standard input.
-lso note that the locale specified by the environment affects sort order+ set 56A55& to get
the traditional sort order that uses native byte values.
Examples
,et#s say you have a file, data1txt, which contains the following -SCII text'
apples
oranges
pears
kiwis
bananas
;o sort the lines in this file alphabetically, use the following command'
sort data.txt
...which will produce the following output'
apples
bananas
kiwis
oranges
pears
)ote that this command does not actually change the input file, data1txt. If you want to write the
output to a new file, output1txt, redirect the output like this'
sort data.txt > output.txt
...which will not display any output, but will create the file output1txt with the same sorted data
from the previous command. ;o check the output, use the cat command'
cat output.txt
...which will display the sorted data'
apples
bananas
kiwis
oranges
pears
<ou can also use the built(in sort option -o, which allows you to specify an output file'
sort -o output.txt data.txt
/sing the -o option is functionally the same as redirecting the output to a file+ neither one has an
advantage over the other.
Sorting .n %e'erse Order
<ou can perform a reverse(order sort using the -r flag. For example, the following command'
sort -r data.txt
...will produce the following output'
pears
oranges
kiwis
bananas
apples
7andling Mixed-ase Data
=ut what about situations where you have a mixture of upper( and lower(case letters at the
beginning of your lines> In cases like this, the behavior of sort can seem confusing, but really it
?ust needs some more information from you to sort the data the way you want. ,et#s take a closer
look.
,et#s say our input file data1txt contains the following data'
a
b
A
B
b
c
D
d

sorting this data without any options, like this'


sort data.txt
...will produce the following output'
a
A
b
b
B
c

d
D
-s you can see, it#s sorted alphabetically, with lowercase letters always appearing before
uppercase letters. ;his is $case(insensitive$ sorting, and this is the default for %)/ sort, which is
the version of sort used in %)/@,inux.
-t this point you might be asking yourself, well, if case(insensitive sorting is the default, then
what is the $-f@--ignore-case$ option for> ;he answer has to do with locali5ation settings and
bytewise sorting.
In brief, $locali5ation$ refers to what language the operating system uses, which at the most basic
level defines what characters it uses. 4ach letter in the system is represented in a certain order.
Changing the locale settings will affect what characters the operating system is using, and ((
most relevant to sorting (( what order they are encoded in. For an example, refer to the /nited
States 4nglish -SCII encoding table. -s you can see from the table, a capital - ($A$ is
character number AB, and lowercase a ($a$ is character number CD. So you might expect sort to
arrange its output so that capital letters come before lowercase letters.
7efining operating system locale is a sub?ect which goes beyond the scope of this document, but
for now, it will suffice to say that in order to achieve bytewise sorting, we need to set the
environment variable 56A55 to .
/nder the default ,inux shell, bash, we can accomplish this with the following command'
export !"A!!=
;his sets the environment variable 56A55 to the value , which will enforce bytewise sorting.
)ow if we run the command'
sort data.txt
...we will see the following output'
A
B

D
a
b
b
c
d
...and no", the -f@--ignore-case option has the following effect'
A
a
B
b
b

c
D
d
...performing a $case(insensitive bytewise$ sort.
3arning' if you are using the 8oin command in con?unction with sort, be aware that there is a
known incompatibility between the two programs (( unless you define the locale. If you are using
8oin and sort to process the same input, it is highly recommended that you set 56A55 to ,
which will standardi5e the locali5ation used by all programs.
hecking 9or Sorted Order
If you ?ust want to check to see if your input file is already sorted, use the -c option'
sort -c data.txt
If your data is unsorted, you will receive an informational message reporting the line number of
the first unsorted data, and what the unsorted data is'
sort# data.txt#$# disorder# A
Sorting Multiple 9iles :sing +he Output Of find
Ene useful way to sort data is to sort the input of multiple files, using the output of the find
command. ;he most reliable (and responsible way to accomplish this is to specify that find
produces a )/,(terminated file list as its output, and to pipe that output into sort using the
--files*-from option.
)ormally, find outputs one file on each line+ in other words, it inserts a line break after each
filename it outputs. For instance, let#s say we have three files named data#1txt, data!1txt, and
data;1txt. find can generate a list of these files using the following command'
find -name %data&.txt%
;his command uses the .uestion mark wildcard to match any file that has a single character after
the word $data$ in its name, ending in the extension $1txt$. It produces the following output'
.'data(.txt
.'data$.txt
.'data).txt
It would be nice if we could use this output to tell the sort command, $sort the data in any files
found by find as if they were all one big file.$ ;he problem with the standard find output is, even
though it#s easy for humans to read, it can cause problems for other programs that need to read it
in. ;his is because filenames can include non(standard characters, so in some cases, this format
will be read incorrectly by another program.
;he correct way to format find#s output to be used as a file list for another program is to use the
-print* option when running find. ;his terminates each filename with the )/, character (-SCII
character number 5ero, which is universally illegal to use in filenames. ;his makes things easier
for the program reading the file list, since it knows that any time it sees the )/, character, it can
be sure it#s at the end of a filename.
So, if we run the previous command with the -print* option at the end, like this'
find -name %data&.txt% -print0
...it will produce the following output'
.'data(.txt.'data$.txt.'data).txt
<ou can#t see it, but after each filename is a )/, character. ;his character is non(printable, so it
will not appear on your screen, but it#s there, and any programs you pipe this output to (sort, for
example will see them.
=e careful how you word the find commandF It#s important to specify -print* last+ find needs
this to be specified after the other options.
Ekay, but how do we tell sort to read this file list and sort the contents of all those files>
Ene way to do it is to pipe the find output to sort, specifying the --files*-from option in the sort
command, and specify the file as a dash ($-$, which will read from the standard input. Gere#s
what the command will look like'
find -name %data&.txt% -print0 * sort --files0-from=-
...and it will output the sorted data of any files located by find which matches the pattern
data<1txt, as if t$e% "ere all one file. ;his is a very powerful function of sort (( give it a tryF
omparing Only Selected 9ields Of Data
)ormally, sort decides how to sort lines based on the entire line' it compares every character
from the first character in a line, to the last one.
If, on the other hand, you want sort to compare a limited subset of your data, you can specify
which fields to compare using the -k option.
For instance, if you have an input file data1txt 3ith the following data'
0( +oe
0) ,arie
0$ Albert
0- Da.e
...and you sort it without any options, like this'
sort data.txt
...you will receive the following output'
0( +oe
0) ,arie
0$ Albert
0- Da.e
...as you can see, nothing was changed from the original data ordering, because of the numbers at
the beginning of the line (( which were already sortedF Gowever, if you want to sort based on the
names, you can use the following command'
sort -k ) data.txt
;his command will sort the se&ond field, and ignore the first. (;he $k$ in $-k$ stands for $key$ ((
we are defining the $sorting key$ used in the comparison.
Fields are defined as anything separated by whitespace+ in this case, an actual space character.
Eur command above will produce the following output'
0$ Albert
0- Da.e
0( +oe
0) ,arie
...which is sorted by the second field, listing the lines alphabetically by name, and ignoring the
numbers in the sorting process.
<ou can also specify a more complex -k option. ;he complete positional argument looks like
this'
-k POS1,POS2
...where POS1 is the starting field position, and POS2 is the ending field position. 4ach field
position, in turn, is defined as'
F.C
...where F is the field number and C is the character within that field to begin the sort
comparison.
So, let#s say our input file data1txt contains the following data'
0( +oe /r.Designer
0) ,arie +r.De.eloper
0$ Albert +r.Designer
0- Da.e /r.De.eloper
...we can sort by seniority if we specify the third field as the sort key'
sort -k $ data.txt
...this produces the following output'
0$ Albert +r.Designer
0) ,arie +r.De.eloper
0( +oe /r.Designer
0- Da.e /r.De.eloper
Er, we can ignore the first three characters of the third field, and sort solely based on title,
ignoring seniority'
sort -k $.$ data.txt
0( +oe /r.Designer
0$ Albert +r.Designer
0) ,arie +r.De.eloper
0- Da.e /r.De.eloper
3e can also specify where in the line to sto' comparing. If we sort based on onl% the third(
through(fifth characters of the third field of each line, like this'
sort -k $.$0$.1 data.txt
...sort will see onl% the same thing on every line' $1De$ ... and not$ing else. -s a result, sort will
not see any differences in the lines, and the sorted output will be the same as the original file'
0( +oe /r.Designer
0) ,arie +r.De.eloper
0$ Albert +r.Designer
0- Da.e /r.De.eloper
sort command is used to sort a file, arranging the records in a particular order. =y default, the
sort command sorts file assuming the contents are ascii. /sing options in sort command, it can
also be used to sort numerically. ,et us discuss it with some examples'
9ile =ith Ascii data>
,et us consider a file with the following contents'
2 cat file
3nix
!inux
/olaris
A45
!inux
6735
#1 sort simply sorts the file in alphabetical order'
2 sort file
A45
6735
!inux
!inux
/olaris
3nix
-ll records are sorted alphabetically.
!1 sort remo'es the duplicates using the (u option'
2 sort -u file
A45
6735
!inux
/olaris
3nix
;he duplicate #,inux# record got removed. #(u# option removes all the duplicate records in the file.
4ven if the file have had 26 #,inux# records, with (u option, only the first record is retained.
9ile =ith numbers '
,et us consider a file with numbers'
2 cat file
)0
(8
1
-8
)00
;1 ;he default sort #might# give incorrect result on a file containing numbers'
2 sort file
(8
)0
)00
-8
1
In the above result, 966 got placed immediately below 96, not at the end which is incorrect. ;his
is because the sort did -SCII sort. If the file had not contained #966#, the default sort would have
given proper result. Gowever, it is incorrect to sort a numerical file in this way since the sorting
logic is incorrect.
?1 +o sort a file numericallly'
2 sort -n file
1
(8
)0
-8
)00
(n option can sort the decimal numbers as well.
@1 sort file numerically in re'erse order'
2 sort -nr file
)00
-8
)0
(8
1
#r# option does a reverse sort.
Multiple 9iles '
,et us consider examples with multiple files, say file2 and file9, containing numbers'
2 cat file(
)0
(8
1
-8
)00
2 cat file)
)1
(9
1
-9
)00
A1 sort can sort multiple files as well.
2 sort -n file( file)
1
1
(9
(8
)0
)1
-9
-8
)00
)00
;he result of sort with multiple files will be a sorted and merged output of the multiple files.
B1 SortC merge and remo'e duplicates'
2 sort -nu file( file)
1
(9
(8
)0
)1
-9
-8
)00
(u option becomes more handy in case of multiple files. 3ith this, the output is now sorted,
merged and without duplicate records.
9iles =ith multiple fields and delimiter '
,et us consider a file with multiple fields'
2 cat file
!inux0)0
3nix0$0
A450)1
!inux0)1
/olaris0(0
67350(00
D1 sorting a file containing multiple fields'
2 sort file
A450)1
67350(00
!inux0)0
!inux0)1
/olaris0(0
3nix0$0
-s shown above, the file got sorted on the 2st field, by default.
E1 sort file on the basis of #st field'
2 sort -t%0% -k(0( file
A450)1
67350(00
!inux0)0
!inux0)1
/olaris0(0
3nix0$0
;his is being more explicit. #(t# option is used to provide the delimiter in case of files with
delimiter. #(k# is used to specify the keys on the basis of which the sorting has to be done. ;he
format of #(k# is ' #(km,n# where ( is the starting key and n is the ending key. In other words, sort
can be used to sort on a range of fields ?ust like how the group by in s.l does. In our case, since
the sorting is on the 2st field alone, we speciy #2,2#. Similarly, if the sorting is to be done on the
basis of first H fields, it will be' #(k 2,H#.
)ote' For a file which has fields delimited by a space or a tab, there is no need to specify the $(t$
option since the white space is the delimiter by default in sort.
#*1 sorting file on the basis of the !nd field'
2 sort -t%0% -k)0) file
/olaris0(0
67350(00
!inux0)0
A450)1
!inux0)1
3nix0$0
##1 sorting file on the basis of !nd field C numerically'
2 sort -t%0% -k)n0) file
/olaris0(0
!inux0)0
A450)1
!inux0)1
3nix0$0
67350(00
#!1 %emo'e duplicates from the file based on #st field'
2 sort -t%0% -k(0( -u file
A450)1
67350(00
!inux0)0
/olaris0(0
3nix0$0
;he duplicate ,inux record got removed. Ieep in mind, the command $sort (u file$ would not
have worked here becuase both the #,inux# records are not same, the values were different.
Gowever, in the above, sort is told to remove the duplicates based on the 2st key, and hence the
duplicate #,inux# record got removed. -ccording to sort, in case of a group of similar records,
except the first one, the rest are considered duplicate.
#;1 Sort the file numerically on the !nd field in re'erse order'
2 sort -t%0% -k)nr0) file
67350(00
3nix0$0
A450)1
!inux0)1
!inux0)0
/olaris0(0
#?1 sort the file alphabetically on the #st fieldC numerically on the !nd field'
2 sort -t%0% -k(0( -k)n0) file
A450)1
67350(00
!inux0)0
!inux0)1
/olaris0(0
3nix0$0
#@1 sort a file based on the #st and !nd fieldC and numerically on ;rd field on a file
containing B columns'
2 sort -t%0% -k(0) -k$n0$ file
( See more at' http'@@www.theunixschool.com@9629@6J@linux(sort(command(
examples.htmlKsthash.LM=Asx.b.dpuf
Sort Command Examples in Unix / Linux Tutorials
Sort command in unix or linux system is used to order the elements or text. Sort
command has the capability of sorting numerical values and strings. The sort
command can order the lines in a text fle.
The syntax of sort command is:
sort [options] filename
The options are:
-b # 4gnores leading spaces in eac: line
-d # 3ses dictionar; sort order. onisders onl; spaces and alp:anumeric
c:aracters in sorting
-f # 3ses case insensiti.e sorting.
-, # /orts based on mont:s. onsiders onl; first $ letters as mont:. <g# +A=0
><B
-n # 3ses numeric sorting
-? # /orts t:e input file randoml;.
-r # ?e.erse order sorting
-k # /orts file based on t:e data in t:e specified field positions.
-u # /uppresses duplicate lines
-t # input field separator
Sort Command Examples:
Before practicing the examples create the below two fles in your unix system:
> cat order.txt
3nix distributed 01 ser.er
!inux .irtual $ ser.er
3nix distributed 01 ser.er
Distributed processing @ s;stem
> cat delim"sort.txt
,a;da;*-
+anmon*(
Declast*()
1. Sorting lines of text
The default sort command uses alphabetical order (S!"" order# to sort the fle. "t
treats each line as a string and then sorts the lines.
> sort order.txt
Distributed processing @ s;stem
!inux .irtual $ ser.er
3nix distributed 01 ser.er
3nix distributed 01 ser.er
$. Sorting based on the feld positions.
%ou can specify the feld postions using the &' option of sort command. The sort
command uses the space or tab as the default delimiter. To sort based on the data
in the second feld( run the below command:
> sort -k) order.txt
3nix distributed 01 ser.er
3nix distributed 01 ser.er
Distributed processing @ s;stem
!inux .irtual $ ser.er
%ou can also pecify more than feld with ' option as a comma separated list. The
below command uses the second and fourth felds to sort the data.
> sort -k)0- order.txt
). *umeric sorting
"nstead of the default alphabetical sorting order( you can ma'e the sort command to
sort in numeric order using the &n option. This is shown below:
> sort -nk$ order.txt
!inux .irtual $ ser.er
3nix distributed 01 ser.er
3nix distributed 01 ser.er
Distributed processing @ s;stem
+. Sort in reverse order
By default( the sort command sorts the data in ascending order. %ou can change this
to descending order using the &r option.
> sort -nrk$ order.txt
Distributed processing @ s;stem
3nix distributed 01 ser.er
3nix distributed 01 ser.er
!inux .irtual $ ser.er
,. Suppressing duplicates or -rint only uni.ue values
%ou can produce only uni.ue values in the output using the & u option of the sort
command.
> sort -u order.txt
Distributed processing @ s;stem
!inux .irtual $ ser.er
3nix distributed 01 ser.er
nother way is piping the output of sort command to uni. command.
> sort order.txt * uniA
/. 0elimited fle input
"n the second( third and fourth examples we have sorted the data based on the feld
positions. 1ere the felds are separted by space or tab character. 2hat if the felds
are specifed by any other character3 "n such cases( we have to specify the input
delimiter with the &t option. n example is shown below:
> sort -tB*B -nrk) delim"sort.txt
Declast*()
,a;da;*-
+anmon*(
4. Sorting on months.
2e can sort the data in the monthwise using the &5 option of the sort command.
This is shown below:
> sort -, delim"sort.txt
+anmon*(
,a;da;*-
Declast*()
Treats the frst ) characters in the string as month and then sorts in months order.
Sort command is helpful to sort@order lines in text files. <ou can sort the data in text file and
display the output on the screen, or redirect it to a file. =ased on your re.uirement, sort provides
several command line options for sorting data in a text file.
Sort Command Syntax'
2 sort [-options]
For example, here is a test file'
2 cat test
CCC
sss
AAA
aaa
BBB
ddd
AAA
-nd, here is what you get when sort command is executed on this file without any option. It sorts
lines in test file and displays sorted output.
2 sort test
aaa
AAA
BBB
ddd
AAA
sss
CCC
1. Perform Numeric Sort using -n option
If we want to sort on numeric value, then we can use -n or )nu(eri&-sort option.
Create the following test file for this example'
2 cat test
)) CCC
$$ sss
(( AAA
DD aaa
11 BBB
;he following sort command sorts lines in test file on numeric value in first word of line and
displays sorted output.
2 sort -n test
(( AAA
)) CCC
$$ sss
11 BBB
DD aaa
2. Sort uman !eada"le Num"ers using -# option
If we want to sort on human readable numbers (e.g., 9I 2* 2%, then we can use -$ or )$u(an-
nu(eri&-sort option.
Create the following test file for this example'
2 cat test
)E
)F
(E
@G
(G
(F
),
;he following sort command sorts human readable numbers (i.e 2I N 2 ;housand, 2* N 2
*illion, 2% N 2 %iga, 2; N 2 ;era in test file and displays sorted output.
2 sort -: test
(E
)E
),
(F
)F
(G
@G
$. Sort %ont#s of an &ear using -% option
If we want to sort in the order of months of year, then we can use -M or )(ont$-sort option.
Create the following test file for this example'
2 cat test
sept
aug
Han
oct
apr
feb
mar((
;he following sort command sorts lines in test file as per month order. )ote, lines in file should
contain at least H character name of month name at start of line (e.g. ?an, feb, mar. If we will
give, ?a for Oanuary or au for -ugust, then sort command would not consider it as month name.
2 sort -, test
Han
feb
mar((
apr
aug
sept
oct
'. C#ec( if Content is )lread* Sorted using -c option
If we want to check data in text file is sorted or not, then we can use -& or )&$e&*+ )
&$e&*,diagnose-first option.
Create the following test file for this example'
2 cat test
)
1
(
@
;he following sort command checks whether text file data is sorted or not. If it is not, then it
shows first occurrence with line number and disordered value.
2 sort -c test
sort# test#$# disorder# (
+. !e,erse t#e -utput and C#ec( for Uni.ueness using -r and -u options
If we want to get sorted output in reverse order, then we can use -r or )re-erse option. If file
contains duplicate lines, then to get uni.ue lines in sorted output, P(uQ option can be used.
Create the following test file for this example'
2 cat test
1
)
)
(
-
-
;he following sort command sorts lines in test file in reverse order and displays sorted output.
2 sort -r test
1
-
-
)
)
(
;he following sort command sorts lines in test file in reverse order and removes duplicate lines
from sorted output.
2 sort -r -u test
1
-
)
(
/. Selecti,el* Sort t#e Content0 Customi1e delimiter0 2rite output to a 3le
using -(0 -t0 -o options
If we want to sort on the column or word position in lines of text file, then P(kQ option can be
used. If we each word in each line of file is separated by delimiter except RspaceS, then we can
specify delimiter using P(tQ option. 3e can get sorted output in any specified output file (using P(
oQ option instead of displaying output on standard output.
Create the following test file for this example'
2 cat test
aa aa CC
aa aa ff
aa aa tt
aa aa kk
;he following sort command sorts lines in test file on the Hrd word of each line and displays
sorted output.
2 sort -k$ test
aa aa ff
aa aa kk
aa aa tt
aa aa CC
2 cat test
aa*1a*CC
aa*)a*ff
aa*(a*tt
aa*$a*kk
Gere, several options are used altogether. In test file, words in each line are separated by
delimiter RTS. It sorts lines in test file on the 9nd word of each line on the basis of numeric value
and stores sorted output into specified output file.
2 sort -n -tB*B -k) test -o outfile
;he contents of output file are shown below.
2 cat outfile
aa*(a*tt
aa*)a*ff
aa*$a*kk
aa*1a*CC
6 dd your comment
7inux provides several powerful administrative tools and utilities which will help you
to manage your systems e8ectively. "f you don9t 'now what these tools are and how
to use them( you could be spending lot of time trying to perform even the basic
administrative tas's. The focus of this course is to help you understand system
administration tools( which will help you to become an e8ective 7inux system
administrator.
:et the 7inux Sysadmin !ourse *ow;
cat filename1txt
abc
cde
hi8
klm
kle
ble
;his will be sorted first with first char. 3hen it finds both the char same in this example klm and
kle start with same characters, so it tries to sort with third character which are different in those
two lines. ;he output of sort as below
sort filename.txt
abc
ble
cde
hi8
kle
klm
Sort command syntax
sort filename1txt
Example#> Sort a given file according to alpha(bates
sort filename1txt
Example!> I have a file with host names in third column, how can I sort them according to this
column(according to hostname>. /se (k for sorting according to column(kolumn
sort -k; filename1txt
;he above command will sort according to third column.
Example;>I want to sort @etc@passwd file according to home directories but my sort is not
working how can I sort them> =y default sort will take space/tabs as field separators. =ut in
/etc/pass=d file the field separator is > so we have to mention this one when sorting a file. ;his
can be done with (t option
sort -t> -kA /etc/pass=d
Example?> I want to sort according to number, suppose I want to sort @etc@passwd file according
to /I7, use (n option to do that. -gain sort will not understand numbers by default, we have to
use (n to make sure sort command understand it.
sort -n -t> -k; /etc/pass=d
Note> For example with out (n option sort will put 26 before H when it find this values, by
default it will sort only first numerical char.
Example@> Sort the file and reverse the order
sort -r filename1txt
ExampleA> Some times its re.uired to sort the file and display only uni. values.
sort -u filename
Note> though the values on other field are different this will not consider by (u option.
ExampleB> I want to sort a file according to my re.uirement and save it to a different file. /se (o
option to save the sorted output to a file.
sort -o temp1txt filename1txt
ExampleD> 7o you have file content with si5es like 26I, 96%, :B*, H9; etc. <ou can sort
accourding to human readable by using (h option. ;his option works &G4,B and above versions.
sort -h filename1txt
Similar to above example we can use (m for sorting according to month of the year.
sort -M filename1txt
ExampleE> Check if the file is alrady in sorted format or not by using (c option. ;his option will
show you what is the first occurence disorderd value.
sort -c filename1txt
<ou can now mix above options to get your sorting work done.
Fhat is a %egular expression<
- regular expression is a concept of matching a pattern in a given string.
Fhich commands/programming languages support regular expressions<
vi, tr, rename, grep, sed, awk, perl, python etc.
Gasic %egular Expressions
Gasic regular expressions> ;his set includes very basic set of regular expressions which do not
re.uire any options to execute. ;his set of regular expressions are developed long time back.

H Iaret/-o=er symbol to match a starting at the beginning of line1
, I+o match end of the line
J I* or more occurrence of pre'ious character1
1 I+o match any character
KL I%ange of character
KHcharL Inegate of occurrence of a character set
M=ordN IActual =ord finding
IEscape character
,ets start with our &egexp with examples, so that we can understand it better.
H %egular Expression
Example #> Find all the files in a given directory
ls -l O grep H-
-s you are aware that the first character in ls (l output, - is for regular files and d for directories
in a given folder. ,et us see what U( indicates. ;he U symbol is for matching line starting, U(
indicates what ever lines starts with (, ?ust display them. 3hich indicates a regular file in
,inux@/nix.
If we want to find all the directories in a folder use grep Ud option along ls (l as shown below
ls -l O grep Hd
Gow about character files and block files>
ls -l O grep Hc
ls -l O grep Hb
3e can even find the lines which are commented using U operator with below example
grep PHQR filename
Gow about finding lines in a file which starts with RabcS
grep PHabcR filename
3e can have number of examples with this U option.
, %egular Expression
Example !> *atch all the files which ends with sh
ls -l O grep sh,
-s V indicates end of the line, the above command will list all the files whose names end with sh.
how about finding lines in a file which ends with dead
grep Pdead,R filename
Gow about finding empty lines in a file>
grep PH,R filename
J %egular Expression
Example ;> *atch all files which have a word twt, twet, tweet etc in the file name.
ls -l O grep Pt=eJtR
Gow about searching for apple word which was spelled wrong in a given file where apple is
misspelled as ale, aple, appple, apppple, apppppple etc. ;o find all patterns
grep PapJleR filename
&eaders should observe that the above pattern will match even ale word as W indicates 6 or more
of previous character occurrence.
1 %egular Expression
Example ?> Filter a file which contains any single character between t and t in a file name.
ls -l O grep Pt1tR
Gere . will match any single character. It can match tat, tHt, t.t, tXt etc any single character
between t and t letters.
Gow about finding all the file names which starts with a and end with x using regular
expressions>
ls -l O grep Pa1JxR
;he above .W indicates any number of characters
Note> .W in this combination . indicates any character and it repeated(W 6 or more number of
times.
Suppose you have files as..
a=x
a=ex
a=eex
a=asdfx
a;@dfetrx
etc.. it will find all the files@folders which start with a and ends with x in our example.
KL S0uare braces/Grackets %egular Expression
Example @> Find all the files which contains a number in the file name between a and x
ls -l O grep PaK*-ELxR
;his will find all the files which is
a6xsdf
asda2xsdfas
..
..
asdfdsaraCxsdf
etc.
So where ever it finds a number it will try to match that number.
Some of the range operator examples for you.
0a(51 Y*atchSs any single char between a to 5.
0-(Z1 Y*atchSs any single char between a to 5.
06(C1 Y*atchSs any single char between 6 to C.
0a(5-(Z6(C1 Y *atchSs any single character either a to 5 or - to Z or 6 to C
0F[KV8U1 \ *atchSs any F or [ or K or V or 8 or U character.
<ou ?ust have to think what you want match and keep those character in the braces@=rackets.
KHcharL %egular Expression
ExampleA> *atch all the file names except a or b or c in its filenames
ls O grep PKHabcLP
;his will give output all the file names except files which contain a or b or c.
M=ordN %egular expression
ExampleB> Search for a word abc, for example I should not get abcxy5 or readabc in my output.
grep PMabcNR filename
Escape %egular Expression
Example D> Find files which contain 0 in its name, as 0 is a special charter we have to escape it
grep SKS filename
or
grep TKKLP filename
Note> If you observe 01 is used to negate the meaning of 0 regular expressions, so if you want to
find any specail char keep them in 01 so that it will not be treated as special char.
)ote' )o need to use (4 to use these regular expressions with grep. 3e have egrep and fgrep
which are e.ual to Pgrep -EQ. I suggest you ?ust concentrate on grep to complete your work,
donSt go for other commands if grep is there to resolve your issues. Stay tuned to our next post
on %egular expressions1
.nter'al %egular expressions
;hese are used to mention no of character@character set reputation info. )ote that interval regular
expression and extended reg re.uire (4 option with grep
Note> In order to use this set of regular expressions you have to us (4 with grep command and (r
option with sed commands

UnV In occurrence of pre'ious character
UnCmV I n to m times occurrence of pre'ious character
UmC V Im or more occurrence of pre'ious character1
Example #> Find all the file names which contain PtQ and t repeats for H times consecutively.
ls -l O grep -E PtU;VR
(4 option is used to extend regexp understanding for grep.
Example !> Find all the file names which contain l letter in filename with 2 occurrence to H
occurrence consecutively.
ls -l O grep -E PlU#C;VR
Example ;> Find all the file names which contains k letter B and more in a file name.
ls -l | grep -E 'k{5,}'
;his is bit tricky, let me explain this. -ctually we given a range i.e B to infinity(Oust given only
comma after B.

Extended regular expressions
;hese regular expressions extend the regular expression features.
Note>.n order to use this set of regular expressions you ha'e to us -E =ith grep command
and -r option =ith sed commands
W Ione more occurrence of pre'ious character
O X or optionC =e can specify either a character to present in the pattern1
< X * or one occurrence of pre'ious character
YZ X grouping of character set1
Example #> Find all the files which contains f letter, one more occurrences.
ls -l O grep -E PfWR
Example !> Find all the files which may contain a or b in its file name
ls -l O grep -E PaObR
Example ;> Find all the files which may contain t or 2 occurrence of t in filename
ls -l O grep -E Pt<R
for example i have below files
test
best
see
do
my grep command will list test, best files as output.
)ote' *y grep output contain all these files though see and do files do not contain t, the is
because we given > which will search for 6 or 2 occurrence of previous character. See and do
contains 6 tSs in its name, so it will find these files too.
Example ?> Find all the files which contains ab in the se.uence
ls -l O grep -E PYabZR
;his will give all the files which contains ab in the file name conse.uently.
Llease stay tuned to our next article on grep command and how to use it.
Sort a 3le in alp#a"etical order
2 cat phonebook
/mit:0 Brett 111--$)(
Doe0 +o:n 111-()$-
Doe0 +ane 111-$)(-
A.er;0 or; 111--($)
>ogart;0 /uCie 111-)$(-
2 sort phonebook
A.er;0 or; 111--($)
Doe0 +ane 111-$)(-
Doe0 +o:n 111-()$-
>ogart;0 /uCie 111-)$(-
/mit:0 Brett 111--$)(
Sort "* num"er
;he -n option makes the program sort according to numerical value'
2 du 'bin'I * sort -n
- 'bin'domainname
)- 'bin'ls
(0) 'bin's:
$0- 'bin'cs:
Sort t#e current director* "* 3le si1e
2 ls -s * sort -n
8@ =o.(.txt
()9 "arc:"backup.lst
()9 "arc:"backup.lst.tmp
(D09 =,J=
Columns or 3elds
In old versions of sort, the K( option made the program sort using the second column of data (K)
for the third, etc.. ;his is deprecated, and instead the -k option can be used to do the same thing
(note' $-k )$ for the second column'
2 cat zipcode
Adam ()$-1
Bob $-1@D
+oe 1@D98
/am -1@D9
Lend; )$-1@

2 sort -k )n zipcode
Adam ()$-1
Lend; )$-1@
Bob $-1@D
/am -1@D9
+oe 1@D98
Sort on multiple 3elds
;he -k m0n option lets you sort on a key that is potentially composed of multiple fields (start at
column m, end at column n'
2 cat quota
fred )000
bob (000
an (000
c:ad (000
don (100
eric 1000
2 sort -k)0) -k(0( quota
an (000
bob (000
c:ad (000
don (100
fred )000
eric 1000
Gere the first sort is done using column 9. -k)0) specifies sorting on the key starting and ending
with column 9. If -k) is used instead, the sort key would begin at column 9 and extend to the end
of the line, spanning all the fields in between. ;he n stands for #numeric ordering#. -k(0( dictates
breaking ties using the value in column 2, sorting alphabetically by default. )ote that bob, an and
chad have the same .uota and are sorted alphabetically in the final output.
Sorting a pipe delimited 3le
2 sort -tB*B -k) zipcode
Adam*()$-1
Lend;*)$-1@
Bob*$-1@D
/am*-1@D9
+oe*1@D98
Sorting a ta" delimited 3le
Sorting a file with tab separated values re.uires a tab character to be specified as the column
delimiter. ;his illustration uses the shell#s dollar(.uote notation
021091
to specify the tab as a C
escape se.uence.
2 sort -k)0) -t 2BMtB p:onebook
Doe0 +o:n 111-()$-
>ogart;0 /uCie111-)$(-
Doe0 +ane 111-$)(-
A.er;0 or; 111--($)
/mit:0 Brett 111--$)(
Sort in re,erse
;he -r option ?ust reverses the order of the sort'
2 sort -rk )n zipcode
+oe 1@D98
/am -1@D9
Bob $-1@D
Lend; )$-1@
Adam ()$-1
2hile going through an article on 7inux text processing commands( " came across
7inux sort command. " found this command interesting enough to read more about
it and try out some of it<s examples. So here in this article( "<ll share my
understanding on 7inux sort command through some examples. Before =umping on
to examples( here is an excerpt of information from the man page of sort command:
*5>
sort & sort lines of text fles
S%*?-S"S
sort @?-T"?*A... @B"7>A... sort @?-T"?*A... &&flesC&fromDB
0>S!E"-T"?*
2rite sorted concatenation of all B"7>(s# to standard output.
So we see that the main purpose of this command is to produce a sorted output.

5inux sort command examples

1. ) "asic example
The very frst input that " tried consisted of some random alphabets.
1ere is what " tried:
2 sort
b
C
a
w
s
nd here is the output :
a
b
s
w
C
So we see that the output produced was in sorted form.

2. Sort num"ers
"n the following example( " flled a text fle (sort.txt# with some random numbers.
2 cat sort.txt
9
)
@
(
1
$
Then " used the sort command with sort.txt as input fle to the command.
2 sort sort.txt
(
)
$
1
@
9
So we see that sorted list of numbers was produced in output.

$. Sorting 4ords
"n this example( the sort.txt fle is flled with some words.
2 cat sort.txt
3E
Australia
=ewCealand
BraCil
America
*ow( this fle is given as input to the sort command:
2 sort sort.txt
America
Australia
BraCil
=ewCealand
3E
So we see that words were sorted according to dictionary ordering. >ven the words
beginning with same alphabet were sorted according to succeeding alphabets.

'. Use sort to directl* 4rite data in sorted manner
This command can be used to write unsorted input data to a fle directly in sorted
manner.
1ere is how this can be done :
2 sort > sort.txt
8
6ello
-
L:;
9
B;e
fter the above operation( let<s chec' the fle contents :
2 cat sort.txt
-
9
8
B;e
6ello
L:;
So the output suggests that the input was frst sorted and then written to fle.

+. 2rite sorted concatenation of all input 3les to standard output
"f more that one fle is provided as input( the sort command produces a sorted
concatenation on stdout.
1ere is an example:
2 cat sort(.txt
D
-
8
(
2 cat sort).txt
9
1
@
)
1ere is the output :
2 sort sort(.txt sort).txt
(
)
-
1
@
D
9
8
So we see that a sorted concatenation was produced in output.

/. 2rite result of sort in a 3le
The output of sort command can be written to a fle by using &o option.
1ere is how it<s done :
2 sort -o sort.txt
-
8
)
9
(
*ow let<s chec' the fle :
2 cat sort.txt
(
)
-
9
8
So we see that the output was actually written to the fle whose name was supplied
as input to sort through &o option.

5. Sort mont#s
There is an interesting option &5 through which the month names can be sorted.
1ere is an example :
2 sort -, > sort.txt
D<
+A=
><B
*ow( let<s chec' the fle contents for output :
2 cat sort.txt
+A=
><B
D<
So we see that sort command actually sorted the month names.

6. Sort #uman reada"le num"ers
nother interesting option &h is provided by sort command through which human
readable numbers.
1ere is an example :
2 sort -: > sort.txt
)F
(E
$,
*ow( let<s chec' the fle for output:
2 cat sort.txt
(E
$,
)F
So we see that the numbers were sorted.

7. Produce re,erse sorted results
Fsing &r option provided by sort command( the results can be produced in reverse
order.
2 sort -: -r > sort.txt
)F
(E
$,
1ere is the output of fle :
2 cat sort.txt
)F
$,
(E
So we see that this time the sorting results were written in reverse sorted order.

18. Compare according to string numerical ,alue
This can be done using &* option.
1ere is the input :
2 cat > sort.txt
D mangoes
- oranges
8 grapes
( apple
1ere is the output :
2 sort -n sort.txt
( apple
- oranges
D mangoes
8 grapes

Das könnte Ihnen auch gefallen