info
-
sort
sorts the contents of a text file, line by line -
rules
-
number before letter
-
alphabet
-
lowercase before uppercase
-
demo
-
simple sort
-
test data
$ cat data.txt apples oranges pears kiwis bananas
-
sort lines in this file alphabetically
$ sort data.txt
-
redirect
output$ sort data.txt > output.txt # same as $ sort -o output.txt data.txt
-
sorting in reverse order
# using `-r` flag $ sort -r data.txt
-
-
handling mixed-case data
-
test data
$ cat data.txt a b A B b c D d C
-
simple sort
$ sort data.txt a A b b B c C d D
-
enforce bytewise sorting
$ export LC_ALL=C $ sort data.txt A B C D a b b c d # performing a `case-insensitive bytewise` sort $ sort -f data.txt A a B b b C c D d
-
-
checking for sorted order
$ sort -c data.txt
-
sorting multiple files using the output of
find
$ find . -name "data?.txt" -print0 | sort --files0-from=-
-
comparing only selected fields of data
-
based on names
# 1. test data $ cat data.txt 01 Joe 02 Marie 03 Albert 04 Dave # 2. simple sort $ sort data.txt 01 Joe 02 Marie 03 Albert 04 Dave # 3. `k` stands for `key` # -k pos1,pos2 $ sort -k 2 data.txt
-
ignore and specify where to stop
# 1. test data $ cat data.txt 01 Joe Sr.Designer 02 Marie Jr.Developer 03 Albert Jr.Designer 04 Dave Sr.Developer # 2. sort by field 3 $ sort -k 3 data.txt 03 Albert Jr.Designer 02 Marie Jr.Developer 01 Joe Sr.Designer 04 Dave Sr.Developer # 3. ignore the first three characters of the third field $ sort -k 3.3 data.txt 01 Joe Sr.Designer 03 Albert Jr.Designer 02 Marie Jr.Developer 04 Dave Sr.Developer # 4. sort based on only the third-through-fifth characters of the third field $ sort -k 3.3,3.5 data.txt
-
-
using sort and join together
-
test data
$ cat file1.txt 3 tomato 1 onion 4 beet 2 pepper $ cat file2.txt 4 orange 3 apple 1 mango 2 grapefruit
-
sort
these two files andjoin
them$ join <(sort file1.txt) <(sort file2.txt) 1 onion mango 2 pepper grapefruit 3 tomato apple 4 beet orange
-
-
-
# words of the form `$'string'` are treated specially # so `$'\t'` is `horizontal tab` $ sort -t$'\t' -k3 -nr data.txt
-