標籤:
文章轉自 http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858385.html
sort
sort 命令對 File 參數指定的檔案中的行排序,並將結果寫到標準輸出。如果 File 參數指定多個檔案,那麼 sort 命令將這些檔案串連起來,併當作一個檔案進行排序。
sort文法
[[email protected] ~]# sort [-fbMnrtuk] [file or stdin]選項與參數:-f :忽略大小寫差異,例如 A 與 a 視為編碼相同;-b :忽略最前面的空格符部分;-M :以月份的名字來排序,例如 JAN, DEC 等等的排序方法;-n :使用『純數字』進行排序(預設是以文字型態來排序的);-r :反向排序;-u :就是 uniq ,相同的資料中,僅出現一行代表;-t :分隔字元,預設是用 [tab] 鍵來分隔;-k :以那個區間 (field) 來進行排序的意思
對/etc/passwd 的帳號進行排序
[[email protected] ~]# cat /etc/passwd | sortadm:x:3:4:adm:/var/adm:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
sort 是預設以第一個資料來排序,而且預設是以字串形式來排序,所以由字母 a 開始升序排序。
/etc/passwd 內容是以 : 來分隔的,我想以第三欄來排序,該如何
[[email protected] ~]# cat /etc/passwd | sort -t ‘:‘ -k 3root:x:0:0:root:/root:/bin/bashuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin
預設是以字串來排序的,如果想要使用數字排序:
cat /etc/passwd | sort -t ‘:‘ -k 3nroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/sh
預設是升序排序,如果要倒序排序,如下
cat /etc/passwd | sort -t ‘:‘ -k 3nrnobody: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,先以第六個域的第2個字元到第4個字元進行正向排序,再基於第一個域進行反向排序。
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有多少個shell:對/etc/passwd的第七個域進行排序,然後去重:
cat /etc/passwd | sort -t‘:‘ -k 7 -uroot:x:0:0:root:/root:/bin/bashsyslog: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命令可以去除排序過的檔案中的重複行,因此uniq經常和sort合用。也就是說,為了使uniq起作用,所有的重複行必須是相鄰的。
uniq文法
[[email protected] ~]# uniq [-icu]選項與參數:-i :忽略大小寫字元的不同;-c :進行計數-u :只顯示唯一的行
testfile的內容如下
cat testfilehelloworldfriendhelloworldhello
直接刪除未經排序的檔案,將會發現沒有任何行被刪除
#uniq testfile helloworldfriendhelloworldhello
排序檔案,預設是去重
#cat words | sort |uniqfriendhelloworld
排序之後刪除了重複行,同時在行首位置輸出該行重複的次數
#sort testfile | uniq -c1 friend3 hello2 world
僅顯示存在重複的行,並在行首顯示該行重複的次數
#sort testfile | uniq -dc3 hello2 world
僅顯示不重複的行
sort testfile | uniq -ufriend
cut
cut命令可以從一個文字檔或者文字資料流中提取文本列。
cut文法
[[email protected] ~]# cut -d‘分隔字元‘ -f fields <==用於有特定分隔字元[[email protected] ~]# cut -c 字元區間 <==用於排列整齊的資訊選項與參數:-d :後面接分隔字元。與 -f 一起使用;-f :依據 -d 的分隔字元將一段資訊分割成為數段,用 -f 取出第幾段的意思;-c :以字元 (characters) 的單位取出固定字元區間;
PATH 變數如下
[[email protected] ~]# echo $PATH/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games# 1 | 2 | 3 | 4 | 5 | 6 | 7
將 PATH 變數取出,我要找出第五個路徑。
#echo $PATH | cut -d ‘:‘ -f 5/usr/local/bin
將 PATH 變數取出,我要找出第三和第五個路徑。
#echo $PATH | cut -d ‘:‘ -f 3,5/sbin:/usr/local/bin
將 PATH 變數取出,我要找出第三到最後一個路徑。
echo $PATH | cut -d ‘:‘ -f 3-/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games
將 PATH 變數取出,我要找出第一到第三個路徑。
#echo $PATH | cut -d ‘:‘ -f 1-3/bin:/usr/bin:/sbin:
將 PATH 變數取出,我要找出第一到第三,還有第五個路徑。
echo $PATH | cut -d ‘:‘ -f 1-3,5/bin:/usr/bin:/sbin:/usr/local/bin
實用例子:只顯示/etc/passwd的使用者和shell
#cat /etc/passwd | cut -d ‘:‘ -f 1,7 root:/bin/bashdaemon:/bin/shbin:/bin/sh
wc
統計檔案裡面有多少單詞,多少行,多少字元。
wc文法
[[email protected] ~]# wc [-lwm]選項與參數:-l :僅列出行;-w :僅列出多少字(英文單字);-m :多少字元;
預設使用wc統計/etc/passwd
#wc /etc/passwd40 45 1719 /etc/passwd
40是行數,45是單詞數,1719是位元組數
wc的命令比較簡單使用,每個參數使用如下:
#wc -l /etc/passwd #統計行數,在對記錄數時,很常用/etc/passwd #表示系統有40個賬戶#wc -w /etc/passwd #統計單詞出現次數/etc/passwd#wc -m /etc/passwd #統計檔案的位元組數
linux sort,uniq,cut,wc.