{"id":241576,"date":"2022-05-08T08:38:10","date_gmt":"2022-05-08T00:38:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=241576"},"modified":"2022-04-29T10:00:24","modified_gmt":"2022-04-29T02:00:24","slug":"string-pattern-grep","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/string-pattern-grep.html","title":{"rendered":"\u591a\u4e2a\u5b57\u7b26\u4e32\u548c\u6a21\u5f0f\u4f7f\u7528grep"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\ngrep\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u8ba9\u60a8\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u8f93\u5165\u6587\u4ef6\u4e2d\u641c\u7d22\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u884c\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5339\u914d\u7684\u884c\u5199\u5165\u6807\u51c6\u8f93\u51fa\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528GNU grep\u641c\u7d22\u591a\u4e2a\u5b57\u7b26\u4e32\u6216\u6a21\u5f0f\u3002<\/p>\n

Grep\u591a\u79cd\u6a21\u5f0f<\/strong><\/div>\n

GNU grep\u652f\u6301\u4e09\u79cd\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\uff0cBasic\uff0cExtended\u548cPerl\u517c\u5bb9\u3002 \u5982\u679c\u672a\u6307\u5b9a\u4efb\u4f55\u6b63\u5219\u8868\u8fbe\u5f0f\u7c7b\u578b\uff0c\u5219grep\u4f1a\u5c06\u641c\u7d22\u6a21\u5f0f\u89e3\u91ca\u4e3a\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n

\u8981\u641c\u7d22\u591a\u4e2a\u6a21\u5f0f\uff0c\u8bf7\u4f7f\u7528OR\uff08\u4ea4\u66ff\uff09\u8fd0\u7b97\u7b26\u3002<\/p>\n

\u4ea4\u66ff\u8fd0\u7b97\u7b26 |\uff08\u7ad6\u7ebf\uff09\u5141\u8bb8\u60a8\u6307\u5b9a\u4e0d\u540c\u7684\u53ef\u80fd\u5339\u914d\u9879\uff0c\u8fd9\u4e9b\u5339\u914d\u9879\u53ef\u4ee5\u662f\u6587\u5b57\u5b57\u7b26\u4e32\u6216\u8868\u8fbe\u5f0f\u96c6\u3002 \u5728\u6240\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd0\u7b97\u7b26\u4e2d\uff0c\u6b64\u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u6700\u4f4e\u3002<\/p>\n

\u4f7f\u7528grep\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\u641c\u7d22\u591a\u4e2a\u6a21\u5f0f\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

grep 'pattern1\\|pattern2' file...<\/p>\n

\u5b9e\u4f8b\uff1a<\/p>\n

[linuxidc@linux:~\/www.linuxidc.com]$ grep 'MainWindow\\|QComboBox' linuxidc.py<\/pre>\n

\"\"<\/p>\n

\u4f7f\u7528\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u5143\u5b57\u7b26\u5c06\u89e3\u91ca\u4e3a\u6587\u5b57\u5b57\u7b26\u3002 \u4e3a\u4e86\u4fdd\u7559\u5143\u5b57\u7b26\u7684\u7279\u6b8a\u542b\u4e49\uff0c\u5fc5\u987b\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u5bf9\u5176\u8fdb\u884c\u8f6c\u4e49\u3002 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u7528\u659c\u6760\u8f6c\u4e49OR\u8fd0\u7b97\u7b26\uff08|\uff09\u7684\u539f\u56e0\u3002<\/p>\n

\u8981\u5c06\u6a21\u5f0f\u89e3\u91ca\u4e3a\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8bf7\u8c03\u7528grep -E\uff08\u6216--extended-regexp\uff09\u9009\u9879\u3002 \u4f7f\u7528\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u4e0d\u8981\u8f6c\u4e49|\u8fd0\u7b97\u7b26:<\/p>\n

$grep -E 'pattern1|pattern2' file...<\/p>\n

[linuxidc@linux:~\/www.linuxidc.com]$ grep -E 'MainWindow|QComboBox' linuxidc.py<\/p>\n

Grep\u591a\u4e2a\u5b57\u7b26\u4e32<\/strong><\/div>\n

\u6587\u5b57\u5b57\u7b26\u4e32\u662f\u6700\u57fa\u672c\u7684\u6a21\u5f0f\u3002<\/p>\n

\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6b63\u5728Nginx\u65e5\u5fd7\u9519\u8bef\u6587\u4ef6\u4e2d\u641c\u7d22\u6240\u6709\u51fa\u73b0\u81f4\u547d\uff0c\u9519\u8bef\u548c\u4e25\u91cd\u7684\u5355\u8bcd\uff1a<\/p>\n

$grep 'fatal\\|error\\|critical' \/var\/log\/nginx\/error.log<\/p>\n

\u5982\u679c\u8981\u641c\u7d22\u7684\u5b57\u7b26\u4e32\u5305\u542b\u7a7a\u683c\uff0c\u8bf7\u7528\u53cc\u5f15\u53f7\u5c06\u5176\u5f15\u8d77\u6765\u3002<\/p>\n

\u8fd9\u662f\u4f7f\u7528\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u76f8\u540c\u793a\u4f8b\uff0c\u5b83\u6d88\u9664\u4e86\u5bf9\u8fd0\u7b97\u7b26|\u8fdb\u884c\u8f6c\u4e49\u7684\u9700\u8981\u3002<\/p>\n

$grep -E 'fatal|error|critical' \/var\/log\/nginx\/error.log<\/p>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cgrep\u533a\u5206\u5927\u5c0f\u5199\u3002 \u8fd9\u610f\u5473\u7740\u5c06\u5927\u5199\u548c\u5c0f\u5199\u5b57\u7b26\u89c6\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u3002<\/p>\n

\u8981\u5728\u641c\u7d22\u65f6\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u8bf7\u4f7f\u7528-i\u9009\u9879\u8c03\u7528grep\uff08\u6216 --ignore-case\uff09\uff1a<\/p>\n

$grep -i 'fatal\\|error\\|critical' \/var\/log\/nginx\/error.log<\/p>\n

\u641c\u7d22\u5b57\u7b26\u4e32\u65f6\uff0cgrep\u5c06\u663e\u793a\u8be5\u5b57\u7b26\u4e32\u5d4c\u5165\u8f83\u5927\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u884c\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u60a8\u8981\u641c\u7d22\u201c\u9519\u8bef\u201d\uff0c\u5219grep\u8fd8\u5c06\u5728\u8f83\u5927\u7684\u5355\u8bcd\uff08\u4f8b\u5982\u201c errorless\u201d\u6216\u201c antiterrorists\u201d\uff09\u4e2d\u5d4c\u5165\u201c error\u201d\u7684\u884c\u3002<\/p>\n

\u8981\u4ec5\u8fd4\u56de\u6307\u5b9a\u5b57\u7b26\u4e32\u662f\u6574\u4e2a\u5355\u8bcd\uff08\u7528\u975e\u5355\u8bcd\u5b57\u7b26\u62ec\u8d77\u6765\uff09\u7684\u90a3\u4e9b\u884c\uff0c\u8bf7\u4f7f\u7528-w\uff08\u6216--word-regexp\uff09\u9009\u9879\uff1a<\/p>\n

$grep -w 'fatal\\|error\\|critical' \/var\/log\/nginx\/error.log<\/p>\n

\u6587\u5b57\u5b57\u7b26\u5305\u62ec\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\uff08a-z\uff0cA-Z\u548c0-9\uff09\u548c\u4e0b\u5212\u7ebf\uff08_\uff09\u3002 \u6240\u6709\u5176\u4ed6\u5b57\u7b26\u5747\u89c6\u4e3a\u975e\u5355\u8bcd\u5b57\u7b26\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u6211\u4eec\u5df2\u7ecf\u5411\u60a8\u5c55\u793a\u4e86\u5982\u4f55grep\u641c\u7d22\u591a\u4e2a\u6a21\u5f0f\uff0c\u5b57\u7b26\u4e32\u548c\u5355\u8bcd\u3002<\/p>\n

\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u6216\u53cd\u9988\uff0c\u8bf7\u968f\u65f6\u53d1\u8868\u8bc4\u8bba\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4f7f\u7528GNU grep\u641c\u7d22\u591a\u4e2a\u5b57\u7b26\u4e32\u6216\u6a21\u5f0f\u3002 GNU grep\u652f\u6301\u4e09\u79cd\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5 […]<\/p>\n","protected":false},"author":317,"featured_media":235376,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-241576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/241576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=241576"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/241576\/revisions"}],"predecessor-version":[{"id":241578,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/241576\/revisions\/241578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235376"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=241576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=241576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=241576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}