{"id":455,"date":"2013-09-19T23:18:16","date_gmt":"2013-09-19T23:18:16","guid":{"rendered":"https:\/\/shan.info\/new\/grep%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0\/"},"modified":"2013-09-19T23:18:16","modified_gmt":"2013-09-19T23:18:16","slug":"grep%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/shan.info\/new\/grep%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0\/","title":{"rendered":"Grep\u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<div class=\"sect1\" style=\"margin-left: 10px; margin-right: 10px; font-family: Simsun; font-size: medium; line-height: normal;\" lang=\"en\">\n<div class=\"titlepage\">\n<div>\n<h2 class=\"title\" style=\"clear: both;\">1.&nbsp;grep\u7b80\u4ecb<\/h2>\n<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<p>grep \uff08global search regular expression(RE) and print out the line,\u5168\u9762\u641c\u7d22\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u628a\u884c\u6253\u5370\u51fa\u6765\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u5de5\u5177\uff0c\u5b83\u80fd\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u641c\u7d22\u6587\u672c\uff0c\u5e76\u628a\u5339\u914d\u7684\u884c\u6253\u5370\u51fa\u6765\u3002Unix\u7684grep\u5bb6\u65cf\u5305 \u62ecgrep\u3001egrep\u548cfgrep\u3002egrep\u548cfgrep\u7684\u547d\u4ee4\u53ea\u8ddfgrep\u6709\u5f88\u5c0f\u4e0d\u540c\u3002egrep\u662fgrep\u7684\u6269\u5c55\uff0c\u652f\u6301\u66f4\u591a\u7684re\u5143\u5b57\u7b26\uff0c fgrep\u5c31\u662ffixed grep\u6216fast grep\uff0c\u5b83\u4eec\u628a\u6240\u6709\u7684\u5b57\u6bcd\u90fd\u770b\u4f5c\u5355\u8bcd\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u5143\u5b57\u7b26\u8868\u793a\u56de\u5176\u81ea\u8eab\u7684\u5b57\u9762\u610f\u4e49\uff0c\u4e0d\u518d\u7279\u6b8a\u3002linux\u4f7f\u7528GNU\u7248\u672c\u7684grep\u3002\u5b83\u529f\u80fd \u66f4\u5f3a\uff0c\u53ef\u4ee5\u901a\u8fc7-G\u3001-E\u3001-F\u547d\u4ee4\u884c\u9009\u9879\u6765\u4f7f\u7528egrep\u548cfgrep\u7684\u529f\u80fd\u3002<\/p>\n<p>grep\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u8fd9\u6837\u7684\uff0c\u5b83\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u4e2d\u641c\u7d22\u5b57\u7b26\u4e32\u6a21\u677f\u3002\u5982\u679c\u6a21\u677f\u5305\u62ec\u7a7a\u683c\uff0c\u5219\u5fc5\u987b\u88ab\u5f15\u7528\uff0c\u6a21\u677f\u540e\u7684\u6240\u6709\u5b57\u7b26\u4e32\u88ab\u770b\u4f5c\u6587\u4ef6\u540d\u3002\u641c\u7d22\u7684\u7ed3\u679c\u88ab\u9001\u5230\u5c4f\u5e55\uff0c\u4e0d\u5f71\u54cd\u539f\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p>grep\u53ef\u7528\u4e8eshell\u811a\u672c\uff0c\u56e0\u4e3agrep\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u72b6\u6001\u503c\u6765\u8bf4\u660e\u641c\u7d22\u7684\u72b6\u6001\uff0c\u5982\u679c\u6a21\u677f\u641c\u7d22\u6210\u529f\uff0c\u5219\u8fd4\u56de0\uff0c\u5982\u679c\u641c\u7d22\u4e0d\u6210\u529f\uff0c\u5219\u8fd4\u56de1\uff0c\u5982\u679c\u641c\u7d22\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de2\u3002\u6211\u4eec\u5229\u7528\u8fd9\u4e9b\u8fd4\u56de\u503c\u5c31\u53ef\u8fdb\u884c\u4e00\u4e9b\u81ea\u52a8\u5316\u7684\u6587\u672c\u5904\u7406\u5de5\u4f5c\u3002<\/p>\n<\/div>\n<div class=\"sect1\" style=\"margin-left: 10px; margin-right: 10px; font-family: Simsun; font-size: medium; line-height: normal;\" lang=\"en\">\n<div class=\"titlepage\">\n<div>\n<h2 class=\"title\" style=\"clear: both;\">2.&nbsp;grep\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\u96c6\uff08\u57fa\u672c\u96c6\uff09<\/h2>\n<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<div class=\"variablelist\">\n<dl>\n<dt><span class=\"term\">^<\/span><\/dt>\n<dd>\n<p>\u951a\u5b9a\u884c\u7684\u5f00\u59cb \u5982\uff1a&#8217;^grep&#8217;\u5339\u914d\u6240\u6709\u4ee5grep\u5f00\u5934\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">$<\/span><\/dt>\n<dd>\n<p>\u951a\u5b9a\u884c\u7684\u7ed3\u675f \u5982\uff1a&#8217;grep$&#8217;\u5339\u914d\u6240\u6709\u4ee5grep\u7ed3\u5c3e\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">.<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u4e00\u4e2a\u975e\u6362\u884c\u7b26\u7684\u5b57\u7b26 \u5982\uff1a&#8217;gr.p&#8217;\u5339\u914dgr\u540e\u63a5\u4e00\u4e2a\u4efb\u610f\u5b57\u7b26\uff0c\u7136\u540e\u662fp\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">*<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5148\u524d\u5b57\u7b26 \u5982\uff1a&#8217;*grep&#8217;\u5339\u914d\u6240\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u540e\u7d27\u8ddfgrep\u7684\u884c\u3002 .*\u4e00\u8d77\u7528\u4ee3\u8868\u4efb\u610f\u5b57\u7b26\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">[]<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5982'[Gg]rep&#8217;\u5339\u914dGrep\u548cgrep\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">[^]<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u4e00\u4e2a\u4e0d\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5982\uff1a'[^A-FH-Z]rep&#8217;\u5339\u914d\u4e0d\u5305\u542bA-R\u548cT-Z\u7684\u4e00\u4e2a\u5b57\u6bcd\u5f00\u5934\uff0c\u7d27\u8ddfrep\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">(..)<\/span><\/dt>\n<dd>\n<p>\u6807\u8bb0\u5339\u914d\u5b57\u7b26\uff0c\u5982'(love)&#8217;\uff0clove\u88ab\u6807\u8bb0\u4e3a1\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">&lt;<\/span><\/dt>\n<dd>\n<p>\u951a\u5b9a\u5355\u8bcd\u7684\u5f00\u59cb\uff0c\u5982:'&lt;grep&#8217;\u5339\u914d\u5305\u542b\u4ee5grep\u5f00\u5934\u7684\u5355\u8bcd\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">&gt;<\/span><\/dt>\n<dd>\n<p>\u951a\u5b9a\u5355\u8bcd\u7684\u7ed3\u675f\uff0c\u5982&#8217;grep&gt;&#8217;\u5339\u914d\u5305\u542b\u4ee5grep\u7ed3\u5c3e\u7684\u5355\u8bcd\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">x{m}<\/span><\/dt>\n<dd>\n<p>\u91cd\u590d\u5b57\u7b26x\uff0cm\u6b21\uff0c\u5982\uff1a&#8217;0{5}&#8217;\u5339\u914d\u5305\u542b5\u4e2ao\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">x{m,}<\/span><\/dt>\n<dd>\n<p>\u91cd\u590d\u5b57\u7b26x,\u81f3\u5c11m\u6b21\uff0c\u5982\uff1a&#8217;o{5,}&#8217;\u5339\u914d\u81f3\u5c11\u67095\u4e2ao\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">x{m,n}<\/span><\/dt>\n<dd>\n<p>\u91cd\u590d\u5b57\u7b26x\uff0c\u81f3\u5c11m\u6b21\uff0c\u4e0d\u591a\u4e8en\u6b21\uff0c\u5982\uff1a&#8217;o{5,10}&#8217;\u5339\u914d5&#8211;10\u4e2ao\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">w<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u6587\u5b57\u548c\u6570\u5b57\u5b57\u7b26\uff0c\u4e5f\u5c31\u662f[A-Za-z0-9]\uff0c\u5982\uff1a&#8217;Gw*p&#8217;\u5339\u914d\u4ee5G\u540e\u8ddf\u96f6\u4e2a\u6216\u591a\u4e2a\u6587\u5b57\u6216\u6570\u5b57\u5b57\u7b26\uff0c\u7136\u540e\u662fp\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">W<\/span><\/dt>\n<dd>\n<p>w\u7684\u53cd\u7f6e\u5f62\u5f0f\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u975e\u5355\u8bcd\u5b57\u7b26\uff0c\u5982\u70b9\u53f7\u53e5\u53f7\u7b49\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">b<\/span><\/dt>\n<dd>\n<p>\u5355\u8bcd\u9501\u5b9a\u7b26\uff0c\u5982: &#8216;bgrepb&#8217;\u53ea\u5339\u914dgrep\u3002<\/p>\n<\/dd>\n<\/dl>\n<\/div>\n<\/div>\n<div class=\"sect1\" style=\"margin-left: 10px; margin-right: 10px; font-family: Simsun; font-size: medium; line-height: normal;\" lang=\"en\">\n<div class=\"titlepage\">\n<div>\n<h2 class=\"title\" style=\"clear: both;\">3.&nbsp;\u7528\u4e8eegrep\u548c grep -E\u7684\u5143\u5b57\u7b26\u6269\u5c55\u96c6<\/h2>\n<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<div class=\"variablelist\">\n<dl>\n<dt><span class=\"term\">+<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5148\u524d\u7684\u5b57\u7b26\u3002\u5982\uff1a'[a-z]+able&#8217;\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u5c0f\u5199\u5b57\u6bcd\u540e\u8ddfable\u7684\u4e32\uff0c\u5982loveable,enable,disable\u7b49\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">?<\/span><\/dt>\n<dd>\n<p>\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5148\u524d\u7684\u5b57\u7b26\u3002\u5982\uff1a&#8217;gr?p&#8217;\u5339\u914dgr\u540e\u8ddf\u4e00\u4e2a\u6216\u6ca1\u6709\u5b57\u7b26\uff0c\u7136\u540e\u662fp\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">a|b|c<\/span><\/dt>\n<dd>\n<p>\u5339\u914da\u6216b\u6216c\u3002\u5982\uff1agrep|sed\u5339\u914dgrep\u6216sed<\/p>\n<\/dd>\n<dt><span class=\"term\">()<\/span><\/dt>\n<dd>\n<p>\u5206\u7ec4\u7b26\u53f7\uff0c\u5982\uff1alove(able|rs)ov+\u5339\u914dloveable\u6216lovers\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2aov\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">x{m},x{m,},x{m,n}<\/span><\/dt>\n<dd>\n<p>\u4f5c\u7528\u540cx{m},x{m,},x{m,n}<\/p>\n<\/dd>\n<\/dl>\n<\/div>\n<\/div>\n<div class=\"sect1\" style=\"margin-left: 10px; margin-right: 10px; font-family: Simsun; font-size: medium; line-height: normal;\" lang=\"en\">\n<div class=\"titlepage\">\n<div>\n<h2 class=\"title\" style=\"clear: both;\">4.&nbsp;POSIX\u5b57\u7b26\u7c7b<\/h2>\n<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<p>\u4e3a \u4e86\u5728\u4e0d\u540c\u56fd\u5bb6\u7684\u5b57\u7b26\u7f16\u7801\u4e2d\u4fdd\u6301\u4e00\u81f3\uff0cPOSIX(The Portable Operating System Interface)\u589e\u52a0\u4e86\u7279\u6b8a\u7684\u5b57\u7b26\u7c7b\uff0c\u5982[:alnum:]\u662fA-Za-z0-9\u7684\u53e6\u4e00\u4e2a\u5199\u6cd5\u3002\u8981\u628a\u5b83\u4eec\u653e\u5230[]\u53f7\u5185\u624d\u80fd\u6210\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5982[A- Za-z0-9]\u6216[[:alnum:]]\u3002\u5728linux\u4e0b\u7684grep\u9664fgrep\u5916\uff0c\u90fd\u652f\u6301POSIX\u7684\u5b57\u7b26\u7c7b\u3002<\/p>\n<div class=\"variablelist\">\n<dl>\n<dt><span class=\"term\">[:alnum:]<\/span><\/dt>\n<dd>\n<p>\u6587\u5b57\u6570\u5b57\u5b57\u7b26<\/p>\n<\/dd>\n<dt><span class=\"term\">[:alpha:]<\/span><\/dt>\n<dd>\n<p>\u6587\u5b57\u5b57\u7b26<\/p>\n<\/dd>\n<dt><span class=\"term\">[:digit:]<\/span><\/dt>\n<dd>\n<p>\u6570\u5b57\u5b57\u7b26<\/p>\n<\/dd>\n<dt><span class=\"term\">[:graph:]<\/span><\/dt>\n<dd>\n<p>\u975e\u7a7a\u5b57\u7b26\uff08\u975e\u7a7a\u683c\u3001\u63a7\u5236\u5b57\u7b26\uff09<\/p>\n<\/dd>\n<dt><span class=\"term\">[:lower:]<\/span><\/dt>\n<dd>\n<p>\u5c0f\u5199\u5b57\u7b26<\/p>\n<\/dd>\n<dt><span class=\"term\">[:cntrl:]<\/span><\/dt>\n<dd>\n<p>\u63a7\u5236\u5b57\u7b26<\/p>\n<\/dd>\n<dt><span class=\"term\">[:print:]<\/span><\/dt>\n<dd>\n<p>\u975e\u7a7a\u5b57\u7b26\uff08\u5305\u62ec\u7a7a\u683c\uff09<\/p>\n<\/dd>\n<dt><span class=\"term\">[:punct:]<\/span><\/dt>\n<dd>\n<p>\u6807\u70b9\u7b26\u53f7<\/p>\n<\/dd>\n<dt><span class=\"term\">[:space:]<\/span><\/dt>\n<dd>\n<p>\u6240\u6709\u7a7a\u767d\u5b57\u7b26\uff08\u65b0\u884c\uff0c\u7a7a\u683c\uff0c\u5236\u8868\u7b26\uff09<\/p>\n<\/dd>\n<dt><span class=\"term\">[:upper:]<\/span><\/dt>\n<dd>\n<p>\u5927\u5199\u5b57\u7b26<\/p>\n<\/dd>\n<dt><span class=\"term\">[:xdigit:]<\/span><\/dt>\n<dd>\n<p>\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff080-9\uff0ca-f\uff0cA-F\uff09<\/p>\n<\/dd>\n<\/dl>\n<\/div>\n<\/div>\n<div class=\"sect1\" style=\"margin-left: 10px; margin-right: 10px; font-family: Simsun; font-size: medium; line-height: normal;\" lang=\"en\">\n<div class=\"titlepage\">\n<div>\n<h2 class=\"title\" style=\"clear: both;\">5.&nbsp;Grep\u547d\u4ee4\u9009\u9879<\/h2>\n<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<div class=\"variablelist\">\n<dl>\n<dt><span class=\"term\">-?<\/span><\/dt>\n<dd>\n<p>\u540c\u65f6\u663e\u793a\u5339\u914d\u884c\u4e0a\u4e0b\u7684\uff1f\u884c\uff0c\u5982\uff1agrep -2 pattern filename\u540c\u65f6\u663e\u793a\u5339\u914d\u884c\u7684\u4e0a\u4e0b2\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-b\uff0c&#8211;byte-offset<\/span><\/dt>\n<dd>\n<p>\u6253\u5370\u5339\u914d\u884c\u524d\u9762\u6253\u5370\u8be5\u884c\u6240\u5728\u7684\u5757\u53f7\u7801\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-c,&#8211;count<\/span><\/dt>\n<dd>\n<p>\u53ea\u6253\u5370\u5339\u914d\u7684\u884c\u6570\uff0c\u4e0d\u663e\u793a\u5339\u914d\u7684\u5185\u5bb9\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-f File\uff0c&#8211;file=File<\/span><\/dt>\n<dd>\n<p>\u4ece\u6587\u4ef6\u4e2d\u63d0\u53d6\u6a21\u677f\u3002\u7a7a\u6587\u4ef6\u4e2d\u5305\u542b0\u4e2a\u6a21\u677f\uff0c\u6240\u4ee5\u4ec0\u4e48\u90fd\u4e0d\u5339\u914d\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-h\uff0c&#8211;no-filename<\/span><\/dt>\n<dd>\n<p>\u5f53\u641c\u7d22\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u4e0d\u663e\u793a\u5339\u914d\u6587\u4ef6\u540d\u524d\u7f00\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-i\uff0c&#8211;ignore-case<\/span><\/dt>\n<dd>\n<p>\u5ffd\u7565\u5927\u5c0f\u5199\u5dee\u522b\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-q\uff0c&#8211;quiet<\/span><\/dt>\n<dd>\n<p>\u53d6\u6d88\u663e\u793a\uff0c\u53ea\u8fd4\u56de\u9000\u51fa\u72b6\u6001\u30020\u5219\u8868\u793a\u627e\u5230\u4e86\u5339\u914d\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-l\uff0c&#8211;files-with-matches<\/span><\/dt>\n<dd>\n<p>\u6253\u5370\u5339\u914d\u6a21\u677f\u7684\u6587\u4ef6\u6e05\u5355\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-L\uff0c&#8211;files-without-match<\/span><\/dt>\n<dd>\n<p>\u6253\u5370\u4e0d\u5339\u914d\u6a21\u677f\u7684\u6587\u4ef6\u6e05\u5355\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-n\uff0c&#8211;line-number<\/span><\/dt>\n<dd>\n<p>\u5728\u5339\u914d\u7684\u884c\u524d\u9762\u6253\u5370\u884c\u53f7\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-s\uff0c&#8211;silent<\/span><\/dt>\n<dd>\n<p>\u4e0d\u663e\u793a\u5173\u4e8e\u4e0d\u5b58\u5728\u6216\u8005\u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-v\uff0c&#8211;revert-match<\/span><\/dt>\n<dd>\n<p>\u53cd\u68c0\u7d22\uff0c\u53ea\u663e\u793a\u4e0d\u5339\u914d\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-w\uff0c&#8211;word-regexp<\/span><\/dt>\n<dd>\n<p>\u5982\u679c\u88ab&lt;\u548c&gt;\u5f15\u7528\uff0c\u5c31\u628a\u8868\u8fbe\u5f0f\u505a\u4e3a\u4e00\u4e2a\u5355\u8bcd\u641c\u7d22\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">-V\uff0c&#8211;version<\/span><\/dt>\n<dd>\n<p>\u663e\u793a\u8f6f\u4ef6\u7248\u672c\u4fe1\u606f\u3002<\/p>\n<\/dd>\n<\/dl>\n<\/div>\n<\/div>\n<div class=\"sect1\" style=\"margin-left: 10px; margin-right: 10px; font-family: Simsun; font-size: medium; line-height: normal;\" lang=\"en\">\n<div class=\"titlepage\">\n<div>\n<h2 class=\"title\" style=\"clear: both;\">6.&nbsp;\u5b9e\u4f8b<\/h2>\n<\/div>\n<div>&nbsp;<\/div>\n<\/div>\n<p>\u8981\u7528\u597dgrep\u8fd9\u4e2a\u5de5\u5177\uff0c\u5176\u5b9e\u5c31\u662f\u8981\u5199\u597d\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e0d\u5bf9grep\u7684\u6240\u6709\u529f\u80fd\u8fdb\u884c\u5b9e\u4f8b\u8bb2\u89e3\uff0c\u53ea\u5217\u51e0\u4e2a\u4f8b\u5b50\uff0c\u8bb2\u89e3\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5199\u6cd5\u3002<\/p>\n<div class=\"variablelist\">\n<dl>\n<dt><span class=\"term\">$ ls -l | grep &#8216;^a&#8217;<\/span><\/dt>\n<dd>\n<p>\u901a\u8fc7\u7ba1\u9053\u8fc7\u6ee4ls -l\u8f93\u51fa\u7684\u5185\u5bb9\uff0c\u53ea\u663e\u793a\u4ee5a\u5f00\u5934\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">$ grep &#8216;test&#8217; d*<\/span><\/dt>\n<dd>\n<p>\u663e\u793a\u6240\u6709\u4ee5d\u5f00\u5934\u7684\u6587\u4ef6\u4e2d\u5305\u542btest\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">$ grep &#8216;test&#8217; aa bb cc<\/span><\/dt>\n<dd>\n<p>\u663e\u793a\u5728aa\uff0cbb\uff0ccc\u6587\u4ef6\u4e2d\u5339\u914dtest\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">$ grep &#8216;[a-z]{5}&#8217; aa<\/span><\/dt>\n<dd>\n<p>\u663e\u793a\u6240\u6709\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u4e32\u81f3\u5c11\u67095\u4e2a\u8fde\u7eed\u5c0f\u5199\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u7684\u884c\u3002<\/p>\n<\/dd>\n<dt><span class=\"term\">$ grep &#8216;w(es)t.*1&#8217; aa<\/span><\/dt>\n<dd>\n<p>\u5982\u679cwest\u88ab\u5339\u914d\uff0c\u5219es\u5c31\u88ab\u5b58\u50a8\u5230\u5185\u5b58\u4e2d\uff0c\u5e76\u6807\u8bb0\u4e3a1\uff0c\u7136\u540e\u641c\u7d22\u4efb\u610f\u4e2a\u5b57\u7b26\uff08.*\uff09\uff0c\u8fd9\u4e9b\u5b57\u7b26\u540e\u9762\u7d27\u8ddf\u7740\u53e6\u5916\u4e00\u4e2aes\uff081\uff09\uff0c\u627e\u5230\u5c31\u663e\u793a\u8be5\u884c\u3002\u5982\u679c\u7528egrep\u6216grep -E\uff0c\u5c31\u4e0d\u7528&#8221;&#8221;\u53f7\u8fdb\u884c\u8f6c\u4e49\uff0c\u76f4\u63a5\u5199\u6210&#8217;w(es)t.*1&#8217;\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<\/dd>\n<\/dl>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1.&nbsp;grep\u7b80\u4ecb &nbsp; grep \uff08global search regular expression(RE) and print out the line,\u5168\u9762\u641c\u7d22\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u628a\u884c\u6253\u5370\u51fa\u6765\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u5de5\u5177\uff0c\u5b83\u80fd\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u641c\u7d22\u6587\u672c\uff0c\u5e76\u628a\u5339\u914d\u7684\u884c\u6253\u5370\u51fa\u6765\u3002Unix\u7684grep\u5bb6\u65cf\u5305 \u62ecgrep\u3001egrep\u548cfgrep\u3002egrep\u548cfgrep\u7684\u547d\u4ee4\u53ea\u8ddfgrep\u6709\u5f88\u5c0f\u4e0d\u540c\u3002egrep\u662fgrep\u7684\u6269\u5c55\uff0c\u652f\u6301\u66f4\u591a\u7684re\u5143\u5b57\u7b26\uff0c fgrep\u5c31\u662ffixed grep\u6216fast grep\uff0c\u5b83\u4eec\u628a\u6240\u6709\u7684\u5b57\u6bcd\u90fd\u770b\u4f5c\u5355\u8bcd\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u5143\u5b57\u7b26\u8868\u793a\u56de\u5176\u81ea\u8eab\u7684\u5b57\u9762\u610f\u4e49\uff0c\u4e0d\u518d\u7279\u6b8a\u3002linux\u4f7f\u7528GNU\u7248\u672c\u7684grep\u3002\u5b83\u529f\u80fd \u66f4\u5f3a\uff0c\u53ef\u4ee5\u901a\u8fc7-G\u3001-E\u3001-F\u547d\u4ee4\u884c\u9009\u9879\u6765\u4f7f\u7528egrep\u548cfgrep\u7684\u529f\u80fd\u3002 grep\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u8fd9\u6837\u7684\uff0c\u5b83\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u4e2d\u641c\u7d22\u5b57\u7b26\u4e32\u6a21\u677f\u3002\u5982\u679c\u6a21\u677f\u5305\u62ec\u7a7a\u683c\uff0c\u5219\u5fc5\u987b\u88ab\u5f15\u7528\uff0c\u6a21\u677f\u540e\u7684\u6240\u6709\u5b57\u7b26\u4e32\u88ab\u770b\u4f5c\u6587\u4ef6\u540d\u3002\u641c\u7d22\u7684\u7ed3\u679c\u88ab\u9001\u5230\u5c4f\u5e55\uff0c\u4e0d\u5f71\u54cd\u539f\u6587\u4ef6\u5185\u5bb9\u3002 grep\u53ef\u7528\u4e8eshell\u811a\u672c\uff0c\u56e0\u4e3agrep\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u72b6\u6001\u503c\u6765\u8bf4\u660e\u641c\u7d22\u7684\u72b6\u6001\uff0c\u5982\u679c\u6a21\u677f\u641c\u7d22\u6210\u529f\uff0c\u5219\u8fd4\u56de0\uff0c\u5982\u679c\u641c\u7d22\u4e0d\u6210\u529f\uff0c\u5219\u8fd4\u56de1\uff0c\u5982\u679c\u641c\u7d22\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de2\u3002\u6211\u4eec\u5229\u7528\u8fd9\u4e9b\u8fd4\u56de\u503c\u5c31\u53ef\u8fdb\u884c\u4e00\u4e9b\u81ea\u52a8\u5316\u7684\u6587\u672c\u5904\u7406\u5de5\u4f5c\u3002 2.&nbsp;grep\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\u96c6\uff08\u57fa\u672c\u96c6\uff09 &nbsp; ^ \u951a\u5b9a\u884c\u7684\u5f00\u59cb \u5982\uff1a&#8217;^grep&#8217;\u5339\u914d\u6240\u6709\u4ee5grep\u5f00\u5934\u7684\u884c\u3002 $ \u951a\u5b9a\u884c\u7684\u7ed3\u675f \u5982\uff1a&#8217;grep$&#8217;\u5339\u914d\u6240\u6709\u4ee5grep\u7ed3\u5c3e\u7684\u884c\u3002 . \u5339\u914d\u4e00\u4e2a\u975e\u6362\u884c\u7b26\u7684\u5b57\u7b26 \u5982\uff1a&#8217;gr.p&#8217;\u5339\u914dgr\u540e\u63a5\u4e00\u4e2a\u4efb\u610f\u5b57\u7b26\uff0c\u7136\u540e\u662fp\u3002 * \u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5148\u524d\u5b57\u7b26 \u5982\uff1a&#8217;*grep&#8217;\u5339\u914d\u6240\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u540e\u7d27\u8ddfgrep\u7684\u884c\u3002 .*\u4e00\u8d77\u7528\u4ee3\u8868\u4efb\u610f\u5b57\u7b26\u3002 [] \u5339\u914d\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5982'[Gg]rep&#8217;\u5339\u914dGrep\u548cgrep\u3002 [^] \u5339\u914d\u4e00\u4e2a\u4e0d\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5982\uff1a'[^A-FH-Z]rep&#8217;\u5339\u914d\u4e0d\u5305\u542bA-R\u548cT-Z\u7684\u4e00\u4e2a\u5b57\u6bcd\u5f00\u5934\uff0c\u7d27\u8ddfrep\u7684\u884c\u3002 (..) \u6807\u8bb0\u5339\u914d\u5b57\u7b26\uff0c\u5982'(love)&#8217;\uff0clove\u88ab\u6807\u8bb0\u4e3a1\u3002 &lt; \u951a\u5b9a\u5355\u8bcd\u7684\u5f00\u59cb\uff0c\u5982:'&lt;grep&#8217;\u5339\u914d\u5305\u542b\u4ee5grep\u5f00\u5934\u7684\u5355\u8bcd\u7684\u884c\u3002 &gt; \u951a\u5b9a\u5355\u8bcd\u7684\u7ed3\u675f\uff0c\u5982&#8217;grep&gt;&#8217;\u5339\u914d\u5305\u542b\u4ee5grep\u7ed3\u5c3e\u7684\u5355\u8bcd\u7684\u884c\u3002 x{m} \u91cd\u590d\u5b57\u7b26x\uff0cm\u6b21\uff0c\u5982\uff1a&#8217;0{5}&#8217;\u5339\u914d\u5305\u542b5\u4e2ao\u7684\u884c\u3002 x{m,} \u91cd\u590d\u5b57\u7b26x,\u81f3\u5c11m\u6b21\uff0c\u5982\uff1a&#8217;o{5,}&#8217;\u5339\u914d\u81f3\u5c11\u67095\u4e2ao\u7684\u884c\u3002 x{m,n} \u91cd\u590d\u5b57\u7b26x\uff0c\u81f3\u5c11m\u6b21\uff0c\u4e0d\u591a\u4e8en\u6b21\uff0c\u5982\uff1a&#8217;o{5,10}&#8217;\u5339\u914d5&#8211;10\u4e2ao\u7684\u884c\u3002 w \u5339\u914d\u6587\u5b57\u548c\u6570\u5b57\u5b57\u7b26\uff0c\u4e5f\u5c31\u662f[A-Za-z0-9]\uff0c\u5982\uff1a&#8217;Gw*p&#8217;\u5339\u914d\u4ee5G\u540e\u8ddf\u96f6\u4e2a\u6216\u591a\u4e2a\u6587\u5b57\u6216\u6570\u5b57\u5b57\u7b26\uff0c\u7136\u540e\u662fp\u3002 W w\u7684\u53cd\u7f6e\u5f62\u5f0f\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u975e\u5355\u8bcd\u5b57\u7b26\uff0c\u5982\u70b9\u53f7\u53e5\u53f7\u7b49\u3002 b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-455","post","type-post","status-publish","format-standard","hentry","category-centos-6"],"_links":{"self":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":0,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"wp:attachment":[{"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shan.info\/new\/wp-json\/wp\/v2\/tags?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}