grep的常用和次常用选项

选项选项2英文翻译
-P–perl-regexpPATTERN is a Perl regular expression使用Perl正则表达式语法
-i–ignore-caseignore case distinctions忽略大小写
-v–invert-matchselect non-matching lines选择未匹配的那些行
-L–files-without-matchprint only names of FILEs containing no match输出未匹配的文件名
-l–files-with-matchesprint only names of FILEs containing matches输出匹配的文件名
-c–countprint only a count of matching lines per FILE每个文件输出一个匹配的行数
-n–line-numberprint line number with output lines输出匹配的数据的同时打印行号
-H–with-filenameprint the file name for each match对于每一处匹配,都打印文件名
-h–no-filenamesuppress the file name prefix on output在输出时,不输出文件名前缀(详见下方解释)
-o–only-matchingshow only the part of a line matching PATTERN不打印整行,只显示匹配到的那一部分文字
-r–recursivelike –directories=recurse递归。如果没有指定目录,则取当前目录,否则使用指定目录
-B–before-context=NUMprint NUM lines of leading context显示匹配到的行的同时,显示其前面的#行
-A–after-context=NUMprint NUM lines of trailing context显示匹配到的行的同时,并显示后续的#行
-C–context=NUMprint NUM lines of output context显示匹配到的行的同时,并显示其前面的#行和后面的#行
–exclude-dir=DIRExclude directories matching the pattern DIR from recursive searches.递归搜索中排除匹配到的目录
–exclude=GLOBSkip files whose base name matches GLOB
(using wildcard matching).
如果通配符匹配到了某个文件名,那么跳过这个文件名
–include=GLOBSearch only files whose base name matches GLOB
(using wildcard matching as described under –exclude).
只搜索那些符合通配符的文件

grep的部分帮助信息

[root@localhost ~]# grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

...(略)...

'egrep' means 'grep -E'.  'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input.  With no FILE, read . if a command-line
-r is given, - otherwise.  If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
当文件是"-"时,读取标准输入。
如果没有没有给出文件的话,如果命令行给出了"-r"就读取"."(当前目录),否则读取"-"(标准输入)。
如果向grep传递了少于两个文件,就假定有"-h"选项(例:送给grep三个文件,结果匹配到一个文件,那么grep需要输出文件名,让你知道匹配到的是哪一个文件;如果送给grep一个文件的话,那么grep再显示文件名就显得有些多余了)。
Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>
[root@localhost ~]# 

示例

Usage: grep [OPTION]... PATTERN [FILE]...
Example: grep -i 'hello world' menu.h main.c
递归查找某目录下的(  包含某字符串[字符串忽略大小写]的)所有文件,只输出文件名
grep -rli  "字符串" --include="*.cpp" --include="*.h"  "目录"
递归查找某目录下的(不包含某字符串[字符串忽略大小写]的)所有文件,只输出文件名
grep -rliv "字符串" --include="*.cpp" --include="*.h"  "目录"
递归查找当前目录下所有包含某字符串的文件,输出文件名、行号、该行内容
grep -r -Hn "字符串" #(或)# grep -r -Hn "字符串" ./
递归查找当前目录下所有包含某单词(CharacterString大小写敏感)的文件,输出文件名、行号、该行内容
grep -r -Hn -P '\bCharacterString\b'    # 搜索"正则表达式 元字符 metacharacter"查看详情
递归查找当前目录下所有包含某单词(Character的大小写不敏感)的文件,输出文件名、行号、该行内容
grep -r -Hn -P '\b(?i)Character(?-i)String\b'  # 搜索"正则表达式 模式修改符"查看详情

完。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值