{"id":275042,"date":"2023-06-15T20:10:04","date_gmt":"2023-06-15T12:10:04","guid":{"rendered":"https:\/\/lrxjmw.cn\/lgdhgmlsyzn.html"},"modified":"2023-06-15T20:10:04","modified_gmt":"2023-06-15T12:10:04","slug":"lgdhgmlsyzn","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lgdhgmlsyzn.html","title":{"rendered":"Linux\u7ba1\u9053\u548cgrep\u547d\u4ee4\u4f7f\u7528\u6307\u5357"},"content":{"rendered":"

Linux<\/a>\u4f5c\u4e3a\u4e00\u79cd\u5f00\u6e90\u7684\u64cd\u4f5c\u7cfb\u7edf<\/a>\uff0c\u5176\u5177\u6709\u5f3a\u5927\u7684\u547d\u4ee4<\/a>\u884c\u5de5\u5177\uff0c\u5176\u4e2d\u7ba1\u9053\u64cd\u4f5c\u548cgrep\u547d\u4ee4\u662f\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u5de5\u5177\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u4e2a\u5de5\u5177\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e2e\u52a9\u8bfb\u8005\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n

\u4e00\u3001\u7ba1\u9053\u64cd\u4f5c<\/p>\n

\u7ba1\u9053\u64cd\u4f5c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u547d\u4ee4\u7684\u8f93\u51fa\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u547d\u4ee4\u7684\u8f93\u5165\uff0c\u4ece\u800c\u5b9e\u73b0\u591a\u4e2a\u547d\u4ee4\u4e4b\u95f4\u7684\u534f\u4f5c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ls\u547d\u4ee4\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939suse linux \u4e0b\u8f7d\uff0c\u7136\u540e\u518d\u4f7f\u7528grep\u547d\u4ee4\u7b5b\u9009\u51fa\u5305\u542b\u201ctest\u201d\u7684\u6587\u4ef6\u540d\uff1a<\/p>\n

1. ls\u548cgrep\u7ed3\u5408\u4f7f\u7528<\/b><\/p>\n

\n

\nls | grep test\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u8f93\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u5305\u542b\u201ctest\u201d\u7684\u6587\u4ef6\u540d\u3002<\/p>\n

2.\u591a\u4e2a\u7ba1\u9053\u64cd\u4f5c\u7ed3\u5408\u4f7f\u7528<\/b><\/p>\n

\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u591a\u4e2a\u7ba1\u9053\u64cd\u4f5c\u7ed3\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\nps -ef | grep ssh | grep -v grep | awk '{print $2}'\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5217\u51fa\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684ssh\u8fdb\u7a0b\u7684PID\u3002<\/p>\n

3.\u5c06\u7ba1\u9053\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6<\/b><\/p>\n

\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u7ba1\u9053\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\nls | grep test > output.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5c06\u5305\u542b\u201ctest\u201d\u7684\u6587\u4ef6\u540d\u8f93\u51fa\u5230output.txt\u6587\u4ef6\u4e2d\u3002<\/p>\n

\u4e8c\u3001grep\u547d\u4ee4<\/p>\n

grep\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u5de5\u5177linux\u7cfb\u7edf\u754c\u9762\uff0c\u5b83\u53ef\u4ee5\u5728\u6307\u5b9a\u6587\u4ef6\u6216\u6807\u51c6\u8f93\u5165\u4e2d\u67e5\u627e\u6307\u5b9a\u7684\u6587\u672c\u3002\u4ee5\u4e0b\u662fgrep\u547d\u4ee4\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5\u3002<\/p>\n

1.\u67e5\u627e\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u884c<\/b><\/p>\n

\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u627e\u6587\u4ef6\u4e2d\u5305\u542b\u201chello\u201d\u7684\u884c\uff1a<\/p>\n

\n

\ngrep "hello" file.txt\n<\/pre>\n<\/p>\n

linux \u547d\u4ee4 grep_linux grep\u547d\u4ee4\u8be6\u89e3_linux\u7ba1\u9053\u64cd\u4f5c\u4ee5\u53cagrep\u547d\u4ee4\u7528\u6cd5<\/p>\n

2.\u67e5\u627e\u4e0d\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u884c<\/b><\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528-v\u9009\u9879\u6765\u67e5\u627e\u4e0d\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u4e32\u7684\u884clinux\u7ba1\u9053\u64cd\u4f5c\u4ee5\u53cagrep\u547d\u4ee4\u7528\u6cd5<\/strong>\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep -v"hello" file.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5217\u51fafile.txt\u6587\u4ef6\u4e2d\u4e0d\u5305\u542b\u201chello\u201d\u7684\u6240\u6709\u884c\u3002<\/p>\n

3.\u67e5\u627e\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c<\/b><\/p>\n

\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528grep\u547d\u4ee4\u67e5\u627e\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep -E"^[A-Z]" file.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5217\u51fafile.txt\u6587\u4ef6\u4e2d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u6240\u6709\u884c\u3002<\/p>\n

4.\u663e\u793a\u5339\u914d\u7ed3\u679c\u4e0a\u4e0b\u6587\u4fe1\u606f<\/b><\/p>\n

linux grep\u547d\u4ee4\u8be6\u89e3_linux \u547d\u4ee4 grep_linux\u7ba1\u9053\u64cd\u4f5c\u4ee5\u53cagrep\u547d\u4ee4\u7528\u6cd5<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528-C\u9009\u9879\u6765\u663e\u793a\u5339\u914d\u7ed3\u679c\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep -C 2 "hello" file.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5217\u51fafile.txt\u6587\u4ef6\u4e2d\u5305\u542b\u201chello\u201d\u7684\u884c\uff0c\u5e76\u663e\u793a\u6bcf\u4e2a\u5339\u914d\u7ed3\u679c\u524d\u540e\u4e24\u884c\u3002<\/p>\n

5.\u9012\u5f52\u641c\u7d22\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6<\/b><\/p>\n

\u5982\u679c\u6211\u4eec\u9700\u8981\u5728\u4e00\u4e2a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528-r\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep -r"hello"\/path\/to\/directory\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5728\/path\/to\/directory\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u641c\u7d22\u5305\u542b\u201chello\u201d\u7684\u6587\u4ef6\u3002<\/p>\n

6.\u663e\u793a\u5339\u914d\u7ed3\u679c\u884c\u53f7<\/b><\/p>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528-n\u9009\u9879\u6765\u663e\u793a\u5339\u914d\u7ed3\u679c\u6240\u5728\u884c\u7684\u884c\u53f7\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep -n"hello" file.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5217\u51fafile.txt\u6587\u4ef6\u4e2d\u5305\u542b\u201chello\u201d\u7684\u6240\u6709\u884c\uff0c\u5e76\u663e\u793a\u6bcf\u4e2a\u5339\u914d\u7ed3\u679c\u6240\u5728\u884c\u7684\u884c\u53f7\u3002<\/p>\n

7.\u5ffd\u7565\u5927\u5c0f\u5199\u5339\u914d<\/b><\/p>\n

\u5982\u679c\u6211\u4eec\u9700\u8981\u5ffd\u7565\u5927\u5c0f\u5199\u8fdb\u884c\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528-i\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep -i"hello" file.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5217\u51fafile.txt\u6587\u4ef6\u4e2d\u5305\u542b\u201chello\u201d\u6216\u201cHello\u201d\u7b49\u5b57\u7b26\u4e32\u7684\u6240\u6709\u884c\u3002<\/p>\n

8.\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\u8fdb\u884c\u5339\u914d<\/b><\/p>\n

\u6211\u4eec\u8fd8\u53ef\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\u8fdb\u884c\u5339\u914d\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\necho "hello world"| grep "hello"\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u8f93\u51fa\u5305\u542b\u201chello\u201d\u7684\u884c\u3002<\/p>\n

9.\u5c06\u5339\u914d\u7ed3\u679c\u9ad8\u4eae\u663e\u793a<\/b><\/p>\n

\u5982\u679c\u6211\u4eec\u9700\u8981\u5c06\u5339\u914d\u7ed3\u679c\u9ad8\u4eae\u663e\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528--color\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n

\n

\ngrep --color "hello" file.txt\n<\/pre>\n<\/p>\n

\u6b64\u547d\u4ee4\u4f1a\u5c06file.txt\u6587\u4ef6\u4e2d\u5305\u542b\u201chello\u201d\u7684\u5b57\u7b26\u4e32\u9ad8\u4eae\u663e\u793a\u3002<\/p>\n

10.\u5c06\u591a\u4e2a\u5339\u914d\u6761\u4ef6\u7ec4\u5408\u8d77\u6765<\/b><\/p>\n

\u6700\u540elinux\u7ba1\u9053\u64cd\u4f5c\u4ee5\u53cagrep\u547d\u4ee4\u7528\u6cd5<\/strong>\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u591a\u4e2a\u5339\u914d\u6761\u4ef6\u7ec4\u5408\u8d77\u6765\u8fdb\u884c\u641c\u7d22\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u547d\u4ee4\u4f1a\u5217\u51fafile.txt\u6587\u4ef6\u4e2d\u5305\u542b\u201chello\u201d\u6216\u201cworld\u201d\u7684\u6240\u6709\u884c\uff0c\u5e76\u4e14\u8fd9\u4e9b\u884c\u4e0d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff1a<\/p>\n

\n

\ngrep -E-v"^[A-Z]"-e"hello"-e"world" file.txt\n<\/pre>\n<\/p>\n

\u4ee5\u4e0a\u5c31\u662fLinux\u7ba1\u9053\u64cd\u4f5c\u548cgrep\u547d\u4ee4\u7684\u7528\u6cd5\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u4f7f\u7528Linux\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Linux\u4f5c\u4e3a\u4e00\u79cd\u5f00\u6e90\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5176\u5177\u6709\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5176\u4e2d\u7ba1\u9053\u64cd\u4f5c\u548cgrep\u547d\u4ee4\u662f\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u5de5\u5177\u3002ls\u548cgrep\u7ed3\u5408\u4f7f\u7528\u6b64\u547d\u4ee4\u4f1a\u5217\u51fa\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684ssh\u8fdb\u7a0b\u7684PID\u3002\u4ee5\u4e0b\u662fgrep\u547d\u4ee4\u7684\u4e00\u4e9b\u5e38\u89c1\u7528\u6cd5\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528grep\u547d\u4ee4\u67e5\u627e\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u884c\uff0c\u4f8b\u5982\uff1a\u4ee5\u4e0a\u5c31\u662fLinux\u7ba1\u9053\u64cd\u4f5c\u548cgrep\u547d\u4ee4\u7684\u7528\u6cd5\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u4f7f\u7528Linux\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n","protected":false},"author":1,"featured_media":275043,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-275042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/275042","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=275042"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/275042\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/275043"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=275042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=275042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=275042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}