[b] grep(global search regular expression(RE) and print out the line,全面搜索正則表達式並把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。[/b][b] (1)用法:[/b]
[b] 用法: grep [選項]... PATTERN [FILE]...[/b][b] (2)功能:[/b]
[b] 功能: 在每個 FILE 或是標准輸入中查找 PATTERN。[/b][b] (3)選項參數:[/b]
1) -V, --version 顯示版本號
2) -i 在匹配過程中忽略大小寫
3) -v, --invert-match 顯示不匹配的行 4) -f 指定文件中存的每行字符串作為匹配字符串
5) -c 統計每個文件中包含指定字符串的行數 6) --color=auto 標記匹配顏色
7) -E 將范本樣式為延伸的普通表示法來使用,意味著使用能使用擴展正則表達式
8) -q grep靜默輸出,常用來測試。[b] (4)實例:[/b]
1)[root@localhost grepDir]# grep "MenAngel" t1.txt t2.txt t3.txt 在特定的文本集中查找特定字符串
[root@localhost grepDir]# cat >t1.txt I'm MenAngel! Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! ^Z [3]+ 已停止 cat > t1.txt [root@localhost grepDir]# cat >t2.txt Every one fights for a better future,but I fight for freedom! ^Z [4]+ 已停止 cat > t2.txt [root@localhost grepDir]# cat >t3.txt <<EOF > There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! > When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked. > EOF [root@localhost grepDir]# grep "MenAngel" t1.txt t2.txt t3.txt t1.txt:I'm MenAngel!
2)[root@localhost grepDir]# grep -v "MenAngel" t1.txt t2.txt t3.txt 輸出指定字符串所在行之外的所有文件的行內容
[root@localhost grepDir]# grep -v "MenAngel" t1.txt t2.txt t3.txt t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t2.txt:Every one fights for a better future,but I fight for freedom! t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
3)[root@localhost grepDir]# grep "fight" t1.txt t2.txt t3.txt --color=auto 將查找的字符串用特定的顏色標記出來
[root@localhost grepDir]# grep "fight" t1.txt t2.txt t3.txt --color=auto t2.txt:Every one fights for a better future,but I fight for freedom!
4)[root@localhost grepDir]# grep -c "that" t1.txt t2.txt t3.txt 統計每個文件中包含指定字符串的行數
[root@localhost grepDir]# grep -c "that" t1.txt t2.txt t3.txt t1.txt:1 t2.txt:0 t3.txt:2
5)[root@localhost grepDir]# grep -n "that" t1.txt t2.txt t3.txt 默認情況,輸出包含特定字符串所在的行
[root@localhost grepDir]# grep -n "that" t1.txt t2.txt t3.txt t1.txt:2:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t3.txt:1:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:2:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked. [root@localhost grepDir]# grep "that" t1.txt t2.txt t3.txt t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.6)[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep "MenAngel" 配合cat命令查看文件中指定字符串所在行的內容
[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep "MenAngel" I'm MenAngel! [root@localhost grepDir]# grep "MenAngel" t1.txt t2.txt t3.txt t1.txt:I'm MenAngel!7)[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^I 查找以指定字符串開頭的文本行並輸出
[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^I I'm MenAngel! [root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^M //沒有以M開頭的 [root@localhost grepDir]#8)[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^[^I] 經不以指定字符串開頭的文本所在行的內容輸出
[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^[^I] Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world! Every one fights for a better future,but I fight for freedom! There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves! When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked. [root@localhost grepDir]#9)[root@localhost grepDir]# seq 10|grep "5" -C|-A|-B 3 顯示特定行的前面或後面的內容
[root@localhost grepDir]# seq 10|grep "5" -C 3 2 3 4 5 6 7 8 [root@localhost grepDir]# seq 10|grep "5" -A 3 5 6 7 8 [root@localhost grepDir]# seq 10|grep "5" -B 3 2 3 4 510)[root@localhost grepDir]# grep -C 3 "MenAngel" t1.txt t2.txt t3.txt 無論用不用通道,參數都是可用的
[root@localhost grepDir]# grep -C 3 "MenAngel" t1.txt t2.txt t3.txt t1.txt:I'm MenAngel! t1.txt-Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!11)[root@localhost grepDir]# echo -e "a\nb\nc\na\nb\nc" | grep a -A 1 如果匹配結果有多個,會用“--”作為各匹配結果之間的分隔符
[root@localhost grepDir]# echo -e "a\nb\nc\na\nb\nc" | grep a -A 1 a b -- a b [root@localhost grepDir]# [root@localhost grepDir]# echo -e "a\nb\nc\nd\na\nb\nc\nd" | grep a -A 1 a b -- a b [root@localhost grepDir]# echo -e "a\nb\nc\nd\na\nb\nc\nd" | grep a -A 2 a b c -- a b c12)[root@localhost grepDir]# echo this is a text line | grep -e "is" -e "line" -o 制動多個匹配樣式
[root@localhost grepDir]# echo this is a text line | grep -e "is" -e "line" -o //grep這裡處理的是前面的echo輸出的內容 is is line13)[root@localhost grepDir]# echo MenAngel is sunjimeng|grep -f patfile 指定在文件中每行存的多個字符串
[root@localhost grepDir]# cat >patfile <<EOF > MenAngel > sunjimeng > EOF [root@localhost grepDir]# echo MenAngel is sunjimeng|grep -f patfile MenAngel is sunjimeng [root@localhost grepDir]#14)[root@localhost grepDir]# grep --help
[root@localhost grepDir]# grep --help 用法: grep [選項]... PATTERN [FILE]... 在每個 FILE 或是標准輸入中查找 PATTERN。 默認的 PATTERN 是一個基本正則表達式(縮寫為 BRE)。 例如: grep -i 'hello world' menu.h main.c 正則表達式選擇與解釋: -E, --extended-regexp PATTERN 是一個可擴展的正則表達式(縮寫為 ERE) -F, --fixed-strings PATTERN 是一組由斷行符分隔的定長字符串。 -G, --basic-regexp PATTERN 是一個基本正則表達式(縮寫為 BRE) -P, --perl-regexp PATTERN 是一個 Perl 正則表達式 -e, --regexp=PATTERN 用 PATTERN 來進行匹配操作 -f, --file=FILE 從 FILE 中取得 PATTERN -i, --ignore-case 忽略大小寫 -w, --word-regexp 強制 PATTERN 僅完全匹配字詞 -x, --line-regexp 強制 PATTERN 僅完全匹配一行 -z, --null-data 一個 0 字節的數據行,但不是空行 Miscellaneous: -s, --no-messages suppress error messages -v, --invert-match select non-matching lines -V, --version display version information and exit --help display this help text and exit 輸出控制: -m, --max-count=NUM NUM 次匹配後停止 -b, --byte-offset 輸出的同時打印字節偏移 -n, --line-number 輸出的同時打印行號 --line-buffered 每行輸出清空 -H, --with-filename 為每一匹配項打印文件名 -h, --no-filename 輸出時不顯示文件名前綴 --label=LABEL 將LABEL 作為標准輸入文件名前綴 -o, --only-matching show only the part of a line matching PATTERN -q, --quiet, --silent suppress all normal output --binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match' -a, --text equivalent to --binary-files=text -I equivalent to --binary-files=without-match -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip' -D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' -r, --recursive like --directories=recurse -R, --dereference-recursive likewise, but follow all symlinks --include=FILE_PATTERN search only files that match FILE_PATTERN --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN --exclude-from=FILE skip files matching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L, --files-without-match print only names of FILEs containing no match -l, --files-with-matches print only names of FILEs containing matches -c, --count print only a count of matching lines per FILE -T, --initial-tab make tabs line up (if needed) -Z, --null print 0 byte after FILE name 文件控制: -B, --before-context=NUM 打印以文本起始的NUM 行 -A, --after-context=NUM 打印以文本結尾的NUM 行 -C, --context=NUM 打印輸出文本NUM 行 -NUM same as --context=NUM --group-separator=SEP use SEP as a group separator --no-group-separator use empty string as a group separator --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto' -U, --binary do not strip CR characters at EOL (MSDOS/Windows) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows) ‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。 直接使用‘egrep’或是‘fgrep’均已不可行了。 若FILE 為 -,將讀取標准輸入。不帶FILE,讀取當前目錄,除非命令行中指定了-r 選項。 如果少於兩個FILE 參數,就要默認使用-h 參數。 如果有任意行被匹配,那退出狀態為 0,否則為 1; 如果有錯誤產生,且未指定 -q 參數,那退出狀態為 2。 請將錯誤報告給: [email protected] GNU Grep 主頁: <http://www.gnu.org/software/grep/> GNU 軟件的通用幫助: <http://www.gnu.org/gethelp/>[b] grep的用法遠遠不止這麼多,以後如果見得多了,用的多了,再更新![/b]