Linux --- sort命令

在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

[xxxxxxxxxxx shell]$ sort -nu cc
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 進行正常的對比排序的。 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章