首页 > Linux操作系统 > Linux操作系统 > 管線命令 ( pipe )
| [test @test bin]# last [test @test bin]# last | grep root [test @test bin]# last | grep root | wc -l |

- cut
語法:說明:
[root @test /root ]# cut -d "分隔字元" [-cf] fields
參數說明:
-d :後面接的是用來分隔的字元,預設是『空白字元』
-c :後面接的是『第幾個字元』
-f :後面接的是第幾個區塊?
範例:
[root @test /root]# cat /etc/passwd | cut -d ":" -f 1
將 passwd 這個檔案裡面,每一行裡頭的 : 用來作為分隔號,
而列出第一個區塊!也就是姓名所在啦![root @test /root]# last | cut -d " " -f1
以空白字元為分隔,並列出第一個區間![root @test /root]# last | cut -c1-20
將 last 之後的資料,每一行的 1-20 個字元取出來!
這個 cut 實在很好用!不過,說真的,除非你常常在分析 log 檔案,否則使用到 cut 的機會並不多!好了! cut 主要的用途在於將『同一行裡面的資料進行分解!』,最常使用在分析一些數據或文字資料的時候!這是因為有時候我們會以某些字元當作分割的參數,然後來將資料加以切割,以取得我們所需要的資料。我也很常使用這個功能呢!尤其是在分析 log 檔案的時候!
- sort
語法:說明:
[root @test /root ]# sort [-t 分隔符號] [(+起始)(-結束)] [-nru]
參數說明:
-t 分隔符號:使用分隔符號來隔開不同區間,預設是 tab
+start -end:由第 start 區間排序到 end 區間
-n :使用『純數字』排序(否則就會以文字型態來排序)
-r :反向排序
-u :相同出現的一行,只列出一次!
範例:
[root @test /root]# cat /etc/passwd | sort
將列出來的個人帳號排序![root @test /root]# cat /etc/passwd | sort -t: +2n
將個人帳號中,以使用者 ID 來排序(以 : 來分隔,第三個為 ID ,
但第一個代號為 0 之故)[root @test /root]# cat /etc/passwd | sort -t: +2nr
反相排序囉!
sort 同樣是很常用的指令呢!因為我們常常需要比較一些資訊啦!舉個上面的第二個例子來說好了!今天假設你有很多的帳號,而且你想要知道最大的使用者 ID 目前到哪一號了!呵呵!使用 sort 一下子就可以知道答案咯!當然其使用還不止此啦!有空的話不妨玩一玩!
- wc
語法:說明:
[root @test /root ]# wc [-lmw]
參數說明:
-l :多少行
-m :多少字元
-w :多少字?
範例:
[root @test /root]# cat /etc/passwd | wc -l
這個檔案裡頭有多少行?[root @test /root]# cat /etc/passwd | wc -w
這個檔案裡頭有多少字!?
wc 也可以當作指令?呵呵!這可不是上洗手間的 WC 呢!這是相當有用的計算檔案內容的一個工具組喔!舉個例子來說,當你要知道目前你的帳號檔案中有多少個帳號時,就使用上面的 wc -l 啦!因為 /etc/passwd 裡頭一行代表一個使用者呀!所以知道行數就曉得有多少的帳號在裡頭了!而如果要計算一個檔案裡頭有多少個字元時,呵呵!就使用 wc -w 這個參數吧!
| [root @test /root ]# uniq 參數說明: 範例: [root @test /root]# last | cut -d" " -f1 | sort | uniq |
| [root @test /root ]# last | tee last.list | cut -d " " -f1 參數說明: 範例: [root @test /root]# last | tee last.list | cut -d " " -f1 |
| [root @test /root ]# tr [-ds] SET1 參數說明: -d :刪除 SET1 這個字串 -s :取代掉重複的字元! 範例: [root @test /root]# last | tr '[a-z]' '[A-Z]' <==將小寫改成大寫 [root @test /root]# cat /etc/passwd | tr -d : <==嘿嘿! : 這個符號在 /etc/passwd 中不見了! [root @test /root]# cat /home/test/dostxt | tr -d '\r' > dostxt-noM <==將 DOS 檔案的字尾符號 ^M 的符號去除! |
| [root @test /root ]# split [-bl] 輸入檔案 輸出檔案前導字元 參數說明: -b :以檔案 size 來分 -l :以行數來分 範例: [root @test /root]# split -l 5 /etc/passwd test <==會產生 testaa, testab, testac... 等等的檔案 |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12137615/viewspace-615246/ ,如需转载,请注明出处,否则将追究法律责任。