grep

1.正則表達式php


1.1 基本正則表達式(RE)git

 

匹配 釋義
元字符 ^
行首錨定
$
行尾錨定
.
任意單個字符
*
不表明字符(屬於限定符),匹配其前導字符的任意次
[ ]
字符集匹配
[^ ]
不匹配其中列出的任意字符
\(\)
後續表達式經過「轉義序列」(\1)來引用
x\{m,n\}
匹配字符x出現的次數區間;表示最少時須要逗號
\<
詞首錨定
\>
詞尾錨定
組合
.*
這個組合表示任意字符的任意長度
e*
表示空,所有匹配(由於星號表示任意數量,也包括零)
\?
匹配其前字符 1 次或 0 次
b\{1,1\}
匹配字母「b」 1 次
b\{2,3\} 匹配字母「b」 最少 2 次,至多 3 次
b\{3\} 匹配字母「b」3次
b\{,3\} 錯誤
b\{3,\} 匹配字母「b」 最少 3 次
b\{0,3\}a
i_f02.gif

 

匹配行首開始的年份「1983-02」、「1983-02-06_17:33:26」:web

$ vlinkstatus -a | grep "^[[:digit:]]\{4\}-\([[:digit:]]\{2\}\)"
$ vlinkstatus -a | grep "^[[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}_[[:digit:]]\{2\}\(:\)[[:digit:]]\{2\}\1[[:digit:]]\{2\}"

「.」表明一個字符,不能省略。沒法匹配下面的「file」。
正則表達式

[web@h p]$ cat re.txt
file
file1
file22
file3 rc
[web@h p]$ grep "file.$" re.txt
file1
[web@h p]$ grep "file." re.txt
file1
file22
file3 rc
[web@h p]$

 

在文件「re.txt」追加一行「file4 」,末尾是個空格。bash

[web@h p]$ echo "file4 " >> re.txt
[web@h p]$ grep "file.$" re.txt
file1
[web@h p]$ grep "file. $" re.txt
file4 
[web@h p]$ grep "file..$" re.txt
file22
file4

 

1.2 擴展正則表達式(ERE)ide

元字符 釋義
① - 與基本正則表達式意義、用法徹底相同
+
匹配其前導字符最少一次(屬於限定符)
?
前導字符最多出現一次(屬於限定符);零次或者一次
|
表示多個表達式之間或的關係
( )
表示一組可選值的集合;一般與豎線一塊兒使用、不只於此

 

[web@h p]$ egrep "file[[:digit:]]+" re.txt
file1
file22
file3 rc
file4 
[web@h p]$ egrep "file[[:digit:]]?" re.txt
file
file1
file22
file3 rc
file4 
[web@h p]$ egrep "e(1|3)" re.txt
file1
file3 rc
[web@h p]$

 

1.3 Perl正則表達式spa

 

元字符 釋義
\d

匹配從0到9中的任意一個數字字符;「[0-9]」命令行

\D

匹配一個非數字字符;「[^0-9]」翻譯

\s 匹配任意空白字符 ;空格、製表……,「[\f\n\r\t\v]」
\S 匹配任意非空白字符

 

[web@h p]$ grep -P "e\d" re.txt
file1
file22
file3 rc
file4 
[web@h p]$ grep -P "e\d\s" re.txt
file3 rc
file4

 

1.4 空白字符code

任意空白字符 」,命令行體驗一下;包括「\f \n \r \t \v」。

 

表達式 意義 翻譯
\f form feed 換頁
\n new line 換行

\r

carriage return 回車
\t horizontal tab 水平製表
\v vertical tab 垂直製表

 

[web@h p]$ echo -e "hello\fworld"
world             # 這個命令執行後,就清屏了,只剩下個「world」了。
[web@h p]$ echo -e "hello\nworld"
hello
world
[web@h p]$ echo -e "hello\rworld"
world
[web@h p]$ echo -e "hello\tworld"
hello    world
[web@h p]$ echo -e "hello\vworld"
hello
     world
[web@h p]$ echo -e "hello\vvworld"
hello

     world

 

1.5 字符集

字符類 釋義
[:alpha:] 匹配一個字母;「[a-zA-Z]」
[:digit:] 匹配一個數字;「[0-9]」
[:alnum:] 匹配一個字母或數字;「[0-9a-zA-Z]」
[:lower:] 小寫字母
[:upper:] 大寫字母
[:space:] 一個空白字符;空格、製表符、換行……
[:blank:] 空格、製表符
[:graph:] 一個看得見的可打印字符;不包括空白字符
[:print:] 大於上邊,包括空白字符;不包括控制字符、字符串結束符「\0」、EOF文件結束符(-1)
[:cntrl:] 匹配一個控制字符(ASCII字符集中的前32個字符)
[:punct:] 匹配一個標點符號
[:xdigit:] 匹配十六進制數字

 

2.grep


 

基本語法
grep [option] pattern [file]...

 

選項 說明
輸出控制

-c

只打印匹配的行數
--color 高亮顯示匹配到的內容
-o 只顯示被匹配到的字符串
-h 處理多個文件時,不顯示文件名
-l 只顯示匹配到的文件名稱(跟上邊相反)
-n

顯示匹配到的行,並顯示行號

-q 不輸出匹配結果;而依據程序退出狀態判斷匹配與否
匹配控制
-i 忽略大小寫匹配
-v 顯示不匹配的文件行
-w
匹配整個單詞
-x
匹配整個文本行
-F 不支持正則表達式
-E
支持擴展正則表達式
-P
支持perl正則表達式
上下行控制

-A "n"

匹配行往下顯示n行
-B "n"
匹配行往上顯示n行
-C "n"
匹配行往上、下顯示n行
文件和目錄選擇
-r 遞歸搜索全部的目錄下的文件

 

直接打印出符合條件的行數,而不用另外調用「wc -l」:

$ grep docBase server.xml -c
1

 


 

  1. eg.1.全文搜索:
    #!/bin/bash
    
    #tree -if $1 | grep ".php$"
    echo -e '     path:\t' $1
    echo -e '   string:\t' $2
    echo -e 'extension:\t' $3
    echo ""
    
    for i in $(tree -if $1 | grep ".${3}$")
    do
        if [ -f $i ]
        then
            #echo "hello " $i
            #grep --color -l $2 $i && echo "filename: " $i
            grep --color -l $2 $i
            grep --color $2 $i
            #echo grep -q $2 $i && echo -n "match: "$(grep -c $2 $i) && echo -e "\tfilename: " $i
        fi
    done
    View Code

     

 

grep.

相關文章
相關標籤/搜索