{"id":97,"date":"2013-05-06T19:22:06","date_gmt":"2013-05-06T11:22:06","guid":{"rendered":"http:\/\/coderbee.net\/?p=97"},"modified":"2013-05-06T15:23:49","modified_gmt":"2013-05-06T07:23:49","slug":"grep-%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/linux\/20130506\/97","title":{"rendered":"grep \u547d\u4ee4"},"content":{"rendered":"<p>grep\u662f\u4e00\u4e2a\u6700\u521d\u7528\u4e8eUnix\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5728\u7ed9\u51fa\u6587\u4ef6\u5217\u8868\u6216\u6807\u51c6\u8f93\u5165\u540e\uff0cgrep\u4f1a\u5bf9\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6587\u672c\u8fdb\u884c\u641c\u7d22\uff0c\u5e76\u53ea\u8f93\u51fa\u5339\u914d\uff08\u6216\u8005\u4e0d\u5339\u914d\uff09\u7684\u884c\u6216\u6587\u672c\u3002<\/p>\n<p>egrep\u662fgrep\u7684\u6269\u5145\u7248\u672c\uff0c\u6539\u826f\u4e86grep\u4e0d\u80fd\u6216\u4e0d\u65b9\u4fbf\u7684\u64cd\u4f5c\uff0c\u4e3b\u8981\u662f\u6b63\u5219\u5339\u914d\u65b9\u9762\u7684\u6269\u5c55\u3002<\/p>\n<p>fgrep\u4e0d\u505a\u6b63\u5219\u5904\u7406\uff0c\u8868\u8fbe\u5f0f\u4ec5\u4f5c\u4e3a\u4e00\u822c\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u6240\u6709\u6b63\u5219\u5143\u5b57\u7b26\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<h2>\u8c03\u7528grep<\/h2>\n<p>\u4e00\u822c\u7684grep\u547d\u4ee4\u884c\u662f\uff1a<code>grep  options   pattern  input_file_names<\/code><\/p>\n<p>\u53ef\u4ee5\u6709\u96f6\u4e2a\u6216\u591a\u4e2aoptions\u548cinput_file_names\uff0c\u53ef\u4ee5\u7528<code>-e  pattern<\/code>\u6216<code>-f  file<\/code>\u9009\u9879\u6765\u6307\u5b9a\u591a\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<h2>grep \u5e38\u7528\u9009\u9879<\/h2>\n<ul>\n<li><code>-v<\/code>\uff1a\u9006\u53cd\u6a21\u5f0f\uff0c\u53ea\u8f93\u51fa\u4e0d\u5339\u914d\u7684\u884c\u3002<\/li>\n<li><code>-r<\/code>\uff1a\u9012\u5f52\u641c\u7d22\u76ee\u5f55\u3002<\/li>\n<li><code>-q<\/code>\uff1a\u9759\u9ed8\u6a21\u5f0f\uff0c\u4e0d\u8f93\u51fa\u4efb\u4f55\u7ed3\u679c\uff08stderr\u9664\u5916\uff0c\u7528\u4e8e\u83b7\u53d6\u547d\u4ee4\u8fd4\u56de\u503c\uff0c\u6709\u5339\u914d\u4e3atrue\uff0c\u5426\u5219\u4e3afalse\uff09\u3002<\/li>\n<li><code>-i<\/code>\uff1a\u5ffd\u7565\u5927\u5c0f\u5199\u533a\u522b\u3002<\/li>\n<li><code>-w<\/code>\uff1a\u6574\u4e2a\u5355\u8bcd\u5339\u914d\u3002<\/li>\n<li><code>-n<\/code>\uff1a\u540c\u65f6\u8f93\u51fa\u884c\u53f7\u3002<\/li>\n<li><code>-c<\/code>\uff1a\u53ea\u8f93\u51fa\u5339\u914d\u7684\u884c\u6570\u3002<\/li>\n<li><code>-l<\/code>\uff1a\u53ea\u8f93\u51fa\u542b\u6709\u5339\u914d\u884c\u7684\u6587\u4ef6\u540d\u3002<\/li>\n<li><code>-E<\/code>\uff1a\u5207\u6362\u5230egrep\u3002<\/li>\n<\/ul>\n<h2>\u4e3e\u4f8b<\/h2>\n<p><code>grep  -l  'main'   *.c<\/code>\uff1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709c\u6e90\u6587\u4ef6\u91cc\u641c\u7d22\u542b\u6709<code>main<\/code>\u7684\u6587\u4ef6\uff0c\u53ea\u8f93\u51fa\u6587\u4ef6\u540d\u3002<\/p>\n<p><code>grep  -r  'hello'  \/home\/gigi<\/code>\uff1a\u9012\u5f52\u5730\u641c\u7d22\u76ee\u5f55\u3002<\/p>\n<p><code>find  \/home\/gigi  -name  \"*.c\"  -print0  | xargs  -0r  grep  -H  'hello'<\/code>\uff1a\u5728<code>\/home\/gigi<\/code>\u76ee\u5f55\u4e0b\u7684\u6240\u6709c\u6587\u4ef6\u91cc\u641c\u7d22<code>hello<\/code>\uff0c\u7c7b\u4f3c\u4e8e\uff1a<code>grep  -rH  --include='*.c'  'hello'   \/home\/gigi<\/code>\u3002<\/p>\n<p><code>grep  -e   '--cut  here--'   *<\/code>\uff1a\u5982\u679c\u6a21\u5f0f\u4ee5<code>-<\/code>\u5f00\u5934\uff0c\u9700\u8981\u4f7f\u7528<code>-e<\/code>\u9009\u9879\uff0c\u5426\u5219grep\u662f\u5c1d\u8bd5\u5f53\u4f5c\u9009\u9879\u5217\u8868\u6765\u89e3\u6790\u3002<\/p>\n<p><code>grep  -w  'hello'  *<\/code>\uff1a\u641c\u7d22\u4e00\u4e2a\u5b8c\u6574\u7684\u5355\u8bcd\uff0c\u800c\u4e0d\u662f\u4f5c\u4e3a\u5355\u8bcd\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p><code>grep  -C  2  'hello'   *<\/code>\uff1a\u8f93\u51fa\u5339\u914d\u884c\u7684\u4e0a\u4e0b\u6587\uff0c\u524d\u540e\u54042\u884c\u3002<\/p>\n<p><code>grep  -H  'eli'  \/etc\/passwd  \/dev\/null<\/code>\uff1a\u5f3a\u5236grep\u8f93\u51fa\u5339\u914d\u884c\u6240\u5728\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p><code>ps  -ef  |  grep  '[c]ron<\/code>\uff1a\u8fd9\u91cc\u4e4b\u6240\u4ee5\u8981\u4f7f\u7528\u5b57\u7b26\u96c6<code>[]<\/code>\u6a21\u5f0f\uff0c\u662f\u4e3a\u4e86\u907f\u514d\u5339\u914d\u5230\u8fd9\u6761\u547d\u4ee4\u672c\u8eab\u3002<\/p>\n<p><code>grep  'paul'  \/etc\/motd  |  grep  'franc,ois'<\/code>\uff1agrep\u6ca1\u6cd5\u8868\u793a\u4e00\u4e2a\u903b\u8f91\u4e0e\u7684\u6982\u5ff5\uff0c\u5982\u679c\u9700\u8981\u540c\u65f6\u5339\u914d\u591a\u4e2a\u6a21\u5f0f\uff0c\u9700\u8981\u4f7f\u7528\u591a\u4e2agrep\u547d\u4ee4\u3002<\/p>\n<p><code>cat  \/etc\/passwd  |  grep  'alain'  -  \/etc\/motd<\/code>\uff1a\u540c\u65f6\u5728\u6807\u51c6\u8f93\u5165\u548c\u8f93\u5165\u6587\u4ef6\u91cc\u67e5\u627e\u3002<code>-<\/code>\u8868\u793a\u6807\u51c6\u8f93\u5165\u3002<\/p>\n<p><code>echo -e \"123321\\nabcxyz\\nabcba\" | grep -e '\\(.\\)\\(.\\).\\2\\1'<\/code>\uff1a\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u56de\u6587\u3002\u80fd\u591f\u5339\u914d\u7684\u56de\u6587\u591a\u8fbe19\u4e2a\u5b57\u7b26\uff1a<br \/>\n     <code>echo \"1234567890987654321\" | grep -E -e '^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$'<\/code><\/p>\n<p><!--more--><\/p>\n<h2>grep\u5b8c\u6574\u7684\u9009\u9879<\/h2>\n<ul>\n<li>\u5339\u914d\u63a7\u5236\n<ul>\n<li><code>-e  pattern,  --regexp=pattern<\/code>\uff1a\u7528<code>pattern<\/code>\u4f5c\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8fd9\u7528\u4e8e\u6307\u5b9a\u591a\u4e2a\u641c\u7d22\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6216\u7528\u4e8e\u4fdd\u62a4\u4ee5<code>-<\/code>\u5f00\u5934\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>-e<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<li><code>-f file, --file=File<\/code>\uff1a \u4ece\u6587\u4ef6\u4e2d\u63d0\u53d6\uff0c\u7a7a\u6587\u4ef6\u4e2d\u5305\u542b0\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u4ec0\u4e48\u90fd\u4e0d\u5339\u914d\uff08<code>-f<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<li><code>-i, --ignore-case,  -y<\/code>\uff1a\u5ffd\u7565\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u8f93\u5165\u6587\u4ef6\u7684\u5927\u5c0f\u5199\u5dee\u522b\u3002<code>-y<\/code>\u662f\u4e00\u4e2a\u8fc7\u65f6\u7684\u540c\u4e49\u8bcd\uff0c\u7528\u4e8e\u517c\u5bb9\u6027\uff08<code>-i<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<li><code>-v, --revert-match<\/code>\uff1a\u53cd\u68c0\u7d22\uff0c\u53ea\u9009\u62e9\u4e0d\u5339\u914d\u7684\u884c\uff08<code>-v<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<li><code>-w, --word-regexp<\/code>\uff1a\uff08\u628a\u6a21\u5f0f\u5f53\u4f5c\u6574\u4e2a\u5355\u8bcd\uff0c\u800c\u4e0d\u662f\u5355\u8bcd\u7684\u4e00\u90e8\u5206\u6765\u5339\u914d\uff09\u53ea\u9009\u62e9\u90a3\u4e9b\u5185\u5bb9\u91cc\u5339\u914d\u6574\u4e2a\u5355\u8bcd\u7684\u884c\u3002\u6d4b\u8bd5\u89c4\u5219\u662f\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u5728\u884c\u7684\u5f00\u5934\u6216\u524d\u9762\u662f\u975e\u5355\u8bcd\u7ec4\u6210\u5b57\u7b26\uff0c<br \/>\n       \u7c7b\u4f3c\u5730\uff0c\u5b83\u5fc5\u987b\u5728\u884c\u7684\u7ed3\u5c3e\u6216\u540e\u63a5\u4e00\u4e2a\u975e\u5355\u8bcd\u7ec4\u6210\u5b57\u7b26\u3002\u5355\u8bcd\u7ec4\u6210\u5b57\u7b26\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u3002\uff08 \u5982\u679c\u88ab <code>\\&lt;<\/code> \u548c <code>\\&gt;<\/code> \u5f15\u7528\uff0c\u5c31\u628a\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u8bcd\u641c\u7d22\u3002\uff09<\/li>\n<li><code>-x,  --line-regexp<\/code>\uff1a\u53ea\u9009\u62e9\u90a3\u4e9b\u521a\u597d\u5339\u914d\u6574\u884c\u7684\u884c\uff08<code>-x<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e00\u822c\u7684\u8f93\u51fa\u63a7\u5236\n<ul>\n<li><code>-c, --count<\/code>\uff1a\u53ea\u6253\u5370\u6bcf\u4e2a\u6587\u4ef6\u7684\u5339\u914d\u884c\u6570\uff0c\u4e0d\u663e\u793a\u5339\u914d\u7684\u5185\u5bb9\u3002\u548c<code>-v,  --invert-match<\/code>\u9009\u9879\u4e00\u8d77\u65f6\uff0c\u8ba1\u7b97\u4e0d\u5339\u914d\u7684\u884c\u6570\u3002\uff08<code>-c<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<li><code>--color[=WHEN],  --colour[=WHEN]<\/code>\uff1a<\/li>\n<li><code>-l, --files-with-matches<\/code>\uff1a\u6253\u5370\u5339\u914d\u6a21\u677f\u7684\u6587\u4ef6\u6e05\u5355\u3002<\/li>\n<li><code>-L, --files-without-match<\/code>\uff1a\u6253\u5370\u4e0d\u5339\u914d\u6a21\u677f\u7684\u6587\u4ef6\u6e05\u5355\u3002<\/li>\n<li><code>-m num,  --max-count=num<\/code>\uff1a\u5728\u4e00\u4e2a\u6587\u4ef6\u627e\u5230num\u4e2a\u5339\u914d\u540e\u505c\u6b62\u8bfb\u53d6\u3002<\/li>\n<li><code>-o,  --only-matching<\/code>\uff1a\u53ea\u663e\u793a\u5339\u914d\u884c\u7684\u5339\u914d\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u5339\u914d\u90e8\u5206\u5355\u72ec\u4e00\u884c\u8f93\u51fa\u3002<\/li>\n<li><code>-q, --quiet,  --silent<\/code>\uff1a\u5b89\u9759\uff0c\u4e0d\u8f93\u51fa\u4efb\u4f55\u4fe1\u606f\u5230stdout\u3002\u5728\u627e\u5230\u4efb\u4f55\u5339\u914d\u540e\u7acb\u5373\u4ee5\u72b6\u60010\u9000\u51fa\uff0c\u5373\u4f7f\u68c0\u6d4b\u5230\u9519\u8bef\u3002\uff08<code>-q<\/code>\u7531POSIX\u89c4\u5b9a\uff09\u3002<\/li>\n<li><code>-s, --no-messages<\/code>\uff1a\u4e0d\u663e\u793a\u5173\u4e8e\u4e0d\u5b58\u5728\u6216\u8005\u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8f93\u51fa\u884c\u524d\u7f00\u63a7\u5236\n<ul>\n<li><code>-b, --byte-offset<\/code>\uff1a\u5728\u5339\u914d\u884c\u524d\u9762\u6253\u5370\u8be5\u884c\u6240\u5728\u8f93\u5165\u6587\u4ef6\u5185\u7684\u5b57\u8282\u504f\u79fb\uff0c\u504f\u79fb\u662f\u57fa\u4e8e0\u7684\u3002\u5982\u679c\u6307\u5b9a\u4e86<code>-o,  --only-matching<\/code>\uff0c\u6253\u5370\u5339\u914d\u90e8\u5206\u81ea\u5df1\u7684\u504f\u79fb\u3002<br \/>\n  \u5f53grep\u5141\u8bb8\u5728Ms-DOS\u6216MS-Windows\uff0c\u6253\u5370\u7684\u5b57\u8282\u504f\u79fb\u53d6\u51b3\u4e8e\u662f\u5426\u4f7f\u7528\u4e86<code>-u,  --unix-byte-offsets<\/code>\u9009\u9879\u3002<\/li>\n<li><code>-h, --no-filename<\/code>\uff1a\u5728\u8f93\u51fa\u4e0a\u4e0d\u524d\u7f00\u6587\u4ef6\u540d\u3002\u5f53\u53ea\u641c\u7d22\u4e00\u4e2a\u6587\u4ef6\u65f6\uff0c\u8fd9\u662f\u9ed8\u8ba4\u7684\u3002<\/li>\n<li><code>-H,  --with-filename<\/code>\uff1a\u5728\u6bcf\u4e2a\u5339\u914d\u524d\u6253\u5370\u6587\u4ef6\u540d\u3002\u5f53\u641c\u7d22\u6587\u4ef6\u4e0d\u6b62\u4e00\u4e2a\u65f6\uff0c\u8fd9\u662f\u9ed8\u8ba4\u7684\u3002<\/li>\n<li><code>--label=LABEL<\/code>\uff1aDisplay input actually coming from standard input as input coming from file LABEL.<br \/>\n  This is especially useful when implementing tools like zgrep; e.g.: <code>gzip -cd foo.gz | grep --label=foo -H something<\/code><\/li>\n<li><code>-n, --line-number<\/code>\uff1a\u5728\u5339\u914d\u7684\u884c\u524d\u9762\u6253\u5370\u6240\u5728\u8f93\u5165\u6587\u4ef6\u5185\u7684\u884c\u53f7\uff0c\u884c\u53f7\u662f\u57fa\u4e8e1\u7684\u3002<\/li>\n<li><code>-T,  --initial-tab<\/code>\uff1aMake sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal.<br \/>\n  This is useful with options that prefix their output to the actual content: -H, -n, and -b. In order to improve the probability that lines from a single file will all start at the same column,<br \/>\n  this also causes the line number and byte offset (if present) to be printed in a minimum-size field width.<\/li>\n<li><code>-u,  --unix-byte-offsets<\/code>\uff1aReport Unix-style byte offsets. This option causes grep to report byte offsets as if the file were a Unix-style text file, i.e., the byte offsets ignore the CR<br \/>\n   characters that were stripped. This will produce results identical to running grep on a Unix machine. This option has no effect unless the -b option is also used;<br \/>\n  it has no effect on platforms other than MS-DOS and MS-Windows.<\/li>\n<li><code>-z,  --null<\/code>\uff1aOutput a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, \u2018grep -lZ\u2019 outputs a zero byte after each file<br \/>\n  name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines.<br \/>\n  This option can be used with commands like \u2018find -print0\u2019, \u2018perl -0\u2019, \u2018sort -z\u2019, and \u2018xargs -0\u2019 to process arbitrary file names, even those that contain newline characters.<\/li>\n<\/ul>\n<\/li>\n<li>\u4e0a\u4e0b\u6587\u884c\u63a7\u5236\n<ul>\n<li><code>-A num,  --after-context=num<\/code>\uff1a\u6253\u5370\u5339\u914d\u884c\u540e\u9762\u7684num\u884c\u3002<\/li>\n<li><code>-B num,  --before-context=num<\/code>\uff1a\u6253\u5370\u5339\u914d\u884c\u524d\u9762\u7684num\u884c\u3002<\/li>\n<li><code>-C num,  --context=num,  -num<\/code>\uff1a\u6253\u5370\u5339\u914d\u884c\u524d\u9762\u7684num\u884c\u548c\u540e\u9762\u7684num\u884c\u3002\u5982 <code>grep -2 pattern file<\/code> \uff0c\u540c\u65f6\u663e\u793a\u5339\u914d\u884c\u7684\u4e0a\u4e0b2\u884c\u3002<\/li>\n<li><code>--group-seprator=string<\/code>\uff1a\u5f53\u4f7f\u7528\u4e86<code>-A,  -B,  -C<\/code>\u9009\u9879\u65f6\uff0cprint string instead of &#8212; around disjoint groups of lines.\uff08\u6ca1\u5565\u6548\u679c\uff09<\/li>\n<li><code>--no-group-seprator<\/code>\uff1a\u5f53\u4f7f\u7528\u4e86<code>-A,  -B,  -C<\/code>\u9009\u9879\u65f6\uff0c print disjoint groups of lines adjacent to each other.\uff08\u6ca1\u5565\u6548\u679c\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\u6587\u4ef6\u548c\u76ee\u5f55\u9009\u62e9\n<ul>\n<li><code>-a,  --text<\/code>\uff1a\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u5f53\u4f5c\u6587\u672c\u5904\u7406\uff1b\u8fd9\u7b49\u4ef7\u4e8e<code>--binary-files=text<\/code>\u9009\u9879\u3002<\/li>\n<li><code>--binary-files=type<\/code>\uff1a\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u7684\u8d44\u6e90\u914d\u7f6e\u5143\u6570\u636e\u6216\u5b83\u7684\u5f00\u5934\u51e0\u4e2a\u5b57\u8282\u8868\u660e\u6587\u4ef6\u5305\u542b\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5047\u8bbe\u6587\u4ef6\u662f<code>type<\/code>\u7c7b\u578b\u7684\u3002\u9ed8\u8ba4\u5730\uff0c<code>type<\/code>\u662f<code>binary<\/code>\uff0c<br \/>\n  grep\u4e00\u822c\u8f93\u51fa\u4e00\u4e2a\u5355\u884c\u4fe1\u606f\u8bf4\u660e\u5339\u914d\u5230\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6216\u4e0d\u8f93\u51fa\u4fe1\u606f\u5982\u679c\u6ca1\u6709\u5339\u914d\u3002<br \/>\n  \u5982\u679c<code>type<\/code>\u662f<code>without-match<\/code>\uff0cgrep\u5047\u8bbe\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e0d\u5339\u914d\uff0c\u8fd9\u7b49\u4ef7\u4e8e<code>-I<\/code>\u9009\u9879\u3002<br \/>\n  \u5982\u679c<code>type<\/code>\u662f<code>text<\/code>\uff0cgrep\u628a\u4e8c\u8fdb\u5236\u6587\u4ef6\u5f53\u4f5c\u6587\u672c\u5904\u7406\uff0c\u7b49\u4ef7\u4e8e<code>-a<\/code>\u9009\u9879\u3002<br \/>\n  \u8b66\u544a\uff1a<code>--binary-files=text<\/code>\u53ef\u80fd\u8f93\u51fa\u4e8c\u8fdb\u5236\u5783\u573e\uff0c\u8fd9\u53ef\u80fd\u5e26\u6765\u4e25\u91cd\u7684\u8d1f\u9762\u4f5c\u7528\uff0c\u5982\u679c\u8f93\u51fa\u662f\u7ec8\u7aef\uff0c\u800c\u7ec8\u7aef\u9a71\u52a8\u628a\u4e00\u4e9b\u8f93\u51fa\u89e3\u91ca\u4e3a\u547d\u4ee4\u3002<\/li>\n<li><code>-D  action,  --devices=action<\/code>\uff1a\u5982\u679c\u8f93\u5165\u6587\u4ef6\u662f\u8bbe\u5907\u3001FIFO\u6216socket\uff0c\u7528<code>action<\/code>\u6765\u5904\u7406\u3002\u5982\u679caction\u662fread\uff0c\u6240\u6709\u8bbe\u5907\u88ab\u5f53\u4f5c\u666e\u901a\u6587\u4ef6\u8bfb\u53d6\u3002\u5982\u679caction\u662fskip\uff0c\u8bbe\u5907\u3001FIFO\u548csockets\u88ab\u8df3\u8fc7\u3002<br \/>\n  By default, devices are read if they are on the command line or if the -R (&#8211;dereference-recursive) option is used, and are skipped if they are encountered recursively and the -r (&#8211;recursive)<br \/>\n  option is used. This option has no effect on a file that is read via standard input.<\/li>\n<li><code>-d  action,  --directories=action<\/code>\uff1aIf an input file is a directory, use action to process it. By default, action is \u2018read\u2019, which means that directories are read just as if they were ordinary<br \/>\n  files (some operating systems and file systems disallow this, and will cause grep to print error messages for every directory or silently skip them). If action is \u2018skip\u2019, directories are silently<br \/>\n   skipped. If action is \u2018recurse\u2019, grep reads all files under each directory, recursively, following command-line symbolic links and skipping other symlinks; this is equivalent to the -r option.<\/li>\n<li><code>--exclude=glob<\/code>\uff1a\u8df3\u8fc7base name\uff08\u6587\u4ef6\u540d\uff0c\u4e0d\u5305\u62ec\u8def\u5f84\uff09\u5339\u914dglob\uff08\u4f7f\u7528\u901a\u914d\u7b26\u5339\u914d\uff09\u7684\u6587\u4ef6\u3002\u6587\u4ef6\u540dglob\u53ef\u4ee5\u4f7f\u7528<code>*<\/code>, <code>?<\/code>,  <code>[...]<\/code>\u4f5c\u4e3a\u901a\u914d\u7b26\uff0c\u7528<code>\\<\/code>\u8f6c\u4e49\u901a\u914d\u7b26\u3002<\/li>\n<li><code>--exclude-from=file<\/code>\uff1a\u8df3\u8fc7base name\u5339\u914d\u4efb\u4f55\u4ecefile\u91cc\u8bfb\u53d6\u7684\u6587\u4ef6\u540dglob\uff08\u4f7f\u7528\u5728<code>--exclude<\/code>\u63cf\u8ff0\u7684\u901a\u914d\u7b26\u5339\u914d\u3002\uff09<\/li>\n<li><code>--exclude-dir=dir<\/code>\uff1a\u5728\u9012\u5f52\u641c\u7d22\u4e2d\u6392\u9664\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0fpattern\u7684\u76ee\u5f55\u3002<\/li>\n<li><code>-I<\/code>\uff1a\u4e0d\u5339\u914d\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u7b49\u4ef7\u4e8e<code>--binary-files=without-match<\/code>\u9009\u9879\u3002<\/li>\n<li><code>--include=glob<\/code>\uff1a\u53ea\u641c\u7d22base name\u5339\u914dglob\u7684\u6587\u4ef6\u3002\uff08\u4f7f\u7528\u5728<code>--exclude<\/code>\u63cf\u8ff0\u7684\u901a\u914d\u7b26\u5339\u914d\u3002\uff09<\/li>\n<li><code>-r,  --recursive<\/code>\uff1a\u9012\u5f52\u5904\u7406\u76ee\u5f55\u3002\u89e3\u6790\u7b26\u53f7\u94fe\u63a5\uff0c\u4f46\u8df3\u8fc7\u9012\u5f52\u5f15\u7528\u7684\u94fe\u63a5\u3002\u7b49\u4ef7\u4e8e<code>--directories=recurse<\/code>\u9009\u9879\u3002<\/li>\n<li><code>-R,  --dereference-recursive<\/code>\uff1a\u9012\u5f52\u5904\u7406\u76ee\u5f55\uff0c\u89e3\u6790\u6240\u6709\u7b26\u53f7\u94fe\u63a5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e00\u822c\u7684\u7a0b\u5e8f\u4fe1\u606f\n<ul>\n<li><code>--help<\/code>\uff1a\u8f93\u51fa\u547d\u4ee4\u884c\u9009\u9879\u7684\u7b80\u6d01\u4f7f\u7528\u4fe1\u606f\uff0c\u548cbug\u62a5\u544a\u5730\u5740\uff0c\u7136\u540e\u9000\u51fa\u3002<\/li>\n<li><code>-V, --version<\/code>\uff1a\u663e\u793a\u8f6f\u4ef6\u7248\u672c\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5176\u4ed6\u9009\u9879\n<ul>\n<li><code>--line-buffered<\/code>\uff1a\u5728\u8f93\u51fa\u4e0a\u4f7f\u7528\u884c\u7f13\u5b58\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u6027\u80fd\u4ee3\u4ef7\u3002<\/li>\n<li><code>--mmap<\/code>\uff1a\u8fd9\u4e2a\u9009\u9879\u5df2\u88ab\u5e9f\u5f03\u3002<\/li>\n<li><code>-U,  --binary<\/code>\uff1a\u628a\u6587\u4ef6\u5f53\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/li>\n<li><code>z,  --null-data<\/code>\uff1a\u628a\u8f93\u5165\u5f53\u4f5c\u884c\u7684\u96c6\u5408\uff0c\u6bcf\u4e2a\u7531\u5b57\u82820\uff08ASCII\u7684NULL\u5b57\u7b26\uff09\u800c\u4e0d\u662f\u6362\u884c\u7ed3\u675f\u3002\u548c<code>-Z,  --null<\/code>\u9009\u9879\u4e00\u6837\uff0c\u8fd9\u4e2a\u9009\u9879\u53ef\u7528\u4e8e\u548c\u547d\u4ee4\u5982<code>sort  -z<\/code>\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u5904\u7406\u4efb\u610f\u6587\u4ef6\u540d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>grep\u5982\u4f55\u9009\u62e9\u524d\u7f00\u5b57\u6bb5\u4e0e\u5185\u5bb9\u884c\u4e4b\u95f4\u7684\u5206\u9694\u7b26<\/h3>\n<ul>\n<li>\u5339\u914d\u884c\u4e00\u822c\u4f7f\u7528\u5192\u53f7<code>:<\/code>\u4f5c\u4e3a\u524d\u7f00\u5b57\u6bb5\u4e0e\u5185\u5bb9\u884c\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u3002<\/li>\n<li>\u4e0a\u4e0b\u6587\u884c\u4f7f\u7528<code>-<\/code>\u3002<\/li>\n<li>\u5f53\u6ca1\u6709\u6307\u5b9a\u4e0a\u4e0b\u6587\uff0c\u5339\u914d\u884c\u53ea\u662f\u7b80\u5355\u5730\u8ddf\u5728\u524d\u4e00\u4e2a\u7684\u540e\u9762\u8f93\u51fa\u3002<\/li>\n<li>When nonzero context is specified, lines that are adjacent in the input form a group and are output one right after another, while a separator appears by default<br \/>\n between disjoint groups on a line of its own and without any prefix.<\/li>\n<li>The default separator is \u2018&#8211;\u2019, however whether to include it and its appearance can be changed with the options above.<\/li>\n<li>Each group may contain several matching lines when they are close enough to each other that two otherwise adjacent but divided groups connect and can just merge into a single contiguous one.<\/li>\n<\/ul>\n<h2>\u53c2\u8003\u94fe\u63a5<\/h2>\n<ul>\n<li><a href=\"http:\/\/www.gnu.org\/software\/grep\/manual\/grep.html\">http:\/\/www.gnu.org\/software\/grep\/manual\/grep.html<\/a><\/li>\n<\/ul>\n<h2>\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\u96c6\uff08\u57fa\u672c\u96c6\uff09<\/h2>\n<pre><code>\n^           \u951a\u5b9a\u884c\u7684\u5f00\u59cb\u3002\n$           \u951a\u5b9a\u884c\u7684\u7ed3\u675f\u3002\n.            \u53e5\u70b9\uff0c\u5339\u914d\u4e00\u4e2a\u975e\u6362\u884c\u7b26\u7684\u5b57\u7b26\u3002\n*           \u661f\u53f7\uff0c\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5148\u524d\u7684\u5b57\u7b26\u6216\u6b63\u5219\u3002\n[]           \u5339\u914d\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\u3002\u5982\u679c\u5b57\u7b26\u96c6\u4ee5 ^ \u5f00\u5934\uff0c\u5339\u914d\u4e0d\u518d\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\u3002\n\\(....\\)        \u6807\u8bb0\u5339\u914d\u5b57\u7b26\uff0c\u5982\"\\(love\\)\"\uff0clove\u88ab\u6807\u8bb0\u4e3a 1\u3002\n\\<             \u951a\u5b9a\u5355\u8bcd\u7684\u5f00\u59cb\uff0c\u5982\"\\<grep\" \u5339\u914d\u5305\u542b\u4ee5grep\u5f00\u5934\u7684\u5355\u8bcd\u7684\u884c\u3002\n\\>             \u951a\u5b9a\u5355\u8bcd\u7684\u7ed3\u675f\uff0c\u5982\"grep\\>\" \u5339\u914d\u5305\u542b\u4ee5grep\u7ed3\u5c3e\u7684\u5355\u8bcd\u7684\u884c\u3002 \nx\\{m\\}        \u91cd\u590d\u5b57\u7b26x\uff0cm\u6b21\u3002\u5982'0\\{5\\}' \u5339\u914d\u5305\u542b5\u4e2a0\u7684\u884c\u3002\nx\\{m,\\}        \u91cd\u590d\u5b57\u7b26x\uff0c\u81f3\u5c11m\u6b21\u3002\u5982'0\\{5,\\}' \u5339\u914d\u81f3\u5c11\u5305\u542b5\u4e2a0\u7684\u884c\u3002\nx\\{m,n\\}        \u91cd\u590d\u5b57\u7b26x\uff0cm\u5230n\u6b21\u3002\u5982'0\\{5,6\\}' \u5339\u914d\u5305\u542b5-6\u4e2a0\u7684\u884c\u3002\n\\w               \u5339\u914d\u6587\u5b57\u6216\u6570\u5b57\u5b57\u7b26\uff0c\u4e5f\u5c31\u662f[A-Za-z0-9]\u3002\n\\W               \\w\u7684\u53cd\u7f6e\uff0c[^A-Za-z0-9]\uff0c\u5339\u914d\u4e00\u4e2a\u975e\u5355\u8bcd\u5b57\u7b26\uff0c\u5982\u53e5\u53f7\u7b49\u3002\n\\b               \u5355\u8bcd\u9501\u5b9a\u7b26\uff0c\u5982 '\\bgrep\\b' \u53ea\u5339\u914d grep\u3002\n<\/code><\/pre>\n<h2>\u7528\u4e8eegrep\u548cgrep -E \u7684\u5143\u5b57\u7b26\u6269\u5c55\u96c6<\/h2>\n<pre><code>\n+          \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5148\u524d\u7684\u5b57\u7b26\u6216\u6b63\u5219\u3002\n?          \u5339\u914d\u96f6\u4e2a\u6216\u4e00\u4e2a\u5148\u524d\u7684\u5b57\u7b26\u3002\na|b|c     \u5339\u914da\u6216b\u6216c\u3002\u5982 grep|sed \u5339\u914dgrep\u6216sed\u3002\n()          \u5206\u7ec4\u7b26\u53f7\uff0c\u5982  love(able|rs)ov+  \u5339\u914dloveable\u6216lovers\uff0c\u540e\u9762\u518d\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2aov\u3002\nx{m},  x{m,}, x{m,n}          \u4f5c\u7528\u540c x\\{m\\},  x\\{m,\\}, x\\{m,n\\}  \u3002\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>grep\u662f\u4e00\u4e2a\u6700\u521d\u7528\u4e8eUnix\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5728\u7ed9\u51fa\u6587\u4ef6\u5217\u8868\u6216\u6807\u51c6\u8f93\u5165\u540e\uff0c &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/linux\/20130506\/97\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[34,29],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/97"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}