{"id":228189,"date":"2021-10-19T08:19:45","date_gmt":"2021-10-19T00:19:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=228189"},"modified":"2021-10-11T09:10:14","modified_gmt":"2021-10-11T01:10:14","slug":"linux-grep-guide","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-grep-guide.html","title":{"rendered":"\u5206\u4eabLinux Grep\u9ad8\u7ea7\u7528\u6237\u6307\u5357"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4efb\u4f55\u7ba1\u7406Linux\u670d\u52a1\u5668\u4e00\u6bb5\u65f6\u95f4\u7684\u4eba\u90fd\u719f\u6089\u201cgrep\u201d\u547d\u4ee4\u3002 \u501f\u52a9Linux\u7684\u201cEverything is a file\u201d\u7406\u5ff5\uff0cgrep\u5bf9\u4e8e\u904d\u5386\u65e5\u5fd7\u6587\u4ef6\uff0c\u914d\u7f6e\u6587\u4ef6\uff0c\u68c0\u67e5\u8f93\u51fa\u6587\u4ef6\u4e2d\u7684\u67d0\u4e9b\u9519\u8bef\u4ee5\u53ca\u83b7\u53d6\u5339\u914d\u6570\u91cf\u81f3\u5173\u91cd\u8981\u3002 \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u5c06\u5411\u60a8\u5c55\u793agrep\u7684\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u5b83\u4eec\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002 \u8fd9\u4e9b\u5728\u624b\u518c\u9875\u4e2d\u4e0d\u96be\u627e\u5230\uff0c\u4f46\u5b83\u4eec\u53ea\u662fgrep\u9ed8\u8ba4\u529f\u80fd\u7684\u6269\u5c55\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u540c\u65f6\u641c\u7d22\u591a\u4e2a\u5355\u8bcd<\/strong><\/div>\n

\u901a\u5e38\uff0cgrep\u7684\u7528\u6cd5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

grep 'pattern' filename<\/pre>\n

\u5176\u4e2d'pattern'\u662f\u4efb\u4f55\u7ed9\u5b9a\u7684\u6a21\u5f0f\u6216\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u4f46\u662f\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u9519\u8bef\u65e5\u5fd7\u6587\u4ef6\u5e76\u6b63\u5728\u6267\u884c\u4f8b\u884c\u7ef4\u62a4\u3002 \u60a8\u5e0c\u671b\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\u4e2d\u662f\u5426\u5b58\u5728\u8de8\u8d8a\u5404\u79cd\u6e90\u7684\u67d0\u4e9b\u5173\u952e\u5b57\u3002 \u901a\u5e38\uff0c\u60a8\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u77ed\u8bed\u4f7f\u7528\u5355\u72ec\u7684grep\u8868\u8fbe\u5f0f\u3002<\/p>\n

\u4f46\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u201c-E\u201d\u53c2\u6570\u8f7b\u677e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u9996\u5148\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a\u201clinuxidc-com\u201d\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

\"\"<\/p>\n

\u5047\u8bbe\u6211\u4eec\u8981\u5728\u6b64\u6587\u4ef6\u4e2d\u641c\u7d22\u4ee5\u4e0b\u4e24\u4e2a\u77ed\u8bed:<\/p>\n

linuxidc
\nlinuxmi<\/p>\n

grep -Ew 'linuxidc|linuxmi' linuxidc-com<\/pre>\n

\"\"<\/p>\n

\u6709\u4e24\u4e2a\u6807\u5fd7\uff1a
\n1.-E\u8868\u793a\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f
\n2.-w\u610f\u5473\u7740\u6211\u4eec\u53ea\u60f3\u5339\u914d\u6574\u4e2a\u5355\u8bcd
\n\u4e3a\u4e86\u4f7f\u5b83\u66f4\u5bb9\u6613\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u201cgrep -E\u201d\u66ff\u6362\u4e3a\u201cegrep\u201d\u3002 \u6240\u4ee5\u4e0a\u9762\u7684\u547d\u4ee4\u53d8\u6210\uff1a<\/p>\n

egrep -w 'linuxidc|linuxmi' linuxidc-com<\/pre>\n

\"\"<\/p>\n

\u8fd9\u91cc\u662f\u8f93\u51fa:
\n\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5339\u914d\u5c3d\u53ef\u80fd\u591a\u7684\u5355\u8bcd\u3002\u5982\u679c\u4f60\u60f3\u5220\u9664\u201c\u5355\u8bcd\u201d\u7684\u8981\u6c42\uff0c\u800c\u53ea\u662f\u60f3\u8981\u4e00\u4e2a\u76f4\u63a5\u7684\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5220\u9664\u201cw\u201d\u53c2\u6570\u3002<\/p>\n

\u641c\u7d22\u6574\u4e2a\u76ee\u5f55\u4ee5\u67e5\u627e\u5339\u914d\u9879\u5e76\u663e\u793a\u6587\u4ef6\u540d<\/strong><\/div>\n

\u6211\u4eec\u901a\u5e38\u8ba4\u4e3agrep\u641c\u7d22\u6587\u4ef6\u6216\u6765\u81ea\u53e6\u4e00\u4e2a\u547d\u4ee4\u7684\u7279\u5b9a\u8f93\u51fa\u3002\u4f46\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b83\u540c\u65f6\u641c\u7d22\u6574\u4e2a\u6587\u4ef6\u76ee\u5f55\u3002\u6bd4\u5982\u8bf4\uff0c\u4f60\u6709\u4e00\u5806\u5206\u6563\u5728\u7cfb\u7edf\u5468\u56f4\u7684\u65e5\u5fd7\u6587\u4ef6\u3002\u6bd5\u7adf\uff0c\u8bb8\u591a\u8f6f\u4ef6\u5305\u90fd\u6709\u81ea\u5df1\u7684\u65e5\u5fd7\u6587\u4ef6\u4f4d\u7f6e\u3002\u4e3a\u4e86\u65b9\u4fbf\u60a8\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b\u6307\u5411\u60a8\u611f\u5174\u8da3\u7684\u6240\u6709\u5404\u79cd\u65e5\u5fd7\u6587\u4ef6\u7684\u7b26\u53f7\u94fe\u63a5\u3002\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u201c-R\u201d\u547d\u4ee4\u4e0egrep\u4e00\u8d77\u641c\u7d22\u6240\u6709\u8fd9\u4e9b\u5185\u5bb9\u3002\u9996\u90fd\u201cR\u201d\u544a\u8bc9grep\u5305\u542b\u7b26\u53f7\u94fe\u63a5\u3002\u5c0f\u201cr\u201d\u5ffd\u7565\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5728\u547d\u4ee4\u884c\u4e2d\u627e\u4e0d\u5230\u7684\u7b26\u53f7\u94fe\u63a5\u3002<\/p>\n

\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u641c\u7d22\u5b57\u7b26\u4e32\uff1a<\/p>\n

grep -r linuxidc .<\/pre>\n

\u672b\u5c3e\u7684\u70b9\uff08 .\uff09\u8868\u793a\u5f53\u524d\u76ee\u5f55\u3002 \u6211\u4eec\u5f97\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

\"\"<\/p>\n

\u60a8\u53ef\u4ee5\u5728\u4e0b\u9762\u770b\u5230\uff0c\u5b83\u8fd8\u4f1a\u4ee5\u9012\u5f52\u65b9\u5f0f\u641c\u7d22\u53e6\u4e00\u4e2a\u6a21\u5f0f\u5339\u914d\uff1a
\n\u8fd9\u5bf9\u4e8e\u540c\u65f6\u641c\u7d22\u4e00\u5806\u6587\u4ef6\u975e\u5e38\u6709\u7528\u3002 \u8f93\u51fa\u8fd8\u4f1a\u663e\u793a\u5305\u542b\u5339\u914d\u5b57\u7b26\u4e32\u7684\u6587\u4ef6\u7684\u540d\u79f0\uff01 \u6211\u4e2a\u4eba\u559c\u6b22\u8fd9\u4e2a\u5e26\u6709\u201c-R\u201d\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u540c\u65f6\u641c\u7d22\u4e00\u7ec4\u7b26\u53f7\u94fe\u63a5\uff0c\u4ee5\u8282\u7701\u65f6\u95f4\u3002<\/p>\n

\u8ba1\u7b97\u5339\u914d\u884c\u6570<\/strong><\/div>\n

\u6709\u65f6\u60a8\u60f3\u77e5\u9053\u67d0\u4e2a\u5173\u952e\u5b57\u51fa\u73b0\u5728grep\u4e2d\u7684\u6b21\u6570\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5728\u7f51\u7ad9\u6587\u4ef6\u4e2d\u641c\u7d22\u67d0\u4e2a\u8718\u86db\uff0c\u5219\u9700\u8981\u8ba1\u7b97\u5305\u542b\u7279\u5b9a\u7528\u6237\u4ee3\u7406\u7684\u884c\u6570\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u6dfb\u52a0\u201c-c\u201d\u53c2\u6570\u5373\u53ef\u3002 \u8bf7\u8bb0\u4f4f\uff0c\u8fd9\u4f1a\u5220\u9664grep\u7684\u5e38\u89c4\u8f93\u51fa\uff0c\u5e76\u4e14\u4e0d\u518d\u663e\u793a\u5355\u4e2a\u5339\u914d\u9879\u3002 \u4f8b\u5982\uff0c\u6ca1\u6709\u201c-c\u201d\u53c2\u6570\uff1a<\/p>\n

grep rr linuxidc-com<\/pre>\n

\u6211\u4eec\u5f97\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u4e8b\u4ef6\u5217\u8868\u3002 \u52a0\u5165\u201c-c\u201d\uff1a<\/p>\n

grep -c rr linuxidc-com<\/pre>\n

\u6211\u4eec\u5f97\u5230\u4e86\u884c\u6570\uff1a\u5982\u60a8\u6240\u89c1\uff0c\u5b83\u4e0d\u518d\u663e\u793a\u6bcf\u4e2a\u4e8b\u4ef6\u3002<\/p>\n

Grep\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u7684\u529f\u80fd\u591a\u5e74\u6765\u4e00\u76f4\u5728\u589e\u52a0\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u751a\u81f3\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u683c\u5f0f\u5316\u6587\u4ef6\u7684\u8f93\u51fa\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u9605\u8bfb\u3002 \u8fd9\u4e9b\u9ad8\u7ea7grep\u4f7f\u7528\u793a\u4f8b\u5c06\u8ba9\u60a8\u4f53\u9a8c\u53ef\u80fd\u7684\u5185\u5bb9\uff0c\u5e76\u5e0c\u671b\u60a8\u81ea\u5df1\u6df1\u5165\u7814\u7a76\u624b\u518c\uff01<\/p>\n

\u5982\u679c\u60a8\u559c\u6b22\u9ad8\u7ea7grep\u547d\u4ee4\u7684\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf7\u901a\u8fc7\u793e\u4ea4\u5a92\u4f53\u7f51\u7edc\u4e0e\u60a8\u7684\u670b\u53cb\u5206\u4eab\u3002 \u5982\u679c\u60a8\u5bf9\u672c\u6559\u7a0b\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u8bf7\u5728\u4e0b\u9762\u5199\u4e0b\u8bc4\u8bba\u3002 \u8c22\u8c22\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u901a\u5e38\uff0cgrep\u7684\u7528\u6cd5\u662f\u8fd9\u6837\u7684\uff1a grep ‘pattern’ filename \u5176\u4e2d’pattern’\u662f\u4efb\u4f55\u7ed9 […]<\/p>\n","protected":false},"author":310,"featured_media":228201,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-228189","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\/228189","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\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=228189"}],"version-history":[{"count":8,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228189\/revisions"}],"predecessor-version":[{"id":228238,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228189\/revisions\/228238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/228201"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=228189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=228189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=228189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}