{"id":5986,"date":"2023-09-27T15:45:58","date_gmt":"2023-09-27T07:45:58","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=5986"},"modified":"2023-09-27T15:45:58","modified_gmt":"2023-09-27T07:45:58","slug":"linux%e6%96%87%e4%bb%b6%e5%86%85%e6%90%9c%e7%b4%a2%e6%80%8e%e4%b9%88%e5%81%9a%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/5986\/","title":{"rendered":"linux\u6587\u4ef6\u5185\u641c\u7d22\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"

\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u641c\u7d22\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u800c\u6587\u4ef6\u641c\u7d22\u7684\u65b9\u5f0f\u4e5f\u662f\u591a\u79cdhtml\u6587\u4ef6\u5939\u6253\u5305\u6210exe<\/a>\u591a\u6837\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u4ecb\u7ecdLinux\u7cfb\u7edf\u4e2d\u7684\u6587\u4ef6\u5185\u641c\u7d22\u539f\u7406\u53ca\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n

\u4e00\u3001\u6587\u4ef6\u5185\u641c\u7d22\u539f\u7406<\/p>\n

\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u5185\u641c\u7d22\u7684\u539f\u7406\u662f\u5229\u7528\u547d\u4ee4\u884c\u5de5\u5177\u641c\u7d22\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u4e32\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u547d\u4ee4\u884c\u5de5\u5177\u901a\u8fc7\u6253\u5f00\u6587\u4ef6\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u5339\u914d\u641c\u7d22\uff0c\u6700\u540e\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002\u5176\u4e2d\uff0c\u5e38\u7528\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6709grep\u3001ack\u3001ag\u3001find\u7b49\u3002<\/p>\n

\u4e8c\u3001\u5e38\u7528\u7684\u6587\u4ef6\u5185\u641c\u7d22\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n

1. grep<\/p>\n

grep\u662fLinux\u7cfb\u7edf\u4e2d\u6700\u5e38\u7528\u7684\u6587\u4ef6\u5185\u641c\u7d22\u547d\u4ee4\u884c\u5de5\u5177\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u641c\u7d22\u6587\u4ef6\u4e2d\u5339\u914d\u67d0\u4e2a\u5b57\u7b26\u4e32\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

grep “hello” file.txt<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728file.txt\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

2. ack<\/p>\n

ack\u662fgrep\u7684\u4e00\u4e2a\u66ff\u4ee3\u54c1\uff0c\u5b83\u6bd4grep\u66f4\u5feb\uff0c\u66f4\u667a\u80fd\uff0c\u5e76\u652f\u6301Perl\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

ack “hello” file.txt<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728file.txt\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

3. ag<\/p>\n

ag\u662f\u4e00\u4e2a\u7c7b\u4f3c\u4e8eack\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4e5f\u662f\u4e00\u4e2a\u5feb\u901f\u3001\u667a\u80fd\u7684\u6587\u4ef6\u641c\u7d22\u5de5\u5177\u3002\u5b83\u652f\u6301\u591a\u7ebf\u7a0b\u641c\u7d22\uff0c\u5e76\u4e14\u53ef\u4ee5\u641c\u7d22\u538b\u7f29\u6587\u4ef6\u548cGit\u4ed3\u5e93\u4e2d\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

ag “hello” file.txt<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728file.txt\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

4. find<\/p>\n

find\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u641c\u7d22\u6307\u5b9a\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u652f\u6301\u6309\u7167\u6587\u4ef6\u540d\u3001\u6587\u4ef6\u7c7b\u578b\u3001\u6587\u4ef6\u5927\u5c0f\u3001\u6587\u4ef6\u521b\u5efa\u65f6\u95f4\u7b49\u591a\u79cd\u6761\u4ef6\u8fdb\u884c\u641c\u7d22\u3002\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

find \/path\/to\/dir\/ -name “*.txt” -exec grep “hello” {} \\;<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728\/path\/to\/dir\/\u76ee\u5f55\u4e0b\u641c\u7d22\u6240\u6709\u6269\u5c55\u540d\u4e3a.txt\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

\u4e09\u3001\u6587\u4ef6\u5185\u641c<\/p>\n

<\/figure>\n<\/p>\n

\u7d22\u547d\u4ee4\u884c\u5de5\u5177\u7684\u4f7f\u7528\u6280\u5de7<\/p>\n

1. \u641c\u7d22\u591a\u4e2a\u6587\u4ef6<\/p>\n

\u5982\u679c\u8981\u641c\u7d22\u591a\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26*\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

grep “hello” *.txt<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728\u6240\u6709\u6269\u5c55\u540d\u4e3a.txt\u7684\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

2. \u641c\u7d22\u6392\u9664\u67d0\u4e9b\u6587\u4ef6<\/p>\n

\u5982\u679c\u8981\u641c\u7d22\u6392\u9664\u67d0\u4e9b\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528grep\u7684-v\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

grep -v “hello” file.tx\u5982\u4f55\u5c06\u7f51\u9875\u6253\u5305\u4e3aexe\u6267\u884c<\/a>t<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728file.txt\u6587\u4ef6\u4e2d\u641c\u7d22\u4e0d\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

3. \u641c\u7d22\u5ffd\u7565\u5927\u5c0f\u5199<\/p>\n

\u5982\u679c\u8981\u641c\u7d22\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u4f7f\u7528grep\u7684-i\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

grep -i “hello” file.txt<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728file.txt\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09\uff0c\u5e76\u8f93\u51fa\u5339\u914d\u7ed3\u679c\u3002<\/p>\n

4. \u641c\u7d22\u663e\u793a\u5339\u914d\u884c\u7684\u4e0a\u4e0b\u6587<\/p>\n

\u5982\u679c\u8981\u641c\u7d22\u663e\u793a\u5339\u914d\u884c\u7684\u4e0a\u4e0b\u6587\uff0c\u53ef\u4ee5\u4f7f\u7528grep\u7684-A\u3001-B\u3001-C\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n

“`<\/p>\n

grep -A 2 -B 2 “hello” file.txt<\/p>\n

“`<\/p>\n

\u8868\u793a\u5728file.txt\u6587\u4ef6\u4e2d\u641c\u7d22\u5305\u542b”hello”\u5b57\u7b26\u4e32\u7684\u884c\uff0c\u5e76\u663e\u793a\u5339\u914d\u884c\u7684\u524d\u4e24\u884c\u548c\u540e\u4e24\u884c\uff08\u51715\u884c\uff09\u3002<\/p>\n

\u603b\u7ed3<\/p>\n

\u672c\u6587\u4ecb\u7ecd\u4e86Linux\u7cfb\u7edf\u4e2d\u6587\u4ef6\u5185\u641c\u7d22\u7684\u539f\u7406\u53ca\u5e38\u7528\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5e76\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u4f7f\u7528\u6280\u5de7\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884c\u6587\u4ef6\u641c\u7d22\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u641c\u7d22\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u800c\u6587\u4ef6\u641c\u7d22\u7684\u65b9\u5f0f\u4e5f\u662f\u591a\u79cd\u591a\u6837\u3002\u672c\u6587\u5c06\u4e3a\u60a8\u4ecb\u7ecdLinux\u7cfb\u7edf\u4e2d\u7684\u6587\u4ef6\u5185\u641c\u7d22\u539f\u7406\u53ca\u8be6\u7ec6\u4ecb\u7ecd\u3002\u4e00\u3001\u6587\u4ef6\u5185\u641c\u7d22\u539f\u7406\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6587\u4ef6\u5185\u641c\u7d22\u7684\u539f\u7406\u662f\u5229\u7528\u547d\u4ee4<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[8383,11029,1622,979,16],"topic":[],"class_list":["post-5986","post","type-post","status-publish","format-standard","hentry","category-exekf","tag-exe","tag-vueexe","tag-1622","tag-979","tag-16"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/5986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=5986"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/5986\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=5986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=5986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=5986"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=5986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}