grephtml
grep:global search regular expression(RE) and print out the line (全面搜索正則表達式並把行打印出來)linux
grep在數據中查找一個字符串時,是以「整行」爲單位進行數據選取的git
1. 定義正則表達式
1) grep是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不一樣。egrep是grep的擴展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它們把全部的字母都看做單詞,也就是說,正則表達式中的元字符表示回其自身的字面意義,再也不特殊。linux使用GNU版本的grep。它功能更強,能夠經過-G、-E、-F命令行選項來使用egrep和fgrep的功能。redis
1) -A NUM,--after-context=NUM 除了列出匹配行以外,還列出其後NUM行shell
範例1:express
ompmsc35 chuntaoh> cat test1工具
a1ui
b2this
c3
d4
e5
f6
ompmsc35 chuntaoh> grep -A 1 'b' test1
b2
c3
2) -a或--text
grep本來是搜尋文字文件,若拿二進制的檔案做爲搜尋的目標,則會顯示以下的訊息: Binary file 二進制文件名matches 而後結束。
若加上-a參數則可將二進制檔案視爲文本文件搜尋,至關於--binary-files=text這個參數。
範例2:
ompmsc35 chuntaoh> grep 'redistribute' /bin/mv
Binary file /bin/mv matches
ompmsc35 chuntaoh> grep -a 'redistribute' /bin/mv
This is free software. You may redistribute copies of it under the terms of
範例3:
(1)找出一個二進制文件。如/usr/bin/[,
ompmsc35 chuntaoh> file /usr/bin/[
/usr/bin/[: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.18, dynamically linked (uses shared libs), stripped
(2)二進制文件用strings查看
ompmsc35 chuntaoh> strings [
/lib64/ld-linux-x86-64.so.2
__gmon_start__
libc.so.6
setlocale
mbrtowc
optind
fflush_unlocked
dcgettext
error
__lxstat
iswprint
(2)使用grep -a
ompmsc35 chuntaoh> grep -a shell /usr/bin/[
3) -B NUM,--before-context=NUM
與-A NUM 相對,但這此參數是顯示除符合行以外,並顯示在它以前的NUM行。ompmsc35 chuntaoh> cat test1
a1
b2
c3
d4
e5
f6
ompmsc35 chuntaoh> grep -B 1 'b' test1
a1
b2
4) -b, --byte-offset 打印匹配行前面的文本總共有多少byte
範例4:
ompmsc35 chuntaoh> cat test1
a1
b2
c3
d4
e5
f6
ompmsc35 chuntaoh> grep -b 'a' test1 #前面的行有0字節
0:a1
ompmsc35 chuntaoh> grep -b '1' test1
0:a1
ompmsc35 chuntaoh> grep -b 'b' test1 #前面的行有3個字節,由於\n也算一個字節
3:b2
ompmsc35 chuntaoh> grep -b '2' test1
3:b2
ompmsc35 chuntaoh> od -N4 test1 -t c #N只查看4個字節,顯t按ASCII顯示,可知\n也算一個字節
0000000 a 1 \n b
0000004
ompmsc35 chuntaoh> od -N6 test1 -t c
0000000 a 1 \n b 2 \n
0000006
5)-C [NUM]
-NUM
--context[=NUM] 列出匹配行以外並列出上下各NUM行,默認值是2,爲何不能用默認的兩行
ompmsc35 chuntaoh> grep --context=2 '3' test1
a1
b2
c3
d4
e5
6)-c:計算找到」搜索字符串」的個數。不顯示符合樣式行,只顯示符合的總行數。
若再加上-v,--invert-match,參數顯示不符合的總行數。
ompmsc35 chuntaoh> grep -c '3' test1
1
ompmsc35 chuntaoh> grep -c '3' test1 -v
5
7) -d ACTION, --directories=ACTION
ompmsc35 chuntaoh> cp test1 ./dir/test1
ompmsc35 chuntaoh> grep -d recurse '3' dir
dir/test1:c3
ompmsc35 chuntaoh> grep -r '3' dir
dir/test1:c3
ompmsc35 chuntaoh> grep -r 'c3' /home/chuntaoh/dir
/home/chuntaoh/dir/test1:c3
ompmsc35 chuntaoh> grep -r 'c3' /home/chuntaoh/dir -d skip #跳過目錄,沒有輸出
ompmsc35 chuntaoh> grep -r 'c3' /home/chuntaoh/dir -d read #看做通常文檔,沒有輸出
8)-E, --extended-regexp 採用規則表示式去解釋樣式。至關於egrep
ompmsc35 chuntaoh> grep '3|4' test1 #通常狀況下,不能用|分隔兩個匹配方式
ompmsc35 chuntaoh> grep '3\|4' test1 #可是若是加了\轉義,則能夠
c3
d4
ompmsc35 chuntaoh> egrep '3|4' test1 #egrep可用|
c3
d4
9)-e PATTERN, --regexp=PATTERN
指定多個匹配模式,很到知足兩個模式中任意一個的全部結果
一般用在避免partern用-開始。
ompmsc35 chuntaoh> cat test1
a1
b2
-c3
d4
e5
f6
ompmsc35 chuntaoh> grep '-c' test1 #沒有輸出
ompmsc35 chuntaoh> grep -e -c test1
-c3
範例6:-e: 指定多個匹配模式,很到知足兩個模式中任意一個的全部結果
ompmsc35 chuntaoh> cat test
one
two
three
four
five
six
ompmsc35 chuntaoh> grep -e t -e f test
two
three
four
five
輸出了含有字符t或字符f的全部行,也可以使用正則表達式
ompmsc35 chuntaoh> grep [tf] test
two
three
four
five
10)-f FILE, --file=FILE
事先將要搜尋的樣式寫入到一個檔案,一行一個樣式。而後採用檔案搜尋。空的檔案表示沒有要搜尋的樣式,所以也就不會有任何符合。ompmsc35 chuntaoh> grep 'redistribute' /bin/mv
Binary file /bin/mv matches
17)-i, --ignore-case 忽略大小寫,包含要搜尋的樣式及被搜尋的檔案。
ompmsc35 chuntaoh> grep -i 'C' test1
-c3
18) -L, --files-without-match 不顯示日常通常的輸出結果,反而顯示出沒有符合的文件名稱
ompmsc35 chuntaoh> grep -L 'c' test1 test2
test2
19) -l, --files-with-matches 不顯示日常通常的輸出結果,只顯示符合的文件名稱
ompmsc35 chuntaoh> grep -l 'c' test1 test2
test1
20)--mmap 不懂
若是可能,使用mmap系統呼叫去讀取輸入,而不是預設的read系統呼叫。
在某些情況,--mmap 能產生較好的效能。 然而,--mmap若是運做中檔案縮短,或I/O 錯誤發生時,可能形成未定義的行爲(包含core dump)。
21)-n, --line-number 在顯示行前,標上行號。
ompmsc35 chuntaoh> grep -n '3' test1
3:-c3
22)-q, --quiet, --silent 不顯示任何的通常輸出。請參閱-s或--no-messages
grep -q用於if邏輯判斷
23) -R -r, --recursive 遞歸地,讀取每一個資料夾下的全部文件,此至關於-d recsuse 參數
ompmsc35 chuntaoh> grep -r 'c3' /home/chuntaoh/dir
/home/chuntaoh/dir/test1:c3
24) -s, --no-messages 不顯示關於不存在或沒法讀取的錯誤信息。
不懂
小注: 不像GNU grep,傳統的grep不符合POSIX.2協議,由於缺少-q參數,且他的-s 參數表現像GNU grep的 -q 參數。ompmsc35 chuntaoh> grep 'c3' test1 test2 test3
test1:-c3
grep: test3: No such file or directory
ompmsc35 chuntaoh> grep -s 'c3' test1 test2 test3
test1:-c3
25) -V, --version顯示出grep的版本號到標準錯誤。
當在回報有關grep的bugs時,grep版本號是必需要包含在內的。
26)-v, --invert-match 顯示除搜尋樣式行以外的所有。
ompmsc35 chuntaoh> grep -v 'c3' test1
a1
b2
d4
e5
f6
27)w, –word-regexp 意思就是精確匹配,匹配單詞還不是字符串,如想匹配「is」,」this」就不會被匹配
5. POSIX字符類
爲了在不一樣國家的字符編碼中保持一至,POSIX(The Portable Operating System Interface)增長了特殊的字符類,如[:alnum:]是A-Za-z0-9的另外一個寫法。要把它們放到[]號內才能成爲正則表達式,如[A-Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字符類。
fgrep把全部的字母都看做單詞,也就是說,正則表達式中的元字符表示回其自身的字面意義,再也不特殊。
類 等價的正則表達式 解釋
[[:upper:]] [A-Z] 小寫字符
[[:lower:]] [a-z] 大寫字符
[[:alpha:]] [a-zA-Z] 文字字符
[[:alnum:]] [0-9a-zA-Z] 文字數字字符
[[:digit:]] [0-9] 數字字
[[:space:]] [空格或tab鍵等] 全部空白字符(新行,空格,製表符)
範例7:匹配每段爲3個數字的IP地址
grep "[0-9]\{3\}.[0-9]\{3\}.[0-9]\{3\}.[0-9]\{3\}" file
範例8:匹配全部的IP地址
grep "[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}" file
範例12:在全部文件中查詢單詞」sort it」
grep 「sort it」 *
範例13: 查詢空行,查詢以某個條件開頭或者結尾的行。
結合使用^和$可查詢空行。使用- n參數顯示實際行數
ompmsc35 chuntaoh> grep -n '^$' test1 #說明第3行,第4行是空行
3:
4:
ompmsc35 chuntaoh>