sort
Linux sort 命令用于將文本檔案內容加以排序,
sort 可針對文本檔案的內容,以行為單位來排序,sort可針對文本檔案的內容,以行為單位來排序,sort命令將每一行作為一個單位進行比較,比較原則是從首字符向后,依次按ASCII碼值進行比較,最后將他們按一定的順序進行輸出,sort命令經常可以與ls、cat等命令結合使用,將查詢到的結果按照我們要求進行排序,
語法
sort [-bcdfimMnr][-o<輸出檔案>][-t<分隔字符>][+<起始欄位>-<結束欄位>][--help][--verison][檔案][-k field1[,field2]]
引數說明
選項:
排序類選項:
-b, --ignore-leading-blanks 忽略行首的空格
-d, --dictionary-order 只處理空格,英文字母和數字
-f, --ignore-case 小寫字母視為大寫字母
-g, --general-numeric-sort 按照數字大小比較
-i, --ignore-nonprinting 只考慮可列印的字符
-M, --month-sort 按月份排序 (其他 < 'JAN' < ... < 'DEC')
-h, --human-numeric-sort 按可讀性更好的數字顯示方式排序(1K,2G)
-n, --numeric-sort 按照字串中的數字比較
-R, --random-sort 隨機排序,但是將相同的鍵分組
--random-source=FILE 從 FILE 中獲得隨機位元組
-r, --reverse 反向排序
--sort=WORD 根據 WORD 排序,可選項是:-g|h|M|n|R|V
-V, --version-sort 根據版本號數字排序
其他選項:
--batch-size=NMERGE 一次最多合并 NMERGE 輸入,更多使用臨時檔案
-c, --check, --check=diagnose-first 檢查是否排序
-C, --check=quiet, --check=silent 類似 -c, 不輸出報錯
--compress-program=PROG 使用 PROG 壓縮,使用 PROG -d 解壓縮
--debug 注釋用于排序的行部分,并警告對 stderr 的可疑使用
--files0-from=F 從檔案 F 中以空結尾的名稱指定的檔案中讀取輸入
-k, --key=KEYDEF 使用鍵排序,位置和型別由 KEYDEF 指定
-m, --merge 將排好序的檔案合并
-o, --output=FILE 把排序結果輸出到 FILE
-s, --stable 穩定比較
-S, --buffer-size=SIZE 使用 SIZE 作為主緩沖區大小
-t, --field-separator=SEP 指定 SEP 為分隔符
-T, --temporary-directory=DIR 使用 DIR 作為臨時檔案
--parallel=N 將同時運行的排序數設定為 N
-u, --unique 去重
-z, --zero-terminated 行分隔符是空,不是換行
--help 幫助檔案
--version 版本資訊
KEYDEF 是 F[.C][OPTS][,F[.C][OPTS]] 表示開始和停止位置,其中 F 是欄位編號,C 是欄位中的字符位置;兩者都是從 1 開始,停止位置默認為行尾,如果 -t 和 -b 均無效,則欄位中的字符從前一個空格的開頭開始計數,OPTS 是一個或多個單字母排序選項 [bdfgiMhnRrV],它覆寫該鍵的全域排序選項,如果沒有給出鍵,則使用整行作為鍵,使用 --debug 來診斷不正確的密鑰使用,SIZE 可以加后綴:% b K M G T P E Z Y
用法
-
默認按照字母排序
[root@honey-master tmp] cat sort.txt four three for two two six [root@honey-master tmp] sort sort.txt for four six three two two -
按照數字大小排序
[root@honey-master tmp] cat sort.txt 717 17 71 1 1 7 1.5 [root@honey-master tmp] sort sort.txt 1 1 1.5 浮點數也可以排序 17 7 71 717 -
去重后排序
[root@honey-master tmp] sort -u sort.txt 1 1.5 17 7 71 717 -
去重后降序排序
[root@honey-master tmp] sort -ur sort.txt 717 71 7 17 1.5 1 -
檔案夾大小排序
[root@honey-master tmp] du -sh * | sort -hr 96K hsperfdata_root 92K chromium-ym2SRD 88K chromium-6OMI0k 32K systemd-private-5a76db78a7624ffe8c521b35a8f39be6-elasticsearch-0.service-MRQ20O 4.0K sort.txt 0 pymp-yl2enfzo -
使用du命令查看/usr/share下的檔案排序,取前10個結果
[root@honey-master tmp] du -s /usr/share/* | head 732 /usr/share/aclocal 156 /usr/share/aclocal-1.16 18712 /usr/share/adobe 360 /usr/share/alsa 96 /usr/share/anaconda 100 /usr/share/appdata 56 /usr/share/applications 380 /usr/share/asciidoc 160 /usr/share/audit 1336 /usr/share/augeas這里的管head默認輸出前10行的資料,后面也可以加引數進行改變,比如

使用sort排序之后:
[root@honey-master tmp] du -s /usr/share/* | head | sort -nr 18712 /usr/share/adobe 1336 /usr/share/augeas 732 /usr/share/aclocal 380 /usr/share/asciidoc 360 /usr/share/alsa 160 /usr/share/audit 156 /usr/share/aclocal-1.16 100 /usr/share/appdata 96 /usr/share/anaconda 56 /usr/share/applications -
使用 ll 列印 /opt 下邊的檔案,取前 10 個結果,用sort 排序,用 k 選項指定第幾個欄位
[root@honey-master opt] ll | sort -nk 1| head -r--r--r--. 1 root root 166062080 May 30 22:06 ffmpeg-git-amd64-static.tar -rw-r--r--. 1 root root 70159813 May 30 22:13 kafka_2.11-2.4.1.tgz -rw-r--r--. 1 root root 630905856 May 30 22:09 asciicast2mp4.tar -rw-r--r--. 1 root root 777374720 May 30 22:09 singlefile.tar.gz -rw-r--r--. 1 root root 874286592 May 30 22:08 logstash.tar.gz -rwxr-xr-x. 1 root root 56806961 May 30 22:12 cerebro-0.9.2.tgz drwx--x--x. 4 root root 28 May 30 22:09 containerd drwxr-xr-x. 2 root root 29 May 30 22:32 nta drwxr-xr-x. 2 root root 62 May 30 22:46 msf drwxr-xr-x. 2 root root 87 May 30 22:13 kafka.cli.test [root@honey-master opt] ll | sort -nk 2| head -r--r--r--. 1 root root 166062080 May 30 22:06 ffmpeg-git-amd64-static.tar -rw-r--r--. 1 root root 70159813 May 30 22:13 kafka_2.11-2.4.1.tgz -rw-r--r--. 1 root root 630905856 May 30 22:09 asciicast2mp4.tar -rw-r--r--. 1 root root 777374720 May 30 22:09 singlefile.tar.gz -rw-r--r--. 1 root root 874286592 May 30 22:08 logstash.tar.gz -rwxr-xr-x. 1 root root 56806961 May 30 22:12 cerebro-0.9.2.tgz drwxr-xr-x. 2 root root 29 May 30 22:32 nta drwxr-xr-x. 2 root root 62 May 30 22:46 msf drwxr-xr-x. 2 root root 87 May 30 22:13 kafka.cli.test drwxr-xr-x. 2 root root 88 May 30 22:46 pvp-strike [root@honey-master opt] ll | sort -nk 5| head total 2515244 drwxr-xr-x. 3 root root 17 May 30 22:07 python2.7.10-customized drwxr-xr-x. 3 root root 17 May 30 22:07 python3.6.10-customized drwxr-xr-x. 3 root root 17 Sep 18 2020 redis drwxr-xr-x. 3 root root 18 May 30 22:08 honeypot-web drwx--x--x. 4 root root 28 May 30 22:09 containerd drwxr-xr-x. 2 root root 29 May 30 22:32 nta drwxr-xr-x. 5 501 games 57 Jun 18 2020 cerebro-0.9.2 drwxr-xr-x. 2 root root 62 May 30 22:46 msf drwxr-xr-x. 6 root root 70 May 30 22:13 kafka-manager-1.3.3.23 [root@honey-master opt] ll | sort -nk 7| head total 2515244 drwxr-xr-x. 3 root root 17 Sep 18 2020 redis drwxr-xr-x. 5 501 games 57 Jun 18 2020 cerebro-0.9.2 drwxr-xr-x. 10 postgres postgres 4096 Feb 20 2014 zookeeper -r--r--r--. 1 root root 166062080 May 30 22:06 ffmpeg-git-amd64-static.tar -rw-r--r--. 1 root root 70159813 May 30 22:13 kafka_2.11-2.4.1.tgz -rw-r--r--. 1 root root 630905856 May 30 22:09 asciicast2mp4.tar -rw-r--r--. 1 root root 777374720 May 30 22:09 singlefile.tar.gz -rw-r--r--. 1 root root 874286592 May 30 22:08 logstash.tar.gz -rwxr-xr-x. 1 root root 56806961 May 30 22:12 cerebro-0.9.2.tgz -
對 /etc/passwd 檔案內容進行排序,設定分隔符為:,根據第1個欄位進行排序:
[root@honey-master opt] cat /etc/passwd | sort -t ":" -k 1 | head adm:x:3:4:adm:/var/adm:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin chrony:x:990:986::/var/lib/chrony:/sbin/nologin clamscan:x:986:981:Clamav scanner user:/:/sbin/nologin clamupdate:x:987:983:Clamav database update user:/var/lib/clamav:/sbin/nologin clevis:x:995:991:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin cockpit-ws:x:993:989:User for cockpit web service:/nonexisting:/sbin/nologin cockpit-wsinstance:x:992:988:User for cockpit-ws instances:/nonexisting:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin -
將排序結果輸出到檔案
[root@honey-master opt] sort -t ":" -k 3 -nr /etc/passwd | head > /tmp/out.log [root@honey-master opt] cat /tmp/out.log nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin postgres:x:1000:1000::/home/postgres:/bin/bash systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin clevis:x:995:991:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin setroubleshoot:x:994:990::/var/lib/setroubleshoot:/sbin/nologin cockpit-ws:x:993:989:User for cockpit web service:/nonexisting:/sbin/nologin cockpit-wsinstance:x:992:988:User for cockpit-ws instances:/nonexisting:/sbin/nologin-o 引數:這個不支持部分排序操作,需要將所有排序之后寫入一個檔案,也可以是源檔案,不如 > 靈活
[root@honey-master tmp] sort -t ":" -k 3 -nr passwd -o passwd.txt [root@honey-master tmp] ls passwd.txt passwd -
多組輸出
[root@honey-master tmp] cat test Alice 98 02/20/2021 Alice 97 01/31/2021 Alice 92 03/01/2020 Alice 95 07/11/2020 Bob 99 12/23/2021 Bob 91 02/25/2021 Bob 98 01/30/2020 Chris 98 02/19/2021 Chris 98 02/05/2020 Chris 94 02/08/2021 Chris 92 02/10/2021 Chris 93 02/13/2021指定先以第1個欄位排序,再以第2個欄位,且指定型別為數字:
[root@honey-master tmp] sort -k 1 -k 2n test Alice 92 03/01/2020 Alice 95 07/11/2020 Alice 97 01/31/2021 Alice 98 02/20/2021 Bob 91 02/25/2021 Bob 98 01/30/2020 Bob 99 12/23/2021 Chris 92 02/10/2021 Chris 93 02/13/2021 Chris 94 02/08/2021 Chris 98 02/05/2020 Chris 98 02/19/2021指定按順序以第3個欄位的第7個、第1個、第4個字符排序:是按照年份的第一位,月份的第一位,以及日期的第一位排序
[root@honey-master tmp] sort -k 3.7 -k 3.1 -k 3.4 test Bob 98 01/30/2020 Chris 98 02/05/2020 Alice 92 03/01/2020 Alice 95 07/11/2020 Alice 97 01/31/2021 Chris 94 02/08/2021 Chris 92 02/10/2021 Chris 93 02/13/2021 Chris 98 02/19/2021 Alice 98 02/20/2021 Bob 91 02/25/2021 Bob 99 12/23/2021 -
-k M,N :M的意思是,從哪個域開始作為比較的依據;N截止到哪個域比較結束,M,N是一個(域的)范圍,如果這個M,N結合-u使用,那么-u的比較范圍就是M域和N域之間的這段內容,如果數字N被省略,那么-u比較的范圍就是從M域開始,一直到每行的最后一個域的最后一個字符,
首先說-u的作用是整行都完全重合,才會去重,其他時候不會去重

單列名字并不會去重
如果配合上-k引數,-t引數默認為" ",且-k只有一個引數的時候,默認作用域是從第一個引數到末尾,所以后面的不重復,這一行也不會去重
但如果引數有兩個,比如
-k 1,1,那么作用域就只限于第一個部分,此時就可以去重了[root@honey-master tmp] sort -u -k 1 test Alice 92 03/01/2020 Alice 95 07/11/2020 Alice 97 01/31/2021 Alice 98 02/20/2021 Bob 91 02/25/2021 Bob 98 01/30/2020 Bob 99 12/23/2021 Chris 92 02/10/2021 Chris 93 02/13/2021 Chris 94 02/08/2021 Chris 98 02/05/2020 Chris 98 02/19/2021 [root@honey-master tmp] sort -u -k 1,1 test Alice 98 02/20/2021 Bob 99 12/23/2021 Chris 98 02/19/2021
本文來自博客園,作者:ivanlee717,轉載請注明原文鏈接:https://www.cnblogs.com/ivanlee717/p/16329733.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483513.html
標籤:Linux
上一篇:如何避免CodeIgniter中的單個查詢中的多個聯接
下一篇:Mac 睡眠喚醒 不睡眠 問題
