grep:正則表達式,文本過濾工具,可以實現以指定的"模式(Pattern)"逐行搜索文件中的內容,並將匹配到的行顯示出來.正則表達式
模式:是由正則表達式的元字符,其餘字符組合起來的匹配字符。bash
每一類正則表達式自己的表達式是須要用戶本身去寫的,但表達式的元字符都有着固定的或者特定的意義,咱們能夠根據本身的須要去理解或者組合字符,生成咱們須要的模式ssh
-v:顯示不被模式匹配到的行,invert-match -i:在作模式匹配的時候不區分大小寫ignore-case 工具 -o:只顯示匹配到的串,而非默認顯示匹配到的行,only-matchingspa -A 數字:A=after,顯示到匹配到的行後,還顯示每個匹配項下面*行的內容 3d -B 數字:before,前*行排序 -C 數字:context,上下文*行ci -E:擴展的正則表達式it |
例子:io
[root@lbg test]# grep -o 'root' /etc/passwd --只顯示字段,不顯示整行。 [root@lbg test]# grep -A 1 'root' /etc/passwd --顯示該行和下一行內容 [root@lbg test]# grep -B 2 'root' /etc/passwd --顯示該行和其前兩行內容。 |
1.字符匹配( . [] [^] [:space:] [:punct:])
. 匹配任意單個字符(製表符,空格,標點,字母,數 字,其餘符號) [] 匹配指定範圍內的任意單個字符 [^] 指定範圍外的單個字符 [:space:] 匹配空白字符,包括空格,tab, [:punct:] 標點符號(用法同空白字符) |
例子:
[root@lbg test]# grep 'r[oa]t' /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin |
* (貪婪模式) 作次數匹配,匹配*前面的字符0,1或屢次,只表示次數,不表示字符 ? 只作次數匹配,0或1次,寫法是: \? {m,n} 最少m次,最多n次 寫法爲: \{m,n\} 用{m,n}時能夠沒有上限,但下限必定要有. 且 ?和{}都要加\. |
[root@lbg test]# cat 1 ab a b a b a b [root@lbg test]# grep 'a b' 1 --匹配1個空格 a b [root@lbg test]# grep 'a *b' 1 --匹配0及以上的空格 ab a b a b a b a b [root@lbg test]# grep 'a \?b' 1 --匹配0或1個空格 ab a b [root@lbg test]# grep 'a \{2,3\}b' 1 ---匹配2或3個空格 a b a b [root@lbg test]# grep 'a \{2,\}b' 1 --匹配2個及以上空格 a b a b a b |
\< 錨定詞首 ####找到以r..t開頭的內容 \ \> 錨定詞尾 ### r..t\> ^ 脫字符 行首錨定 $ 行尾錨定 ##### root$ -->必須以root結尾 |
[root@lbg test]# grep '\
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@lbg test]# grep 'ot\>' /etc/passwd ---ot結尾的單詞
root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin [root@lbg test]# grep '^ro' /etc/passwd --以ro開頭的行 root:x:0:0:root:/root:/bin/bash [root@lbg test]# grep 'bash$' /etc/passwd --以bash結尾的行 |
[root@lbg test]# ls ---無文件 [root@lbg test]# touch a --建立文件a [root@lbg test]# cp /test/a{,1} --將a文件複製,並在其原路徑下重命名爲a1. [root@lbg test]# ls a a1 |
wc [options]... file_name -l 僅顯示行數(line) -w 僅顯示單詞數(word) -c 僅顯示字節數(char) 單獨的wc顯示的信息依次是:行數 單詞數 字節數 文件名 |
[root@lbg test]# cat a how do you do how do you do [root@lbg test]# wc a ---顯示4種信息 2 8 28 a [root@lbg test]# wc -l a --顯示行數 2 a [root@lbg test]# ls /test a b c d [root@lbg test]# ls /test | wc -l ----使用wc統計目錄下文件的個數 4 |
tr:轉換字符或者刪除字符(tr -d '字符集合' --delete ) tr '集合1' '集合2' --將集合1裏的內容按集合2對應位置的內容替換顯示出來。 |
例子:
[root@lbg test]# echo 'abcdabcd' |tr 'ac' '13' --兩邊集合一一對應的狀況 1b3d1b3d [root@lbg test]# echo 'abcdabcd' |tr 'acd' '13' --集合1多於集合2的狀況 1b331b33 [root@lbg test]# echo 'abcdabcd' |tr 'acd' '1234' --集合1少於集合2的狀況 1b231b23 [root@lbg test]# echo 'abcdabcd' |tr 'a-z' 'A-Z' --大小寫轉換 ABCDABCD [root@lbg test]# echo 'abcdabcd' |tr -d 'ab' ---刪除字符 cdcd |
-c :按照字符數量切 (characters)
-d :按指定分隔符切(delimiter 定界符) -f:指定要顯示的字段 (file) 單個數字-->一個字段 逗號分隔的多個數字-->指定多個離散字段 - -->連續字段,3-5表示3到5字段 |
[root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2 --按空格切 are [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2,4 --第2個和第4個 are dogs [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2-4 --2到4個 are many dogs [root@lbg test]# echo 'there are many dogs'|cut -d m -f 1 --以m切割 there are [root@lbg test]# echo 'there are many dogs'|cut -d m -f 2 --以m切割 any dogs [root@lbg test]# echo 'how do you do'|cut -c 2-5 ---取第2到第5個字符 ow d |
-f:忽略字符大小寫(ignore-case) -n:對數值進行排序 //Sort 默認字符排序 加,-n 數字排序. -r : 逆序輸出 -t:指定分隔符 -k:基於哪一個字段進行排序 (key) -u:uniq,重複的行只顯示一行 (unique) |
[root@lbg test]# cut -d : -f 3 /etc/passwd |sort -n [root@lbg test]# cat a 234 123 234 [root@lbg test]# sort -u a 123 234 [root@lbg test]# sort -n a 123 234 234 [root@lbg test]# sort -n -u a 123 234 |
9.uniq:去重複行。
-c:統計每一行出現的次數(count)
-d:僅顯示重複過的行 -u:僅顯示未重複行 注意uniq去重,認定是連續的纔是重複的.不是連續的不會去重.
|
[root@lbg test]# cat a 234 234 123 234 [root@lbg test]# uniq a 234 123 234 [root@lbg test]# sort a | uniq -c 1 123 3 234 [root@lbg test]# sort -u -n a 123 234 |
\1--引用從左到右的第一個括號內範圍.
\2--引用從左到右的第二個括號內範圍.
例子:
[root@lbg test]# cat a he love his lover she like her liker [root@lbg test]# grep 'l..e.*e' a he love his lover she like her liker [root@lbg test]# grep '\(l..e\).*\1' a ---與命令grep 'l..e.*e' a相同 he love his lover she like her liker [root@lbg test]# grep '\(l\(..\)e\).*\1' a ---與命令grep 'l..e.*e' a相同 he love his lover she like her liker [root@lbg test]# grep 'l..e.*l..' a he love his lover she like her liker [root@lbg test]# grep '\(l\(..\)e\).*\2' a --與grep 'l..e.*l..' a 相同 he love his lover she like her liker |