Linux grep命令

应用实例

  1. grep ‘pattern’ filename 搜索filename文章内容
  2. grep ‘pattern’ filename1 filename2 搜索两篇文件内容
  3. grep ‘pattern’ * 搜索当前目录内容(语法的意思搜索所有文件)
  4. grep -r ‘pattern’ * 递归搜索,包含子文件
  5. grep -l -r ‘pattern’ * 结果只返回匹配的文件名

语法

grep [OPTIONS] PATTERN [FILE…]
grep [OPTIONS] [-e PATTERN]… [-f FILE]… [FILE…]

参数

匹配选择

参数描述
-E, –extended-regexp拓展的正则表达式
-F, –fixed-stringsInterpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.
-G, –basic-regexp基础的正则表达式
-P, –perl-regexpInterpret the pattern as a Perl-compatible regular expression (PCRE).

匹配控制

参数描述
-e PATTERN, –regexp=PATTERNUse PATTERN as the pattern.
-f FILE, –file=FILEObtain patterns from FILE, one per line.
-i, –ignore-case忽略大小写
-v, –invert-match反选
-w, –word-regexp只匹配完整的单词才返回
-x, –line-regexp只匹配完整的行才返回
-yObsolete synonym for -i.

一般的输出控制

参数描述
-c, –count压缩标准输出; 打印匹配数
-L, –files-without-match压缩标准的输出; 输出不匹配的文件名
-l, –files-with-matches压缩标准输出; 只显示文件名
-m NUM, –max-count=NUMStop reading a file after NUM matching lines.
-o, –only-matchingPrint only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-q, –quiet, –silentdo not write anything to standard output.
-s, –no-messagesSuppress error messages about nonexistent or unreadable files.

输出行前缀控制

参数描述
-b, –byte-offsetPrint the 0-based byte offset within the input file before each line of output.
-H, –with-filenamePrint the file name for each match.
-h, –no-filenameSuppress the prefixing of file names on output.
-n, –line-number打印行标
-T, –initial-tabMake sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal.
-u, –unix-byte-offsetsReport Unix-style byte offsets.
-Z, –nullOutput a zero byte (the ASCII NUL character) instead of the character that normally follows a file name.

内容行控制

参数描述
-A NUM, –after-context=NUMPrint NUM lines of trailing context after matching lines.
-B NUM, –before-context=NUMPrint NUM lines of leading context before matching lines.
-C NUM, -NUM, –context=NUMPrint NUM lines of output context.

文件路径选择

参数描述
-a, –textProcess a binary file as if it were text
-D ACTION, –devices=ACTIONIf an input file is a device, FIFO or socket, use ACTION to process it.
-d ACTION, –directories=ACTIONIf an input file is a directory, use ACTION to process it.
-IProcess a binary file as if it did not contain matching data;
-r, –recursive递归搜索子文件夹
-R, –dereference-recursiveRead all files under each directory, recursively.

## 其他选择

参数描述
-U, –binaryTreat the file(s) as binary.
-z, –null-dataTreat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.

参考文献:
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值