grep --help
匹配模式选择:
-E, --extended-regexp 扩展正则表达式egrep
-F, --fixed-strings 一个换行符分隔的字符串的集合fgrep
-G, --basic-regexp 基本正则
-P, --perl-regexp 调用的perl正则
-e, --regexp=PATTERN 后面根正则模式,默认无
-f, --file=FILE 从文件中获得匹配模式
-i, --ignore-case 不区分大小写
-w, --word-regexp 匹配整个单词
-x, --line-regexp 匹配整行
-z, --null-data a data line ends in 0 byte, not newline
杂项:
-s, --no-messages 不显示不存在或无匹配文本的错误信息
-v, --invert-match 显示不包含匹配文本的所有行
-V, --version 显示版本号
--help 显示帮助信息
--mmap use memory-mapped input if possible
输入控制:
-m, --max-count=NUM 匹配的最大数
-b, --byte-offset 打印匹配行前面打印该行所在的块号码。
-n, --line-number 显示匹配行及行号
--line-buffered 刷新输出每一行
-H, --with-filename 当搜索多个文件时,显示匹配文件名前缀
-h, --no-filename 当搜索多个文件时,不显示匹配文件名前缀
--label=LABEL print LABEL as filename for standard input
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent 不显示任何东西
--binary-files=TYPE assume that binary files are TYPE
TYPE is 'binary', 'text', or 'without-match'
-a, --text 匹配二进制的东西
-I 不匹配二进制的东西
-d, --directories=ACTION 目录操作,读取,递归,跳过
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION 设置对设备,FIFO,管道的操作,读取,跳过
ACTION is 'read' or 'skip'
-R, -r, --recursive 递归调用
--include=PATTERN files that match PATTERN will be examined
--exclude=PATTERN files that match PATTERN will be skipped.
--exclude-from=FILE files that match PATTERN in FILE will be skipped.
-L, --files-without-match 匹配多个文件时,显示不匹配的文件名
-l, --files-with-matches 查询多文件时只输出包含匹配字符的文件名
-c, --count 打印匹配的行数
-Z, --null print 0 byte after FILE name
文件控制:
-B, --before-context=NUM 打印匹配本身以及前面的几个行由NUM控制
-A, --after-context=NUM 打印匹配本身以及随后的几个行由NUM控制
-C, --context=NUM 打印匹配本身以及随后,前面的几个行由NUM控制
-NUM 根-C的用法一样的
--color[=WHEN],
--colour[=WHEN] use markers to distinguish the matching string
WHEN may be `always', `never' or `auto'.
-U, --binary do not strip CR characters at EOL (MSDOS)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)
#例子:显示D/BatteryService( 2732): update start'的上下两行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -2 'D/BatteryService( 2732): update start' xiami.log
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -b 'D/BatteryService( 2732): update start' xiami.log
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -c 'D/BatteryService( 2732): update start' xiami.log 21
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -h 'D/BatteryService( 2732): update start' xiami.log testgrep 09-03 10:59:48.537 D/BatteryService( 2732): update start 09-03 11:00:28.601 D/BatteryService( 2732): update start
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -q 'D/BatteryService( 2732): update start' xiami.log bixiaopeng@bixiaopengtekiMacBook-Pro ~$ echo $? 0 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ ls -al |grep -q 'bixiaopeng' xiami.log bixiaopeng@bixiaopengtekiMacBook-Pro ~$ echo $? 1
#查看哪个文件中包含字符 bixiaopeng bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -l 'bixiaopeng' xiami.log testgrep testgrep
#查看哪个文件中不包含字符 bixiaopeng bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -L 'bixiaopeng' xiami.log testgrep xiami.log
#打印出bixiaopeng所在的行号 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -n 'bixiaopeng' testgrep 10:my name is bixiaopeng 11:bixiaopeng is my name
#不加-s的情况 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -n 'hahahah' testgreps grep: testgreps: No such file or directory #加了-s以后 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -s 'hahahah' testgreps bixiaopeng@bixiaopengtekiMacBook-Pro ~$
#显示testgrep文件中不包含hahahah的行 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -v 'hahahah' testgrep wirelessqa is my blog wirelestqa is testfile wirelesmqa is testfile mmmqa is testfile mmqa mqa is testfile Wirelessqa is my blog my blog is wirelessqa my name is bixiaopeng bixiaopeng is my name bxp is my name my name is bxp my name is (bxp) b$##
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -w '\<bxp\>' testgrep bxp is my name my name is bxp my name is (bxp)
bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -V grep (BSD grep) 2.5.1-FreeBSD
#如果不加 -i搜索结果如下,大写的Wirelessqa不会被显示 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -w '\<wirelessqa\>' testgrep wirelessqa is my blog my blog is wirelessqa #如果加上-i 结果如下 bixiaopeng@bixiaopengtekiMacBook-Pro ~$ grep -wi '\<wirelessqa\>' testgrep wirelessqa is my blog Wirelessqa is my blog my blog is wirelessqa