含義
正如cut的中文意思,cut的工做就是「剪」,具體來講是在文件負責剪切數據用的,它以每一行爲一個處理對象,與sed的機制是同樣的。python
剪切依據
cut命令主要是接受三個定位方法:linux
-b
-c
-f
語法格式shell
cut [-bn] [file] 或 cut [-c] [file] 或 cut [-df] [file]
使用說明
cut命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段寫至標準輸出。
若是不指定 File 參數,cut 命令將讀取標準輸入。必須指定 -b、-c 或 -f 標誌之一。
主要參數bash
-b
:以字節
爲單位進行分割。這些字節位置將忽略多字節字符邊界,除非也指定了 -n 標誌。-c
:以字符
爲單位進行分割。-d
:自定義分隔符,默認爲製表符。-f
:與-d
一塊兒使用,指定顯示哪一個區域。-n
:取消分割多字節字符。僅和 -b
標誌一塊兒使用。若是字符的最後一個字節落在由 -b 標誌的 List 參數指示的經常使用示例:
「字節」定位中,提取第1到第5和第10個字節,-b支持形如1-5的寫法,並且多個定位之間用逗號隔開
注意,cut命令若是使用了-b選項,那麼執行此命令時,cut會先把-b後面全部的定位進行從小到大排序,而後再提取。spa
示例1:提取第1到第5字節和第10個字節的內容code
[root@moli_linux1 ~]$ echo "abcde12345" | cut -b 1-5,10 abcde5
示例2:-3表示從第1個字節到第3個字節,3-表示從第3個字節到行尾對象
[root@moli_linux1 ~]$ echo "abcde54321" | cut -b -3 abc [root@moli_linux1 ~]$ echo "abcde54321" | cut -b 3- cde54321
示例3:多字節字符處理
1個空格算1個字節,而1個漢字算3個字節,所以漢字是多字節字符.遇到多字節字符可使用-c
選項,也可使用-b
選項,不過要結合-n
選項,-n
選項告訴cut命令不要將多字節字符拆開。排序
[root@moli_linux1 ~]$ cat cut_test2.txt 星期一aa 星期二bb 星期三cc [root@moli_linux1 ~]$ cut -b 3 cut_test2.txt # 由於沒加-n選項,所以將"星"字給拆分了.. [root@moli_linux1 ~]$ cut -nb 3 cut_test2.txt # 切割第3個字符 一 二 三 [root@moli_linux1 ~]$ cut -c 3 cut_test2.txt 一 二 三 [root@moli_linux1 ~]$ cut -c 4,5 cut_test2.txt aa bb cc [root@moli_linux1 ~]$ cut -nb 4,5 cut_test2.txt aa bb cc
示例4:對於非固定格式的文檔,使用-f選項來定義域來切割文件文檔
[root@moli_linux1 ~]$ head -n 5 /etc/passwd > cut_test.txt [root@moli_linux1 ~]$ cat cut_test.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@moli_linux1 ~]$ cut -d ":" -f 1-3,5 cut_test.txt root:x:0:root bin:x:1:bin daemon:x:2:daemon adm:x:3:adm lp:x:4:lp
上面的cut -d ":" -f 1-3,5 cut_test.txt
中,-d
選項指定:爲分割符,每一個分隔符做爲一個域,-f
後面的數字指定切割哪一個域,這裏1-3,5表示截取第1,2,3和第5個域。字符串
說明
sort命令將文件進行排序,並將排序結果標準輸出。sort命令既能夠從特定的文件,也能夠從stdin中獲取輸入。
工做原理
sort將文件的每一行做爲一個單位,相互比較,比較原則是從首字符向後,依次按ASCII碼值進行比較,最後將他們按升序輸出。
語法
sort(選項)(參數)
經常使用選項
-u
:unique 惟一,排序而且排除重複項-n
: number 按照數字進行排序,默認數字會被看成字符串進行比較,字符或者其餘特殊符號會被當作0排在數字前面-r
:反向排序-b
:忽略每行前面開始出的空格字符示例1:-u選項
[root@moli_linux1 ~]$ cat sort_test.txt hello world >>> hello 12345 shell ??? have a space [root@moli_linux1 ~]$ sort -u sort_test.txt # 去除重複hello並排序 >>> ??? 12345 have a space hello shell world
示例2:-n選項。按照數字進行排序,默認數字會被看成字符串進行比較,字符或者其餘特殊符號會被當作0排在數字前面
[root@moli_linux1 ~]$ cat sort_test2.txt 987 124 123 234 ??? hello 123 [root@moli_linux1 ~]$ sort -n sort_test2.txt ??? hello 123 123 124 234 987
示例3:反向排序-r
[root@moli_linux1 ~]$ cat sort_test2.txt 987 124 123 234 ??? hello 123 [root@moli_linux1 ~]$ sort -rn sort_test2.txt 987 234 124 123 123 hello ???
說明
uniq命令用於報告或忽略文件中的重複行,通常與sort命令結合使用。對於uniq來講,去除重複的內容,內容之間必須相鄰,否則沒法去除。所以多先用sort排序,再用uniq去重。
語法
uniq(選項)(參數)
經常使用選項
-c
:在每列旁邊顯示該行重複出現的次數-d
: 僅顯示重複出現的行列;示例1
[root@moli_linux1 ~]$ cat uniq.txt hello world hello python 1 1 2 3 [root@moli_linux1 ~]$ uniq uniq.txt # 只會去除相鄰的重複內容數字1,不相鄰的重複內容hello不會被刪除 hello world hello python 1 2 3
示例2,結合sort刪除全部重複內容
[root@moli_linux1 ~]$ sort uniq.txt |uniq 1 2 3 hello python world
示例3:統計各行在文件中出現的次數
[root@moli_linux1 ~]$ sort uniq.txt |uniq -c 2 1 1 2 1 3 2 hello 1 python 1 world
示例4:找出文件中重複的行
[root@moli_linux1 ~]$ sort uniq.txt |uniq -d 1 hello
總結:這三個命令常常在shell腳本中使用,除了grep,sed,awk三個以外,也須要熟練掌握。