sort命令詳解

 

 基礎命令學習目錄首頁html

 

原文連接:https://blog.csdn.net/shuanghujushi/article/details/51344215linux

在linux的只用過程當中,老是避免不了排序問題。好比,topN問題。linux提供了sort排序命令,支持經常使用的排序功能。shell

經常使用參數

sort命令支持不少參數,經常使用參數以下:ruby

短參數 長參數 說明
-n – number-sort 按字符串數值排序,與-g區別爲不轉爲浮點數
-g –general-number-sort 按通用數值排序,支持科學計數法
-f –ignore-case 忽略大小寫,默認大小寫字母不一樣
-k –key=POS1[,POS2] 排序從POS1開始,若指定POS2,則POS2結束,不然以pos1排序
-t –field-separator=SEP 指定列的分割符
-r –reverse 降序排序,默認爲升序
-h –human-numeric-sort 使用易讀性數字(例如: 2K 1G)
-u –unique 去除重複的行
-o –output=FILE 將輸出寫入文件

經常使用用法舉例

1.默認排序

默認狀況下,sort命令,以字母序進行文本排序。以下:bash

shuanghu@shuanghu:tmp$cat word.txt one two three four shuanghu@shuanghu:tmp$sort word.txt four one three two

2.數字排序

若是想對數字進行排序,可使用-n參數tcp

shuanghu@shuanghu:tmp$ cat num.txt 100 20 3 shuanghu@shuanghu:tmp$ sort num.txt -n 3 20 100

3.指定列排序

sort排序的時候,能夠按字段分割的數據進行排序。-t參數表示行的分割字符,-k表示第幾列。固然,能夠進行降序排序,-r參數能夠實現。
下面是對passwd文件,以冒號(:)進行分割,而後對第三列以數字方式進行降序排序。post

shuanghu@shuanghu:etc$ cat passwd daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin shuanghu@shuanghu:etc$ sort -t ':' -k 3 -nr passwd mail:x:8:8:mail:/var/mail:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin games:x:5:60:games:/usr/games:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync sys:x:3:3:sys:/dev:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

4.文件夾大小排序

在du的時候,加上-h可使用易讀性數字,好比2k,1g,3M這種。sort也支持-h參數。
好比,du一個文件夾下的目錄大小後,想以文件大小進行排序。因爲du -h的結果是3k,2M,1G這種,不能簡單的按數字排序。因此,可使用-h參數。具體以下:學習

shuanghu@shuanghu:tmp$ du -h 2.0G ./test2 4.0K ./test3 316M ./test 2.3G . shuanghu@shuanghu:tmp$ du -h |sort -hr 2.3G . 2.0G ./test2 316M ./test 4.0K ./test3

5.系統進程內存佔用排序

查看系統進程中,內存佔用最多的前5個進程信息ui

shuanghu@shuanghu:tmp$ ps aux|sort -gr -k 4|head -n 5 shuanghu 1740 15.7 4.6 1506764 189872 ? Sl 5月07 142:08 compiz root 1304 2.1 1.9 338928 80208 tty7 Ssl+ 5月07 19:29 /usr/bin/X -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch shuanghu 1933 0.0 1.1 1074520 46708 ? Sl 5月07 0:00 /usr/lib/evolution/evolution-calendar-factory shuanghu 1833 0.0 0.8 974900 34468 ? Sl 5月07 0:01 nautilus -n shuanghu 2111 0.0 0.6 655712 24920 ? Sl 5月07 0:16 gnome-terminal

 

6.對文件內容進行去重

若是文件內容有不少重複的,須要進行去重。sort也是支持的,能夠經過-u參數使用url

shuanghu@shuanghu:tmp$cat word.txt one two two three three three four four four shuanghu@shuanghu:tmp$sort -u word.txt four one three two

7.將sort輸出內容寫入文件

在shell中,通常將控制檯內容寫入文件,可使用重定向,但若是想把sort的排序內容寫回文件,則不能使用重定向。則須要-o參數。具體以下:

shuanghu@shuanghu:tmp$cat word.txt one two three four shuanghu@shuanghu:tmp$sort word.txt > word.txt shuanghu@shuanghu:tmp$cat word.txt #輸出爲空 shuanghu@shuanghu:tmp$sort word.txt -o word.txt shuanghu@shuanghu:tmp$sort -u word.txt four one three two
相關文章
相關標籤/搜索