{"id":85714,"date":"2023-12-23T22:18:31","date_gmt":"2023-12-23T14:18:31","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=85714"},"modified":"2023-12-23T22:18:31","modified_gmt":"2023-12-23T14:18:31","slug":"grep-command-instance","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/grep-command-instance.html","title":{"rendered":"grep \u547d\u4ee4\u7684 12 \u4e2a\u5b9e\u4f8b"},"content":{"rendered":"
\u4f60\u662f\u5426\u9047\u5230\u8fc7\u9700\u8981\u5728\u6587\u4ef6\u4e2d\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u6216\u8005\u6837\u5f0f\uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u4ece\u54ea\u513f\u5f00\u59cb\uff1f\u90a3\u4e48,\u5c31\u8bf7grep<\/strong>\u6765\u5e2e\u4f60\u5427\u3002<\/p>\n <\/p>\n grep<\/strong>\u662f\u6bcf\u4e2aLinux<\/strong>\u53d1\u884c\u7248\u90fd\u9884\u88c5\u7684\u4e00\u4e2a\u5f3a\u6709\u529b\u7684\u6587\u4ef6\u6a21\u5f0f\u641c\u7d22\u5de5\u5177\u3002\u65e0\u8bba\u4f55\u79cd\u539f\u56e0\uff0c\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u6ca1\u6709\u9884\u88c5\u5b83\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u901a\u8fc7\u7cfb\u7edf\u7684\u5305\u7ba1\u7406\u5668\u6765\u5b89\u88c5\u5b83\uff08Debian\/Ubuntu<\/strong>\u7cfb\u4e2d\u7684apt-get<\/strong>\u548cRHEl\/CentOS\/Fedora<\/strong>\u7cfb\u4e2d\u7684yum<\/strong>\uff09\u3002<\/p>\n \u6211\u53d1\u73b0\u4f7f\u7528\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u771f\u5b9e\u4f8b\u5b50\u8ba9\u4f60\u6295\u8eab\u5176\u4e2d\u662f\u8ba9\u4f60\u63a5\u89e6grep<\/strong>\u547d\u4ee4\u7684\u6700\u5bb9\u6613\u65b9\u5f0f\u3002<\/p>\n \u5047\u8bbe\u4f60\u5df2\u7ecf\u5728\u4f60\u7684\u7535\u8111\u4e0a\u5b89\u88c5\u4e86\u4e00\u4e2a\u5168\u65b0\u7684Ubuntu<\/strong>\uff0c\u7136\u540e\u4f60\u6253\u7b97\u5378\u8f7dPython<\/strong>\u3002\u4f60\u6d4f\u89c8\u7f51\u9875\u5bfb\u627e\u6559\u7a0b\uff0c\u4f46\u662f\u4f60\u53d1\u73b0\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684Python<\/strong>\u5728\u4f7f\u7528\uff0c\u800c\u4f60\u4e0d\u77e5\u9053\u4f60\u7684Ubuntu<\/strong>\u5b89\u88c5\u5668\u5230\u5e95\u5728\u4f60\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86\u54ea\u4e2a\u7248\u672c\u7684Python\uff0c\u4e5f\u4e0d\u77e5\u9053\u5b83\u5b89\u88c5\u4e86\u54ea\u4e9b\u6a21\u5757\u3002\u89e3\u51b3\u8fd9\u4e2a\u70e6\u607c\u53ea\u9700\u7b80\u5355\u7684\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n \u9996\u5148\uff0c\u6211\u4eec\u8fd0\u884cdpkg -l<\/strong>\u5217\u51fa\u4f60\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684.deb<\/strong>\u5305\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u7ba1\u9053\u5c06\u8f93\u51fa\u7ed3\u679c\u4f20\u8f93\u7ed9\u547d\u4ee4grep -i<\/strong> python\uff0c\u8fd9\u4e00\u6b65\u53ef\u4ee5\u7b80\u5355\u89e3\u91ca\u4e3a\u628a\u7ed3\u679c\u4f20\u8f93\u7ed9grep\u7136\u540e\u8fc7\u6ee4\u51fa\u6240\u6709\u542b\u6709python\u7684\u9879\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u2013i\u9009\u9879\u7528\u4e8e\u5ffd\u7565\u5927\u5c0f\u5199,\u56e0\u4e3a grep \u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\u3002\u4f7f\u7528\u9009\u9879-i<\/strong>\u662f\u4e2a\u597d\u4e60\u60ef\uff0c\u9664\u975e\u4f60\u6253\u7b97\u8fdb\u884c\u66f4\u7ec6\u8282\u7684\u641c\u7d22\u3002<\/p>\n grep\u8fd8\u53ef\u4ee5\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u91cc\u7528\u4e8e\u641c\u7d22\u548c\u8fc7\u6ee4\u3002\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u8fd9\u6837\u7684\u60c5\u666f\uff1a<\/p>\n \u4f60\u7684Apache\u7f51\u9875\u670d\u52a1\u5668<\/strong>\u51fa\u73b0\u4e86\u95ee\u9898\uff0c\u4f60\u4e0d\u5f97\u4e0d\u4ece\u8bb8\u591a\u4e13\u4e1a\u7f51\u7ad9\u91cc\u627e\u4e00\u4e2a\u53d1\u5e16\u8be2\u95ee\u3002\u597d\u5fc3\u56de\u590d\u4f60\u7684\u4eba\u8ba9\u4f60\u7c98\u8d34\u4e0a\u6765\u4f60\u7684\/etc\/apache2\/sites-available\/default-ssl<\/strong>\u6587\u4ef6\u5185\u5bb9\u3002\u5047\u5982\u4f60\u80fd\u79fb\u9664\u6389\u6240\u6709\u7684\u6ce8\u91ca\u884c\uff0c\u90a3\u4e48\u5bf9\u4f60\uff0c\u5bf9\u5e2e\u4f60\u7684\u4eba\uff0c\u4ee5\u53ca\u6240\u6709\u9605\u8bfb\u8be5\u6587\u4ef6\u7684\u4eba\uff0c\u4e0d\u662f\u66f4\u5bb9\u6613\u53d1\u73b0\u95ee\u9898\u5417\uff1f\u4f60\u5f53\u7136\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u505a\u5230\uff01\u53ea\u9700\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n \u9009\u9879-v<\/strong>\u662f\u544a\u8bc9grep<\/strong>\u547d\u4ee4\u53cd\u8f6c\u5b83\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u610f\u601d\u5c31\u662f\u4e0d\u8f93\u51fa\u5339\u914d\u7684\u9879\uff0c\u505a\u76f8\u53cd\u7684\u4e8b\uff0c\u6253\u5370\u51fa\u6240\u6709\u4e0d\u5339\u914d\u7684\u9879\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6709#<\/strong>\u7684\u662f\u6ce8\u91ca\u884c\uff08\u8bd1\u6ce8\uff1a\u5176\u5b9e\u8fd9\u4e2a\u547d\u4ee4\u5e76\u4e0d\u51c6\u786e\uff0c\u5305\u542b\u201c#\u201d\u7684\u884c\u4e0d\u5168\u662f\u6ce8\u91ca\u884c\u3002\u5173\u4e8e\u5982\u4f55\u7cbe\u786e\u5339\u914d\u6ce8\u91ca\u884c\uff0c\u53ef\u4ee5\u4e86\u89e3\u66f4\u591a\u7684\u5173\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5185\u5bb9\u3002\uff09\u3002<\/p>\n grep<\/strong>\u547d\u4ee4\u5bf9\u4e8e\u8fc7\u6ee4\u6765\u81ea\u4e8e\u6807\u51c6\u8f93\u51fa<\/strong>\u7684\u7ed3\u679c\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u7684\u4e00\u4e2a\u6587\u4ef6\u5939\u91cc\u9762\u5168\u662f\u5404\u79cd\u683c\u5f0f\u7684\u97f3\u4e50\u6587\u4ef6\u3002\u4f60\u8981\u627e\u51fa\u827a\u672f\u5bb6jayZ<\/strong>\u7684\u6240\u6709mp3<\/strong>\u683c\u5f0f\u7684\u97f3\u4e50\u6587\u4ef6\uff0c\u91cc\u9762\u4e5f\u4e0d\u8981\u6709\u4efb\u4f55\u6df7\u5408\u97f3\u8f68\u3002\u4f7f\u7528find\u547d\u4ee4<\/strong>\u518d\u7ed3\u5408\u7ba1\u9053\u4f7f\u7528grep<\/strong>\u5c31\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4e2a\u9b54\u6cd5\uff1a<\/p>\n \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528find\u547d\u4ee4\u6253\u5370\u51fa\u6240\u6709\u4ee5.mp3<\/strong>\u4e3a\u540e\u7f00\u540d\u7684\u6587\u4ef6\uff0c\u63a5\u7740\u5c06\u5176\u4f7f\u7528\u7ba1\u9053\u4f20\u9012\u7ed9grep -i<\/strong>\u8fc7\u6ee4\u548c\u6253\u5370\u51fa\u540d\u5b57\u4e3a\u201cJayZ<\/strong>\u201d\u7684\u6587\u4ef6,\u518d\u4f7f\u7528\u7ba1\u9053\u4f20\u9001\u7ed9grep -vi<\/strong>\u4ee5\u4fbf\u8fc7\u6ee4\u6389\u542b\u6709\u201cremix<\/strong>\u201d\u7684\u9879\u3002<\/p>\n \u53e6\u5916\u4e24\u4e2a\u9009\u9879\u662f-A\u548c-B\u4e4b\u95f4\u7684\u5207\u6362\uff0c\u662f\u7528\u4ee5\u663e\u793a\u5339\u914d\u7684\u884c\u4ee5\u53ca\u884c\u53f7\uff0c\u5206\u522b\u63a7\u5236\u5728\u5b57\u7b26\u4e32\u524d\u6216\u5b57\u7b26\u4e32\u540e\u663e\u793a\u7684\u884c\u6570\u3002Man\u9875\u7ed9\u51fa\u4e86\u66f4\u52a0\u8be6\u7ec6\u7684\u89e3\u91ca\uff0c\u6211\u53d1\u73b0\u4e00\u4e2a\u8bb0\u5fc6\u7684\u5c0f\u7a8d\u95e8\uff1a-A=after\u3001-B=before\u3002<\/p>\n grep\u547d\u4ee4\u7684-C<\/strong>\u9009\u9879\u548c\u4f8b4\u4e2d\u7684\u5f88\u76f8\u4f3c\uff0c\u4e0d\u8fc7\u6253\u5370\u7684\u5e76\u4e0d\u662f\u5728\u5339\u914d\u5b57\u7b26\u4e32\u7684\u524d\u9762\u6216\u540e\u9762\u7684\u884c\uff0c\u800c\u662f\u6253\u5370\u51fa\u4e24\u4e2a\u65b9\u5411\u90fd\u5339\u914d\u7684\u884c\uff08\u8bd1\u6ce8\uff1a\u540c\u4e0a\u9762\u7684\u8bb0\u5fc6\u7a8d\u95e8\u4e00\u6837\uff1a-C=center\uff0c\u4ee5\u6b64\u4e3a\u4e2d\u5fc3\uff09\uff1a<\/p>\n \u8fd9\u4e2a\u529f\u80fd\u7c7b\u4f3c\u4e8e\u5c06grep<\/strong>\u8f93\u51fa\u7684\u7ed3\u679c\u7528\u7ba1\u9053\u4f20\u9001\u7ed9\u8ba1\u6570\u5668\uff08wc<\/strong>\u7a0b\u5e8f\uff09\uff0cgrep\u5185\u5efa\u7684\u9009\u9879\u53ef\u4ee5\u8fbe\u5230\u540c\u6837\u7684\u76ee\u7684\uff1a<\/p>\n \u5f53\u4f60\u5728\u7f16\u8bd1\u51fa\u9519\u65f6\u9700\u8981\u8c03\u8bd5\u65f6\uff0cgrep<\/strong>\u547d\u4ee4\u7684-n<\/strong>\u9009\u9879\u662f\u4e2a\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\u3002\u5b83\u80fd\u544a\u8bc9\u4f60\u6240\u641c\u7d22\u7684\u5185\u5bb9\u5728\u6587\u4ef6\u7684\u54ea\u4e00\u884c\uff1a<\/p>\n \u5047\u82e5\u4f60\u8981\u5728\u5f53\u524d\u6587\u4ef6\u5939\u91cc\u641c\u7d22\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u5f53\u524d\u6587\u4ef6\u5939\u91cc\u53c8\u6709\u5f88\u591a\u5b50\u76ee\u5f55\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a-r<\/strong>\u9009\u9879\u4ee5\u4fbf\u4e8e\u9012\u5f52\u7684\u641c\u7d22\uff1a<\/p>\n \u4f20\u9012-w<\/strong>\u9009\u9879\u7ed9grep\u547d\u4ee4\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u7cbe\u786e\u5339\u914d\u641c\u7d22\uff08\u8bd1\u6ce8\uff1a\u5305\u542b\u8981\u641c\u7d22\u7684\u5355\u8bcd\uff0c\u800c\u4e0d\u662f\u901a\u914d\uff09\u3002\u4f8b\u5982\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u8f93\u5165\uff1a<\/p>\n \u5c06\u6253\u5370\u51fa\u542b\u6709\u5f15\u53f7\u5185\u5339\u914d\u9879\u7684\u884c\u3002\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8bd5\u4e00\u4e0b\u8fd9\u4e2a\uff1a<\/p>\n \u641c\u7d22\u8fd9\u4e2a\u5339\u914d\u9879\u65f6\uff0c\u82e5\u641c\u7d22\u7684\u4e1c\u897f\u91cc\u9762\u6ca1\u6709\u8fd9\u6837\u7684\u4e00\u4e2a\u5355\u72ec\u7684\u5355\u8bcd\uff0c\u5c06\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u8fd4\u56de\u3002<\/p>\n \u6211\u4eec\u8fd8\u8981\u5173\u6ce8\u4e00\u4e0bgrep\u7684\u884d\u751f\u5e94\u7528\u3002\u7b2c\u4e00\u4e2a\u662fzgrep<\/strong>\uff0c\u8fd9\u4e2a\u4e0ezcat<\/strong>\u5f88\u76f8\u4f3c\uff0c\u53ef\u4ee5\u7528\u4e8egzip<\/strong>\u538b\u7f29\u8fc7\u7684\u6587\u4ef6\u3002\u5b83\u6709\u4e0egrep<\/strong>\u76f8\u4f3c\u7684\u547d\u4ee4\u9009\u9879\uff0c\u4f7f\u7528\u65b9\u5f0f\u4e5f\u4e00\u6837\uff1a<\/p>\n egrep<\/strong>\u662f\u53e6\u4e00\u4e2a\u884d\u751f\u5e94\u7528\uff0c\u4ee3\u8868\u7740\u201c\u6269\u5c55\u5168\u5c40\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u201d\u3002\u5b83\u53ef\u4ee5\u8bc6\u522b\u66f4\u591a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff0c\u4f8b\u5982at + ?<\/strong> | \u548c\uff08\uff09<\/strong>\u3002\u5728\u641c\u7d22\u6e90\u4ee3\u7801\u6587\u4ef6\u65f6\uff0cegrep\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u8fd8\u6709\u5176\u4ed6\u7684\u4e00\u4e9b\u96f6\u788e\u4ee3\u7801\u6587\u4ef6\u7684\u641c\u7d22\u9700\u8981\uff0c\u4f7f\u5f97\u8fd9\u6837\u7684\u641c\u7d22\u80fd\u529b\u6210\u4e3a\u5fc5\u9700\u3002\u53ef\u4ee5\u5728grep\u547d\u4ee4\u4e2d\u4f7f\u7528\u9009\u9879-E<\/strong>\u6765\u542f\u7528\u5b83\u3002<\/p>\n fgrep<\/strong>\u7528\u4e8e\u5728\u4e00\u4e2a\u6587\u4ef6\u6216\u6587\u4ef6\u5217\u8868\u4e2d\u641c\u7d22\u56fa\u5b9a\u6837\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u529f\u80fd\u4e0egrep -F<\/strong>\u540c\u3002fgrep<\/strong>\u7684\u4e00\u4e2a\u901a\u5e38\u7528\u6cd5\u4e3a\u4f20\u9012\u4e00\u4e2a\u542b\u6709\u6837\u5f0f\u7684\u6587\u4ef6\u7ed9\u5b83\uff1a<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/blog.jobbole.com\/112580\/<\/a><\/p>\n $ sudo apt-get install grep #Debian\/Ubuntu $ sudo yum install grep #RHEL\/CentOS\/Fedora\r\n<\/pre>\n
$ sudo dpkg -l | grep -i python\r\n<\/pre>\n
ii python2.7 2.7.3-0ubuntu3.4 Interactive high-level object-oriented language (version 2.7)\r\n ii python2.7-minimal 2.7.3-0ubuntu3.4 Minimal subset of the Python language (version 2.7)\r\n ii python-openssl 0.12-1ubuntu2.1 Python wrapper around the OpenSSL library\r\n ii python-pam 0.4.2-12.2ubuntu4 A Python interface to the PAM library<\/pre>\n
$ sudo grep -v \"#\" \/etc\/apache2\/sites-available\/default-ssl<\/pre>\n
$ sudo find . -name \".mp3\" | grep -i JayZ | grep -vi \"remix\"<\/pre>\n
\n
$ sudo ifconfig | grep -A 4 etho $ sudo ifconfig | grep -B 2 UP<\/pre>\n
$ sudo ifconfig | grep -C 2 lo<\/pre>\n
$ sudo ifconfig | grep -c inet6<\/pre>\n
$ sudo grep -n \"main\" setup.py<\/pre>\n
$ sudo grep -r \u201cfunction\u201d *<\/pre>\n
$ sudo ifconfig | grep -w \u201cRUNNING\u201d<\/pre>\n
$ sudo ifconfig | grep -w \u201cRUN\u201d<\/pre>\n
$ sudo zgrep -i error \/var\/log\/syslog.2.gz<\/pre>\n
$ sudo grep -E<\/pre>\n
\u00a0$ sudo fgrep -f file_full_of_patterns.txt file_to_search.txt<\/pre>\n
\n