在linux 系统中, man sort时参数解释信息如下:
[xxxxxxxx ~]$ man sort
SORT(1) User Commands SORT(1)
NAME
sort - sort lines of text files
SYNOPSIS
sort [OPTION]... [FILE]...
DESCRIPTION
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
在常用的命令中, 主要不使用任何参数,或者使用 -n -r -u
进行测试:
场景1:
创建文件cc
[migu_pps@LS-MGHJ-PPS-VM-102-84 shell]$ cat cc
8
9
0
01
08
123
12
33
44
18
91
[xxxxxxxxxx shell]$
在直接执行sort时,响应结果如下:
[xxxxxxxxx shell]$ sort cc
0
01
08
12
123
18
33
44
8
9
91
[xxxxxxxx shell]$ sort -n cc
0
01
08
8
9
12
18
33
44
91
123
[xxxxxxxx shell]$ sort -nr cc
123
91
44
33
18
12
9
8
08
01
0
0
01
8
9
12
18
33
44
91
123
[xxxxxxxxx shell]$
在只存在数字的文件里, 只输入sort 不带参数进行确认时,发现,排序规则是先根据第一个字符进行对比的,如果存在相同的,然后再进行对比第二个字符, 由于9 第一个就是9 ,大于123的1,所以123排在9前面
在进行-n 时, -n的意思是根据字符串数值进行比较, 所以即使前面是0,也会将08 8 前后排在一起,当进行 -u 去重操作时,携带参数 -nu 会发现将08去除
所以对于数值进行比较时, 需要添加-n 进行数值比较, -nu 也是能实现排序去重
场景2
对于存在字符串的参加测试
[xxxxxxxxxx shell]$ cat aa
aa
aaa
ca
nn
app
ppa
app
[xxxxxx shell]$ sort aa
aa
aaa
app
app
ca
nn
ppa
[xxxxxxxx shell]$ sort -n aa
aa
aaa
app
app
ca
nn
ppa
[xxxxxxxx shell]$ sort -nu aa
aa
[xxxxxxx shell]$
通过对比发现 sort 也是直接根据第一个字符进行对比排序的, sort 在字符串对比,添加 -n u 是没有任何意义的。 不过在单独使用-u的场景下,还时能完成去重的
[xxxxxxx shell]$ cat aa
aa
aaa
ca
nn
app
ppa
app
aa
aaa
[xxxxxxxxxx shell]$ sort -u aa
aa
aaa
app
ca
nn
ppa
[xxxxxxx shell]$
场景3:
对于数字组合形式的排序,是否正常, 常见场景已IP为例
[xxxxxxxx shell]$ cat bb
172.18.102.11
172.18.102.33
172.19.102.11
173.18.102.11
223.11.11.11
223.11.12.11
8.9.9.1
172.18.102.11
223.11.11.11
[xxxxxxxxx shell]$ sort bb
172.18.102.11
172.18.102.11
172.18.102.33
172.19.102.11
173.18.102.11
223.11.11.11
223.11.11.11
223.11.12.11
8.9.9.1
[xxxxxxxxx shell]$ sort -n bb
8.9.9.1
172.18.102.11
172.18.102.11
172.18.102.33
172.19.102.11
173.18.102.11
223.11.11.11
223.11.11.11
223.11.12.11
[xxxxxxxxxxx shell]$ sort -nu bb
8.9.9.1
172.18.102.11
172.19.102.11
173.18.102.11
223.11.11.11
[xxxxxxx shell]$ sort -u bb
172.18.102.11
172.18.102.33
172.19.102.11
173.18.102.11
223.11.11.11
223.11.12.11
8.9.9.1
[xxxxxxxxx shell]$
同样可以得出,直接进行-u是可以支持去重的, 直接排序,还是按照第一个字符进行判断,然后依次往下, 是可以使用-n 进行正常的对比排序的。