Linux中sort、uniq、cut、wc命令详解
sort
sort 命令對 File 參數(shù)指定的文件中的行排序,并將結(jié)果寫到標(biāo)準(zhǔn)輸出。如果 File 參數(shù)指定多個(gè)文件,那么 sort 命令將這些文件連接起來,并當(dāng)作一個(gè)文件進(jìn)行排序。
sort語法[root@www ~]# sort [-fbMnrtuk] [file or stdin]
選項(xiàng)與參數(shù):-f??:忽略大小寫的差異,例如?A?與?a?視為編碼相同;-b??:忽略最前面的空格符部分;-M??:以月份的名字來排序,例如?JAN,?DEC?等等的排序方法;-n??:使用『純數(shù)字』進(jìn)行排序(默認(rèn)是以文字型態(tài)來排序的);-r??:反向排序;-u??:就是?uniq?,相同的數(shù)據(jù)中,僅出現(xiàn)一行代表;-t??:分隔符,默認(rèn)是用?[tab]?鍵來分隔;-k??:以那個(gè)區(qū)間?(field)?來進(jìn)行排序的意思對/etc/passwd 的賬號進(jìn)行排序[root@www?~]#?cat?/etc/passwd?|?sortadm:x:3:4:adm:/var/adm:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin
sort 是默認(rèn)以第一個(gè)數(shù)據(jù)來排序,而且默認(rèn)是以字符串形式來排序,所以由字母 a 開始升序排序。
?
/etc/passwd 內(nèi)容是以 : 來分隔的,我想以第三欄來排序,該如何
[root@www?~]#?cat?/etc/passwd?|?sort?-t?':'?-k?3root:x:0:0:root:/root:/bin/bash uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin默認(rèn)是以字符串來排序的,如果想要使用數(shù)字排序:
cat?/etc/passwd?|?sort?-t?':'?-k?3nroot:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/sh默認(rèn)是升序排序,如果要倒序排序,如下
cat?/etc/passwd?|?sort?-t?':'?-k?3nr nobody:x:65534:65534:nobody:/nonexistent:/bin/shntp:x:106:113::/home/ntp:/bin/falsemessagebus:x:105:109::/var/run/dbus:/bin/falsesshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin?
如果要對/etc/passwd,先以第六個(gè)域的第2個(gè)字符到第4個(gè)字符進(jìn)行正向排序,再基于第一個(gè)域進(jìn)行反向排序。
cat?/etc/passwd?|??sort?-t':'?-k?6.2,6.4?-k?1r?????? sync:x:4:65534:sync:/bin:/bin/syncproxy:x:13:13:proxy:/bin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/sh?
查看/etc/passwd有多少個(gè)shell:對/etc/passwd的第七個(gè)域進(jìn)行排序,然后去重:
cat?/etc/passwd?|??sort?-t':'?-k?7?-u root:x:0:0:root:/root:/bin/bash syslog:x:101:102::/home/syslog:/bin/falsedaemon:x:1:1:daemon:/usr/sbin:/bin/shsync:x:4:65534:sync:/bin:/bin/syncsshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin?
uniq
?uniq命令可以去除排序過的文件中的重復(fù)行,因此uniq經(jīng)常和sort合用。也就是說,為了使uniq起作用,所有的重復(fù)行必須是相鄰的。
uniq語法
[root@www?~]#?uniq?[-icu] 選項(xiàng)與參數(shù):-i???:忽略大小寫字符的不同;-c??:進(jìn)行計(jì)數(shù)-u??:只顯示唯一的行?
testfile的內(nèi)容如下cat testfile
hello world friend hello world hello直接刪除未經(jīng)排序的文件,將會發(fā)現(xiàn)沒有任何行被刪除#uniq testfile ?
hello world friend hello world hello排序文件,默認(rèn)是去重
#cat?words?|?sort?|uniqfriend hello world?
排序之后刪除了重復(fù)行,同時(shí)在行首位置輸出該行重復(fù)的次數(shù)
#sort?testfile?|?uniq?-c1?friend3?hello2?world?
僅顯示存在重復(fù)的行,并在行首顯示該行重復(fù)的次數(shù)
#sort?testfile?|?uniq?-dc3?hello2?world?
僅顯示不重復(fù)的行
sort?testfile?|?uniq?-u friend?
cut
cut命令可以從一個(gè)文本文件或者文本流中提取文本列。
cut語法
[root@www?~]#?cut?-d'分隔字符'?-f?fields?<==用于有特定分隔字符 [root@www?~]#?cut?-c?字符區(qū)間????????????<==用于排列整齊的信息 選項(xiàng)與參數(shù):-d??:后面接分隔字符。與?-f?一起使用;-f??:依據(jù)?-d?的分隔字符將一段信息分割成為數(shù)段,用?-f?取出第幾段的意思;-c??:以字符?(characters)?的單位取出固定字符區(qū)間;?
PATH 變量如下
[root@www?~]#?echo?$PATH/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games #?1?|?2???????|?3???|?4???????|?5????????????|?6????????????|?7?
將 PATH 變量取出,我要找出第五個(gè)路徑。
#echo?$PATH?|?cut?-d?':'?-f?5/usr/local/bin?
將 PATH 變量取出,我要找出第三和第五個(gè)路徑。
#echo?$PATH?|?cut?-d?':'?-f?3,5/sbin:/usr/local/bin?
將 PATH 變量取出,我要找出第三到最后一個(gè)路徑。
echo?$PATH?|?cut?-d?':'?-f?3-/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games?
將 PATH 變量取出,我要找出第一到第三個(gè)路徑。
#?$PATH?|??-d??-f?-/bin:/usr/bin:/sbin:??
將 PATH 變量取出,我要找出第一到第三,還有第五個(gè)路徑。
?$PATH?|??-d??-f?-,/bin:/usr/bin:/sbin:/usr/local/bin?
實(shí)用例子:只顯示/etc/passwd的用戶和shell
#cat?/etc/passwd?|?cut?-d?':'?-f?1,7?root:/bin/bash daemon:/bin/shbin:/bin/sh?
?wc
統(tǒng)計(jì)文件里面有多少單詞,多少行,多少字符。
wc語法
[root@www?~]#?wc?[-lwm] 選項(xiàng)與參數(shù):-l??:僅列出行;-w??:僅列出多少字(英文單字);-m??:多少字符;?
默認(rèn)使用wc統(tǒng)計(jì)/etc/passwd
#wc?/etc/passwd40???45?1719?/etc/passwd40是行數(shù),45是單詞數(shù),1719是字節(jié)數(shù)
?
wc的命令比較簡單使用,每個(gè)參數(shù)使用如下:#wc -l /etc/passwd ? #統(tǒng)計(jì)行數(shù),在對記錄數(shù)時(shí),很常用40 /etc/passwd ? ? ? #表示系統(tǒng)有40個(gè)賬戶#wc -w /etc/passwd ?#統(tǒng)計(jì)單詞出現(xiàn)次數(shù)45 /etc/passwd#wc -m /etc/passwd ?#統(tǒng)計(jì)文件的字節(jié)數(shù)1719
轉(zhuǎn)載于:https://blog.51cto.com/doublelinux/1910733
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Linux中sort、uniq、cut、wc命令详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle 启动例程 STARTUP参
- 下一篇: Linux 下监控系统几个重要组件