By default, sort assumes that the fields are just words separated by blanks
Syntax : sort <flags> <sort fields> <file name>
The most common flags are as follows:
Options | Descriptions |
-n | Sort numerically, Ignore blanks and tabs |
-r | Reverse the order of sort |
-f | Sort upper and lowercase together |
+x | Ignore first x fields when sorting |
Specify the sort keys like this:
+m Start at the first character of the m+1th field.
-n end at the last character of the nth field (if -N omitted, assume the end of the line).
-t delimeter keys
-k1,2 field1 in primary key,filed2 is secondary key
Examples :
File name : example_sort.dat
g hi 123 liead
a bc 987 asd
d ef 873 lpoae
m no 873 apoie
j kl 234 ppoasd
sort the file ascending order
$ sort example_sort.dat
a bc 987 asd
d ef 873 lpoae
g hi 123 liead
j kl 234 ppoasd
m no 873 apoie
numeric sort the file using field3
$ sort -n -k3 example_sort.dat
g hi 123 liead
j kl 234 ppoasd
d ef 873 lpoae
m no 873 apoie
a bc 987 asd
tab delimiter [ctrl+v+TAB] sort the file using field2
$ sort -t" " -k2 example_sort.dat
a bc 987 asd
d ef 873 lpoae
g hi 123 liead
j kl 234 ppoasd
m no 873 apoie
reverse sort
$ sort -r example_sort.dat
m no 873 apoie
j kl 234 ppoasd
g hi 123 liead
d ef 873 lpoae
a bc 987 asd
sorted datas stored into another file
$ sort example_sort.dat > example.sorted
$ cat example.sorted
a bc 987 asd
d ef 873 lpoae
g hi 123 liead
j kl 234 ppoasd
m no 873 apoie
cut field3 and apply numeric sort
$ cut -d" " -f3 example_sort.dat |sort -n
123
234
873
873
987
cut field3 and apply numeric sort & eliminate duplicate values
$ cut -d" " -f3 example_sort.dat |sort -n|uniq -u
123
234
873
987
No comments:
Post a Comment