歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux命令 >> Linux grep命令參數及使用方法詳解

Linux grep命令參數及使用方法詳解

日期:2017/2/27 16:38:14   编辑:Linux命令
功能說明:查找文件裡符合條件的字符串。

語  法:grep [-abcEFGhHilLnqrsvVwxy][-A<顯示列數>][-B<顯示列數>][-C<顯示列數>][-d<進行動作>][-e<范本樣式>][-f<范本文件>][--help][范本樣式][文件或目錄...]

補充說明:grep指令用於查找內容包含指定的范本樣式的文件,如果發現某文件的內容符合所指定的范本樣式,預設grep指令會把含有范本樣式的那一列顯示出來。若不指定任何文件名稱,或是所給予的文件名為“-”,則grep指令會從標准輸入設備讀取數據。

參  數:
-a或--text 不要忽略二進制的數據。
-A<顯示列數>或--after-context=<顯示列數> 除了顯示符合范本樣式的那一列之外,並顯示該列之後的內容。
-b或--byte-offset 在顯示符合范本樣式的那一列之前,標示出該列第一個字符的位編號。
-B<顯示列數>或--before-context=<顯示列數> 除了顯示符合范本樣式的那一列之外,並顯示該列之前的內容。
-c或--count 計算符合范本樣式的列數。
-C<顯示列數>或--context=<顯示列數>或-<顯示列數> 除了顯示符合范本樣式的那一列之外,並顯示該列之前後的內容。
-d<進行動作>或--directories=<進行動作> 當指定要查找的是目錄而非文件時,必須使用這項參數,否則grep指令將回報信息並停止動作。
-e<范本樣式>或--regexp=<范本樣式> 指定字符串做為查找文件內容的范本樣式。
-E或--extended-regexp 將范本樣式為延伸的普通表示法來使用。
-f<范本文件>或--file=<范本文件> 指定范本文件,其內容含有一個或多個范本樣式,讓grep查找符合范本條件的文件內容,格式為每列一個范本樣式。
-F或--fixed-regexp 將范本樣式視為固定字符串的列表。
-G或--basic-regexp 將范本樣式視為普通的表示法來使用。
-h或--no-filename 在顯示符合范本樣式的那一列之前,不標示該列所屬的文件名稱。
-H或--with-filename 在顯示符合范本樣式的那一列之前,表示該列所屬的文件名稱。
-i或--ignore-case 忽略字符大小寫的差別。
-l或--file-with-matches 列出文件內容符合指定的范本樣式的文件名稱。
-L或--files-without-match 列出文件內容不符合指定的范本樣式的文件名稱。
-n或--line-number 在顯示符合范本樣式的那一列之前,標示出該列的列數編號。
-q或--quiet或--silent 不顯示任何信息。
-r或--recursive 此參數的效果和指定“-d recurse”參數相同。
-s或--no-messages 不顯示錯誤信息。
-v或--revert-match 反轉查找。
-V或--version 顯示版本信息。
-w或--word-regexp 只顯示全字符合的列。
-x或--line-regexp 只顯示全列符合的列。
-y 此參數的效果和指定“-i”參數相同。
--help 在線幫助。

示 例:
grep match_pattern filename或者grep "match_pattern" filename
this is the line containing match_pattern #命令會返回包含給定match_pattern的文本行

[root@server1 test]# echo -e "this is a word\nnext line" | grep word --color=auto
this is a word

grep命令通常將match_pattern視為通配符。如果要使用正則表達式,需要添加-E(extended)使用擴展正則表達式,也可以使用默認允許正則表達式的grep命令---egrep
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444" | grep -E "[a-z]+"
www.linuxeye.com 43243
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444" | egrep "[a-z]+"
www.linuxeye.com 43243

#選項-o只輸出文本中匹配到的文本部分
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444" | egrep -o "[a-z]+"
www
linuxeye
com

#選項-v(invert)將匹配結果進行反轉
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444" | egrep -v "[a-z]+"
4444

#-c統計匹配行的數量,並不是匹配的次數
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444\n3333" | egrep -vc "[a-z]+"
2

#-n打印出包含匹配字符串的行號
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444\n3333" | egrep -vn "[a-z]+"
2:4444
3:3333

#-b打印樣式匹配所位於的字符或字節偏移,一行中字符串的字符偏移是從該行的第一個字符開始計算,起始值是0
[root@server1 test]# echo -e "www.linuxeye.com 43243\n4444\n3333" | egrep -vb "[a-z]+"
23:4444
28:3333

#-l搜索多個文件並找出匹配文本位於哪一個文件中 -l相反選項-L,它會返回一個不匹配的文件列表
[root@server1 test]# grep -l line2 *
version1.txt
version2.txt
version.patch

[root@server1 test]# grep -L line2 *
filestat.sh
other
out.html
remove_duplicates.sh
test
test_copy1
test_copy2

#-R遞歸搜索文件
[root@server1 test]# grep -Rn "text" .

#-i忽略樣式中的大小寫
[root@server1 test]# echo www.LINUXEYE.COM | grep -i linuxeye
www.LINUXEYE.COM

#-e匹配多個樣式中的一個
[root@server1 test]# echo www.LINUXEYE.COM | grep -ie linx -e com
www.LINUXEYE.COM

#-f與-e選項以後,-f在樣式文件中逐行寫下匹配的樣式
[root@server1 test]# cat pat_file
hello
cool
[root@server1 test]# echo "hello this is cool" | grep -f pat_file
hello this is cool

#在grep搜索中包含或排除文件
只在目錄中遞歸搜索所有的.c和.cpp文件
[root@server1 test]# grep "main()" /usr -r --include *.{c,cpp}
/usr/src/kernels/2.6.18-194.el5PAE-i686/scripts/kconfig/gconf.c: gtk_main();
...

在搜索中排除所有的README文件
[root@server1 test]# grep "main()" . -r --exclude "README"
如果需要排除目錄,可以使用--exclude-dir選項
如果需要從文件中讀取所需排除的文件列表,使用--exclude-from FILE

使用0值字節後綴的grep與xargs
[root@server1 test]# echo test > file1
[root@server1 test]# echo cool > file2
[root@server1 test]# echo test > file3
[root@server1 test]# grep "test" file* -lZ | xargs -0 rm

grep的靜默輸出
-q(quiet mode)
[root@server1 test]# cat file2
cool
[root@server1 test]# grep -q 'co' file2 ;echo $?
0
[root@server1 test]# grep -q 'cookk' file2 ;echo $?
1

打印出匹配文本的之前行或之後行
[root@server1 test]# seq 10 | grep 5 -A 3
5
6
7
8
[root@server1 test]# seq 10 | grep 5 -B 3
2
3
4
5
[root@server1 test]# seq 10 | grep 5 -C 2
3
4
5
6
7
Copyright © Linux教程網 All Rights Reserved