grep的常用和次常用选项

来源:互联网 发布:mac os x 10.10.5 编辑:程序博客网 时间:2024/04/30 02:21
选项 选项2 英文 翻译 -P –perl-regexp PATTERN is a Perl regular expression 使用Perl正则表达式语法 -i –ignore-case ignore case distinctions 忽略大小写 -v –invert-match select non-matching lines 选择未匹配的那些行 -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 每个文件输出一个匹配的行数 -n –line-number print line number with output lines 输出匹配的数据的同时打印行号 -H –with-filename print the file name for each match 对于每一处匹配,都打印文件名 -h –no-filename suppress the file name prefix on output 在输出时,不输出文件名前缀(详见下方解释) -o –only-matching show only the part of a line matching PATTERN 不打印整行,只显示匹配到的那一部分文字 -r –recursive like –directories=recurse 递归。如果没有指定目录,则取当前目录,否则使用指定目录 -B –before-context=NUM print NUM lines of leading context 显示匹配到的行的同时,显示其前面的#行 -A –after-context=NUM print NUM lines of trailing context 显示匹配到的行的同时,并显示后续的#行 -C –context=NUM print NUM lines of output context 显示匹配到的行的同时,并显示其前面的#行和后面的#行 无 –exclude-dir=DIR Exclude directories matching the pattern DIR from recursive searches. 递归搜索中排除匹配到的目录 无 –exclude=GLOB Skip files whose base name matches GLOB
(using wildcard matching). 如果通配符匹配到了某个文件名,那么跳过这个文件名 无 –include=GLOB Search only files whose base name matches GLOB
(using wildcard matching as described under –exclude). 只搜索那些符合通配符的文件

grep的部分帮助信息

[root@localhost ~]# grep --helpUsage: 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.orgGNU 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'  # 搜索"正则表达式 模式修改符"查看详情

完。

0 0
原创粉丝点击