{"id":847,"date":"2024-01-03T11:19:32","date_gmt":"2024-01-03T03:19:32","guid":{"rendered":"https:\/\/22pig.com\/?p=847"},"modified":"2024-01-03T11:19:33","modified_gmt":"2024-01-03T03:19:33","slug":"linux-%e4%b8%89%e5%89%91%e5%ae%a2-awk%ef%bc%8csed%ef%bc%8cgrep","status":"publish","type":"post","link":"http:\/\/22pig.com\/?p=847","title":{"rendered":"linux \u4e09\u5251\u5ba2 awk\uff0csed\uff0cgrep"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Linux\u4e09\u5251\u5ba2 awk\uff0csed\uff0cgrep<\/h2>\n\n\n\n<p>Linux\u4e2d\u7684\u4e09\u5251\u5ba2\u662f\u6307awk\uff0csed\uff0cgrep\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>awk\u64c5\u957f\u53d6\u5217<\/li>\n\n\n\n<li>sed\u64c5\u957f\u53d6\u884c\u548c\u66ff\u6362<\/li>\n\n\n\n<li>grep\u64c5\u957f\u67e5\u627e\u529f\u80fd<\/li>\n<\/ul>\n\n\n\n<p><strong>1\u3001awk<\/strong><\/p>\n\n\n\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<p>awk\u662f\u4e00\u79cd\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u8bed\u8a00\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u3002<\/p>\n\n\n\n<p><strong>\u4e3e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u67e5\u627enginx\u7684\u8fdb\u7a0bid\nps aux|grep nginx|grep -v grep|awk '{print $2}';\n\n#\u4f7f\u7528\"\uff1a\"\u5206\u5272\uff0c\u6253\u5370\u6587\u4ef6\u7b2c1\u5217\nawk -F ':' '{print $1}' \/etc\/passwd;\n\n#\u4f7f\u7528\"\uff1a\"\u5206\u5272\uff0c\u6253\u5370\u6587\u4ef6\u7b2c1\uff0c2\uff0c3\u5217\nawk -F ':' '{print $1, $2, $3}' \/etc\/passwd;\n\n#\u683c\u5f0f\u5316\u8f93\u51fa\nawk -F ':' '{ printf(\"%5s  %5s  %5s\\n\", $1, $2, $3) }' \/etc\/passwd;\n\n#\u4f7f\u7528\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u5148\u4f7f\u7528\"1\"\u5206\u5272\uff0c\u7136\u540e\u5bf9\u5206\u5272\u7ed3\u679c\u518d\u4f7f\u7528\"\uff1a\"\u5206\u5272\uff0c\u6253\u5370\u6587\u4ef6\u7b2c1\uff0c2\uff0c3\u5217\nawk -F '&#91;1 :]' '{print $1, $2, $3}' \/etc\/passwd;\n\n#\u4f7f\u7528-v\u8bbe\u7f6e\u53d8\u91cf\uff0c1+a\uff1a\u5728\u7b2c1\u5217\u540e\u9762\u6dfb\u52a0a\u5217\nawk -F ':' -va='666' '{print $1, $1+a}' \/etc\/passwd;\n\n#\u4f7f\u7528-v\u8bbe\u7f6e\u53d8\u91cf\uff0c1b\uff1a\u5728\u7b2c1\u5217\u540e\u9762\u62fc\u63a5b\u503c\u4f5c\u4e3a\u65b0\u76841\u5217\nawk -F ':' -va='666' -vb='abc' '{print $1, $1+a, $1b}' \/etc\/passwd;\n\n#\u6839\u636e\u5173\u952e\u5b57\u5339\u914d\u67d0\u4e00\u884c\nawk '\/root\/' \/etc\/passwd;<\/code><\/pre>\n\n\n\n<p><strong>2\u3001sed<\/strong><\/p>\n\n\n\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<p>sed\u4e3b\u8981\u7528\u6765\u81ea\u52a8\u7f16\u8f91\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u3001\u7b80\u5316\u5bf9\u6587\u4ef6\u7684\u53cd\u590d\u64cd\u4f5c\u3001\u7f16\u5199\u8f6c\u6362\u7a0b\u5e8f\u7b49\u3002<\/p>\n\n\n\n<p><strong>\u4e3e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u66ff\u6362:\u5c06\u6587\u672c\u91cc\u6240\u6709aaa\u66ff\u6362\u4e3abbb\u3002s:search \u67e5\u627e\u5e76\u66ff\u6362; g:global \u5168\u90e8\u66ff\u6362; -i:implace; * \u901a\u914d\u7b26\u3002sed -i \"s\/aaa\/bbb\/g\" \/tmp\/\/\/demo.txt;\n\n#\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002$\u83b7\u53d6\u6587\u4ef6\u7684\u672b\u5c3e, a\u8ffd\u52a0\u5728\u6587\u4ef6\u672b\u5c3e\u3002sed -i '$a\\\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9' \/tmp\/demo.txt;\n\n#\u5728\u6307\u5b9a\u884c\uff08\u5173\u952e\u5b57\uff09\u524d\u9762\u6216\u540e\u9762\u6dfb\u52a0\u5185\u5bb9\u3002\u524d\u9762\u8ffd\u52a0i; \u540e\u9762\u8ffd\u52a0a\u3002sed -i '\/aaa\/ i\\\u6307\u5b9a\u884c\uff08\u5173\u952e\u5b57\uff09\u524d\u52a0' \/tmp\/demo.txt;\nsed -i '\/aaa\/ a\\\u6307\u5b9a\u884c\uff08\u5173\u952e\u5b57\uff09\u540e\u52a0' \/tmp\/demo.txt;\n\n#\u5728\u6307\u5b9a\u884c\uff08\u5173\u952e\u5b57\uff09\u524d\u9762\u6216\u540e\u9762\u6dfb\u52a0\u5185\u5bb9\u3002\u524d\u9762\u8ffd\u52a0i; \u540e\u9762\u8ffd\u52a0a\u3002sed -i '\/aaa\/i \u6307\u5b9a\u884c\uff08\u5173\u952e\u5b57\uff09\u524d\u52a0' \/tmp\/demo.txt;\nsed -i '\/aaa\/a \u6307\u5b9a\u884c\uff08\u5173\u952e\u5b57\uff09\u540e\u52a0' \/tmp\/demo.txt;\n\n#\u5728\u6307\u5b9a\u884c\uff08\u884c\u53f7\uff09\u524d\u9762\u6216\u8005\u540e\u9762\u6dfb\u52a0\u5185\u5bb9\u3002\u524d\u9762\u8ffd\u52a0i; \u540e\u9762\u8ffd\u52a0a\u3002sed -i '10 i \u6307\u5b9a\u884c\uff08\u884c\u53f7\uff09\u524d\u52a0' \/tmp\/demo.txt;\nsed -i '10 a \u6307\u5b9a\u884c\uff08\u884c\u53f7\uff09\u540e\u52a0' \/tmp\/demo.txt;\n\n#\/ \u9700\u7528 \\ \u6765\u8f6c\u4e49\nsed -i \"s\/&lt;core&gt;&#91;0-9]&lt;\/core&gt;.*$\/&lt;core&gt;2&lt;\/core&gt;\/g\" \/tmp\/demo.txt<\/code><\/pre>\n\n\n\n<p><strong>3\u3001grep<\/strong><\/p>\n\n\n\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<p>grep(global regular expression print\uff0c\u5168\u5c40\u6b63\u5219\u8868\u8fbe\u5f0f\u8f93\u51fa)\u662f\u4e00\u4e2a\u641c\u7d22\u5de5\u5177\u3002<\/p>\n\n\n\n<p>grep\u547d\u4ee4\u53ef\u6839\u636e\u6307\u5b9a\u6a21\u5f0f\u5bf9\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u8fdb\u884c\u641c\u7d22\uff0cgrep\u4f1a\u5c06\u5305\u542b\u6a21\u5f0f\u7684\u4e00\u884c\u8f93\u51fa\u3002<\/p>\n\n\n\n<p><strong>\u4e3e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5728\u5355\u4e2a\u6587\u4ef6\u4e2d\u67e5\u627e\ngrep root \/etc\/passwd;\n\n#\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u67e5\u627e\ngrep root \/etc\/passwd \/etc\/shadow \/etc\/gshadow;\n\n#\u4f7f\u7528 -v \u53c2\u6570\u6392\u9664\u6307\u5b9a\u6a21\u5f0f\ngrep -v root \/etc\/passwd;\n\n#\u4f7f\u7528 -c \u53c2\u6570\u8ba1\u7b97\u6a21\u5f0f\u5339\u914d\u5230\u7684\u6570\u91cf\ngrep -c root \/etc\/passwd;\n\n#\u4f7f\u7528 -n \u53c2\u6570\u5728\u6587\u4ef6\u4e2d\u67e5\u627e\u6307\u5b9a\u6a21\u5f0f\u5e76\u663e\u793a\u5339\u914d\u884c\u7684\u884c\u53f7\ngrep -n root \/etc\/passwd;\n\n#\u4f7f\u7528 ^$ \u67e5\u627e\u6587\u4ef6\u4e2d\u6240\u6709\u7a7a\u884c\ngrep ^$ \/etc\/passwd;\n\n#\u4f7f\u7528 -e|-E \u53c2\u6570\u67e5\u627e\u591a\u4e2a\u6a21\u5f0f\ngrep -e '6901' -e 'root' \/etc\/passwd;\ngrep -E '6901|root' \/etc\/passwd;\n\n#\u4f7f\u7528 -r \u53c2\u6570\u9012\u5f52\u5730\u67e5\u627e\u6307\u5b9a\u6a21\u5f0f\ngrep -r root \/etc\/;\n\n#\u4f7f\u7528 ^ \u7b26\u53f7\u8f93\u51fa\u6240\u6709\u4ee5\u6307\u5b9a\u6a21\u5f0f\u5f00\u5934\u7684\u884c\ngrep ^root \/etc\/passwd;\n\n#\u4f7f\u7528 $ \u7b26\u53f7\u8f93\u51fa\u6240\u6709\u4ee5\u6307\u5b9a\u6a21\u5f0f\u7ed3\u5c3e\u7684\u884c\ngrep bash$ \/etc\/passwd;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u4e09\u5251\u5ba2 awk\uff0csed\uff0cgrep Linux\u4e2d\u7684\u4e09\u5251\u5ba2\u662f\u6307awk\uff0csed\uff0cgrep\u3002 1\u3001awk \u7b80\u4ecb awk\u662f\u4e00\u79cd\u5904\u7406\u6587\u672c\u6587\u4ef6\u7684\u8bed\u8a00\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\u3002 \u4e3e\u4f8b 2\u3001sed \u7b80\u4ecb sed\u4e3b\u8981\u7528\u6765\u81ea\u52a8\u7f16\u8f91\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u3001\u7b80\u5316\u5bf9\u6587\u4ef6\u7684\u53cd\u590d\u64cd\u4f5c\u3001\u7f16\u5199\u8f6c\u6362\u7a0b\u5e8f\u7b49\u3002 \u4e3e\u4f8b 3\u3001grep \u7b80\u4ecb grep(global regular expression print\uff0c\u5168\u5c40\u6b63\u5219\u8868\u8fbe\u5f0f\u8f93\u51fa)\u662f\u4e00\u4e2a\u641c\u7d22\u5de5\u5177\u3002 grep\u547d\u4ee4\u53ef\u6839\u636e\u6307\u5b9a\u6a21\u5f0f\u5bf9\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u8fdb\u884c\u641c\u7d22\uff0cgrep\u4f1a\u5c06\u5305\u542b\u6a21\u5f0f\u7684\u4e00\u884c\u8f93\u51fa\u3002 \u4e3e\u4f8b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-847","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=847"}],"version-history":[{"count":2,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/847\/revisions"}],"predecessor-version":[{"id":862,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/847\/revisions\/862"}],"wp:attachment":[{"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=847"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}