{"id":85,"date":"2025-06-24T09:23:06","date_gmt":"2025-06-24T01:23:06","guid":{"rendered":"http:\/\/1.14.207.80\/?p=85"},"modified":"2025-06-24T09:23:35","modified_gmt":"2025-06-24T01:23:35","slug":"linux%e5%9f%ba%e7%a1%80%ef%bc%88%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.zhangjianjia.xyz\/index.php\/2025\/06\/24\/linux%e5%9f%ba%e7%a1%80%ef%bc%88%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%ef%bc%89\/","title":{"rendered":"Linux\u57fa\u7840\uff08\u6b63\u5219\u8868\u8fbe\u5f0f\uff09"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h1>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u6587\u672c\u5904\u7406\u4e09\u5251\u5ba2\uff08grep, sed, awk\uff09<\/h1>\n<h2>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u6838\u5fc3\u77e5\u8bc6<\/h2>\n<h3>1. \u6b63\u5219\u8868\u8fbe\u5f0f\u7c7b\u578b\u5bf9\u6bd4<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u7c7b\u578b<\/strong><\/th>\n<th><strong>\u6807\u8bc6\u7b26<\/strong><\/th>\n<th><strong>\u7279\u70b9<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u57fa\u7840\u6b63\u5219 (BRE)<\/td>\n<td>\u9ed8\u8ba4<\/td>\n<td>\u5143\u5b57\u7b26\u9700\u8f6c\u4e49\uff08\u5982<code>\\+<\/code>, <code>\\(<\/code>\uff09<\/td>\n<td><code>grep &quot;a\\+&quot; file<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u6269\u5c55\u6b63\u5219 (ERE)<\/td>\n<td><code>-E<\/code><\/td>\n<td>\u5143\u5b57\u7b26\u514d\u8f6c\u4e49\uff08\u5982<code>+<\/code>, <code>(<\/code>\uff09<\/td>\n<td><code>grep -E &quot;a+&quot; file<\/code><\/td>\n<\/tr>\n<tr>\n<td>Perl\u6b63\u5219 (PCRE)<\/td>\n<td><code>-P<\/code><\/td>\n<td>\u652f\u6301\u7279\u6b8a\u5b57\u7b26\u7c7b\uff08<code>\\d<\/code>, <code>\\w<\/code>\uff09<\/td>\n<td><code>grep -P &quot;\\d+&quot; file<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2. \u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u7d20\u8be6\u89e3<\/h3>\n<h4>\u5355\u5b57\u7b26\u5339\u914d<\/h4>\n<table>\n<thead>\n<tr>\n<th><strong>\u8868\u8fbe\u5f0f<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>.<\/code><\/td>\n<td>\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/td>\n<td><code>a.c<\/code> \u2192 abc, adc<\/td>\n<\/tr>\n<tr>\n<td><code>[abc]<\/code><\/td>\n<td>\u5339\u914d\u6307\u5b9a\u5b57\u7b26\u4e4b\u4e00<\/td>\n<td><code>[aeiou]<\/code> \u2192 a, e<\/td>\n<\/tr>\n<tr>\n<td><code>[^abc]<\/code><\/td>\n<td>\u5339\u914d\u975e\u6307\u5b9a\u5b57\u7b26<\/td>\n<td><code>[^0-9]<\/code> \u2192 a, B<\/td>\n<\/tr>\n<tr>\n<td><code>\\d<\/code><\/td>\n<td>\u6570\u5b57\uff08\u4ec5PCRE\uff09<\/td>\n<td><code>\\d\\d<\/code> \u2192 12, 34<\/td>\n<\/tr>\n<tr>\n<td><code>\\w<\/code><\/td>\n<td>\u5355\u8bcd\u5b57\u7b26\uff08\u4ec5PCRE\uff09<\/td>\n<td><code>\\w+<\/code> \u2192 hello, world<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u91cf\u8bcd\u5339\u914d<\/h4>\n<table>\n<thead>\n<tr>\n<th><strong>\u91cf\u8bcd<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>BRE<\/strong><\/th>\n<th><strong>ERE\/PCRE<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>*<\/code><\/td>\n<td>0\u6b21\u6216\u591a\u6b21<\/td>\n<td><code>a*<\/code><\/td>\n<td>\u540c\u5de6<\/td>\n<\/tr>\n<tr>\n<td><code>+<\/code><\/td>\n<td>1\u6b21\u6216\u591a\u6b21<\/td>\n<td><code>a\\+<\/code><\/td>\n<td><code>a+<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>?<\/code><\/td>\n<td>0\u6b21\u62161\u6b21<\/td>\n<td><code>a\\?<\/code><\/td>\n<td><code>a?<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>{n}<\/code><\/td>\n<td>\u6070\u597dn\u6b21<\/td>\n<td><code>a\\{3\\}<\/code><\/td>\n<td><code>a{3}<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>{n,}<\/code><\/td>\n<td>\u81f3\u5c11n\u6b21<\/td>\n<td><code>a\\{3,\\}<\/code><\/td>\n<td><code>a{3,}<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>{n,m}<\/code><\/td>\n<td>n\u5230m\u6b21<\/td>\n<td><code>a\\{2,4\\}<\/code><\/td>\n<td><code>a{2,4}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u4f4d\u7f6e\u951a\u5b9a<\/h4>\n<table>\n<thead>\n<tr>\n<th><strong>\u951a\u70b9<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>^<\/code><\/td>\n<td>\u884c\u9996<\/td>\n<td><code>^start<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>$<\/code><\/td>\n<td>\u884c\u5c3e<\/td>\n<td><code>end$<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\&lt;<\/code><\/td>\n<td>\u8bcd\u9996<\/td>\n<td><code>\\&lt;word<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\&gt;<\/code><\/td>\n<td>\u8bcd\u5c3e<\/td>\n<td><code>word\\&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\b<\/code><\/td>\n<td>\u5355\u8bcd\u8fb9\u754c\uff08\u4ec5PCRE\uff09<\/td>\n<td><code>\\bword\\b<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u5206\u7ec4\u4e0e\u5f15\u7528<\/h4>\n<table>\n<thead>\n<tr>\n<th><strong>\u8868\u8fbe\u5f0f<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>( )<\/code><\/td>\n<td>\u5206\u7ec4<\/td>\n<td><code>(abc)+<\/code> \u2192 abcabc<\/td>\n<\/tr>\n<tr>\n<td><code>\\1, \\2...<\/code><\/td>\n<td>\u5f15\u7528\u5206\u7ec4<\/td>\n<td><code>(f)or\\1<\/code> \u2192 forf<\/td>\n<\/tr>\n<tr>\n<td><code>(?: )<\/code><\/td>\n<td>\u975e\u6355\u83b7\u5206\u7ec4\uff08PCRE\uff09<\/td>\n<td><code>(?:ab)+<\/code> \u2192 abab<\/td>\n<\/tr>\n<tr>\n<td><code>(?= )<\/code><\/td>\n<td>\u6b63\u5411\u5148\u884c\u65ad\u8a00\uff08PCRE\uff09<\/td>\n<td><code>Windows(?=10)<\/code> \u2192 Windows10<\/td>\n<\/tr>\n<tr>\n<td><code>(?&lt;= )<\/code><\/td>\n<td>\u6b63\u5411\u540e\u884c\u65ad\u8a00\uff08PCRE\uff09<\/td>\n<td><code>(?&lt;=MS)Windows<\/code> \u2192 MSWindows<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e8c\u3001grep\u6587\u672c\u641c\u7d22\u5927\u5e08<\/h2>\n<h3>\u5e38\u7528\u9009\u9879\u901f\u67e5\u8868<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u9009\u9879<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>-i<\/code><\/td>\n<td>\u5ffd\u7565\u5927\u5c0f\u5199<\/td>\n<td><code>grep -i &quot;error&quot; log.txt<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-v<\/code><\/td>\n<td>\u53cd\u5411\u5339\u914d<\/td>\n<td><code>grep -v &quot;debug&quot; app.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-r<\/code><\/td>\n<td>\u9012\u5f52\u641c\u7d22\u76ee\u5f55<\/td>\n<td><code>grep -r &quot;TODO&quot; src\/<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-l<\/code><\/td>\n<td>\u53ea\u663e\u793a\u6587\u4ef6\u540d<\/td>\n<td><code>grep -l &quot;pattern&quot; *.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-c<\/code><\/td>\n<td>\u7edf\u8ba1\u5339\u914d\u884c\u6570<\/td>\n<td><code>grep -c &quot;success&quot; report.txt<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-n<\/code><\/td>\n<td>\u663e\u793a\u884c\u53f7<\/td>\n<td><code>grep -n &quot;warning&quot; messages.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-o<\/code><\/td>\n<td>\u53ea\u663e\u793a\u5339\u914d\u90e8\u5206<\/td>\n<td><code>grep -o &quot;http:\/\/.*&quot; access.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-A N<\/code><\/td>\n<td>\u663e\u793a\u5339\u914d\u540eN\u884c<\/td>\n<td><code>grep -A 2 &quot;Exception&quot; error.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-B N<\/code><\/td>\n<td>\u663e\u793a\u5339\u914d\u524dN\u884c<\/td>\n<td><code>grep -B 1 &quot;fatal&quot; crash.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-C N<\/code><\/td>\n<td>\u663e\u793a\u5339\u914d\u524d\u540e\u5404N\u884c<\/td>\n<td><code>grep -C 3 &quot;critical&quot; system.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-P<\/code><\/td>\n<td>\u4f7f\u7528Perl\u6b63\u5219<\/td>\n<td><code>grep -P &quot;\\d{3}-\\d{4}&quot; file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-E<\/code><\/td>\n<td>\u4f7f\u7528\u6269\u5c55\u6b63\u5219<\/td>\n<td>`grep -E &quot;apple<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<ol>\n<li>\n<p><strong>\u67e5\u627eIP\u5730\u5740<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">grep -P '((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])' access.log\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u7edf\u8ba1\u9519\u8bef\u7c7b\u578b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">grep -c &quot;ERROR&quot; app.log\ngrep -c &quot;WARNING&quot; app.log\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u63d0\u53d6URL\u53c2\u6570<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">grep -oP 'https?:\/\/\\K[^\/?]+' access.log\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>\u4e09\u3001sed\u6d41\u7f16\u8f91\u5668<\/h2>\n<h3>sed\u547d\u4ee4\u7ed3\u6784<\/h3>\n<pre><code>sed [\u9009\u9879] '\u8303\u56f4 \u547d\u4ee4' \u6587\u4ef6\n<\/code><\/pre>\n<h3>\u5e38\u7528\u9009\u9879<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u9009\u9879<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>-n<\/code><\/td>\n<td>\u7981\u6b62\u9ed8\u8ba4\u8f93\u51fa<\/td>\n<td><code>sed -n 'p' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-e<\/code><\/td>\n<td>\u591a\u547d\u4ee4\u7f16\u8f91<\/td>\n<td><code>sed -e 's\/a\/b\/' -e 's\/c\/d\/'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-i[.SUF]<\/code><\/td>\n<td>\u76f4\u63a5\u4fee\u6539\u6587\u4ef6\uff08\u53ef\u5907\u4efd\uff09<\/td>\n<td><code>sed -i.bak 's\/old\/new\/' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-r<\/code><\/td>\n<td>\u4f7f\u7528\u6269\u5c55\u6b63\u5219<\/td>\n<td><code>sed -r 's\/(ab)+\/cd\/' file<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u8303\u56f4\u6307\u5b9a<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u8303\u56f4<\/strong><\/th>\n<th><strong>\u542b\u4e49<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>10<\/code><\/td>\n<td>\u7b2c10\u884c<\/td>\n<td><code>sed '10d' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>10,20<\/code><\/td>\n<td>10\u523020\u884c<\/td>\n<td><code>sed '10,20s\/a\/b\/'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\/start\/,\/end\/<\/code><\/td>\n<td>\u4ecestart\u5230end\u7684\u884c<\/td>\n<td><code>sed '\/BEGIN\/,\/END\/p'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>$<\/code><\/td>\n<td>\u6700\u540e\u4e00\u884c<\/td>\n<td><code>sed '$s\/$\/END\/'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>1~2<\/code><\/td>\n<td>\u5947\u6570\u884c<\/td>\n<td><code>sed '1~2d' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>2~3<\/code><\/td>\n<td>\u4ece\u7b2c2\u884c\u5f00\u59cb\u6bcf\u96943\u884c<\/td>\n<td><code>sed '2~3s\/a\/b\/'<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u547d\u4ee4<\/strong><\/th>\n<th><strong>\u529f\u80fd<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>s\/\/\/<\/code><\/td>\n<td>\u66ff\u6362\u6587\u672c<\/td>\n<td><code>sed 's\/old\/new\/g' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>p<\/code><\/td>\n<td>\u6253\u5370\u884c<\/td>\n<td><code>sed -n '\/pattern\/p' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>d<\/code><\/td>\n<td>\u5220\u9664\u884c<\/td>\n<td><code>sed '\/debug\/d' app.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>a\\text<\/code><\/td>\n<td>\u884c\u540e\u8ffd\u52a0<\/td>\n<td><code>sed '3a\\New Line' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>i\\text<\/code><\/td>\n<td>\u884c\u524d\u63d2\u5165<\/td>\n<td><code>sed '\/header\/i\\Title' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>c\\text<\/code><\/td>\n<td>\u66ff\u6362\u6574\u884c<\/td>\n<td><code>sed '5c\\Replacement' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>y\/abc\/ABC\/<\/code><\/td>\n<td>\u5b57\u7b26\u8f6c\u6362<\/td>\n<td><code>sed 'y\/abc\/ABC\/' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>r file<\/code><\/td>\n<td>\u8bfb\u53d6\u6587\u4ef6\u63d2\u5165<\/td>\n<td><code>sed '\/pattern\/r data.txt'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>w file<\/code><\/td>\n<td>\u5199\u5165\u5339\u914d\u884c\u5230\u6587\u4ef6<\/td>\n<td><code>sed '\/pattern\/w output.txt'<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<ol>\n<li>\n<p><strong>\u6279\u91cf\u66ff\u6362<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">sed -i.bak 's\/http:\\\/\\\/old-site\/https:\\\/\\\/new-site\/g' *.html\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u6ce8\u91ca\u548c\u7a7a\u884c<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">sed -e '\/^#\/d' -e '\/^$\/d' config.ini\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u591a\u884c\u5185\u5bb9<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">sed '\/Database Settings\/a\\\nDB_HOST=localhost\\\nDB_PORT=3306\\\nDB_USER=admin' config.cfg\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>\u56db\u3001awk\u6587\u672c\u5904\u7406\u8bed\u8a00<\/h2>\n<h3>awk\u5904\u7406\u6a21\u578b<\/h3>\n<pre><code>awk 'BEGIN {\u521d\u59cb\u5316} \u6a21\u5f0f {\u52a8\u4f5c} END {\u6536\u5c3e}' \u6587\u4ef6\n<\/code><\/pre>\n<h3>\u5185\u7f6e\u53d8\u91cf<\/h3>\n<table>\n<thead>\n<tr>\n<th><strong>\u53d8\u91cf<\/strong><\/th>\n<th><strong>\u542b\u4e49<\/strong><\/th>\n<th><strong>\u793a\u4f8b<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>$0<\/code><\/td>\n<td>\u6574\u884c\u5185\u5bb9<\/td>\n<td><code>awk '{print $0}' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>$1-$N<\/code><\/td>\n<td>\u7b2cN\u5217\u5b57\u6bb5<\/td>\n<td><code>awk '{print $2}' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>NF<\/code><\/td>\n<td>\u5f53\u524d\u884c\u5b57\u6bb5\u6570<\/td>\n<td><code>awk 'NF&gt;5{print}' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>NR<\/code><\/td>\n<td>\u5f53\u524d\u884c\u53f7<\/td>\n<td><code>awk 'NR==2{print}' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>FS<\/code><\/td>\n<td>\u8f93\u5165\u5b57\u6bb5\u5206\u9694\u7b26<\/td>\n<td><code>awk -F: '{print $1}'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>OFS<\/code><\/td>\n<td>\u8f93\u51fa\u5b57\u6bb5\u5206\u9694\u7b26<\/td>\n<td>`awk &#8216;BEGIN{OFS=&quot;<\/td>\n<\/tr>\n<tr>\n<td><code>RS<\/code><\/td>\n<td>\u8f93\u5165\u8bb0\u5f55\u5206\u9694\u7b26<\/td>\n<td><code>awk 'BEGIN{RS=&quot;;&quot;}{print}'<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>ORS<\/code><\/td>\n<td>\u8f93\u51fa\u8bb0\u5f55\u5206\u9694\u7b26<\/td>\n<td><code>awk 'BEGIN{ORS=&quot;\\n\\n&quot;}{print}'<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<ol>\n<li>\n<p><strong>\u63d0\u53d6\u7279\u5b9a\u5217<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">awk -F: '{print $1, $3}' \/etc\/passwd\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u7edf\u8ba1\u6587\u4ef6\u4fe1\u606f<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">awk '{chars += length($0); words += NF} END{print &quot;Lines:&quot;, NR, &quot;Words:&quot;, words, &quot;Chars:&quot;, chars}' file.txt\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>HTTP\u72b6\u6001\u7801\u7edf\u8ba1<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">awk '{status[$9]++} END{for(s in status) print s, status[s]}' access.log | sort -nr\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b971-100\u5947\u6570\u548c<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\">seq 100 | awk '{if($1%2) sum+=$1} END{print sum}'\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2>\u4e94\u3001\u901a\u914d\u7b26 vs \u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<table>\n<thead>\n<tr>\n<th><strong>\u7279\u6027<\/strong><\/th>\n<th><strong>\u901a\u914d\u7b26<\/strong><\/th>\n<th><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/th>\n<th><strong>\u793a\u4f8b\u5bf9\u6bd4<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u7528\u9014<\/strong><\/td>\n<td>\u6587\u4ef6\u540d\u5339\u914d<\/td>\n<td>\u6587\u672c\u5185\u5bb9\u5339\u914d<\/td>\n<td><code>ls *.txt<\/code> vs <code>grep &quot;.*txt&quot;<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>\u547d\u4ee4<\/strong><\/td>\n<td><code>ls<\/code>, <code>cp<\/code>, <code>find<\/code><\/td>\n<td><code>grep<\/code>, <code>sed<\/code>, <code>awk<\/code><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><code>*<\/code><\/td>\n<td>\u4efb\u610f\u5b57\u7b26\u5e8f\u5217<\/td>\n<td>\u524d\u5143\u7d200\u6b21\u6216\u591a\u6b21<\/td>\n<td><code>*.log<\/code> vs <code>.*\\.log<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>?<\/code><\/td>\n<td>\u5355\u4e2a\u5b57\u7b26<\/td>\n<td>\u524d\u5143\u7d200\u6b21\u62161\u6b21<\/td>\n<td><code>file?.txt<\/code> vs <code>file.\\.txt<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>[]<\/code><\/td>\n<td>\u5b57\u7b26\u96c6\u5408<\/td>\n<td>\u5b57\u7b26\u96c6\u5408<\/td>\n<td><code>[abc]*<\/code> vs <code>[abc].*<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>.<\/code><\/td>\n<td>\u5b57\u9762\u70b9\u5b57\u7b26<\/td>\n<td>\u4efb\u610f\u5b57\u7b26<\/td>\n<td><code>file.txt<\/code> vs <code>file\\.txt<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>^<\/code><\/td>\n<td>\u65e0\u7279\u6b8a\u542b\u4e49<\/td>\n<td>\u884c\u9996<\/td>\n<td>&#8211; vs <code>^start<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>$<\/code><\/td>\n<td>\u65e0\u7279\u6b8a\u542b\u4e49<\/td>\n<td>\u884c\u5c3e<\/td>\n<td>&#8211; vs <code>end$<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>{}<\/code><\/td>\n<td>\u6269\u5c55\u5e8f\u5217<\/td>\n<td>\u9700\u8f6c\u4e49<\/td>\n<td><code>{1..10}<\/code> vs <code>\\{1,10\\}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u516d\u3001\u9ad8\u7ea7\u6280\u5de7\u5408\u96c6<\/h2>\n<h3>1. \u7ec4\u5408\u4f7f\u7528\u4e09\u5251\u5ba2<\/h3>\n<pre><code class=\"language-bash\"># \u67e5\u627e\u8bbf\u95ee\u91cf\u6700\u9ad8\u76845\u4e2aIP\ngrep -oP '\\d+\\.\\d+\\.\\d+\\.\\d+' access.log | sort | uniq -c | sort -nr | head -5\n\n# \u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\nls *.jpg | awk '{print &quot;mv&quot;, $0, &quot;image_&quot; NR &quot;.jpg&quot;}' | bash\n<\/code><\/pre>\n<h3>2. \u5b9e\u7528\u5355\u884c\u547d\u4ee4<\/h3>\n<pre><code class=\"language-bash\"># \u5220\u9664\u6587\u4ef6\u4e2d\u7684\u7a7a\u884c\nsed '\/^$\/d' file.txt\n\n# \u5728\u6bcf\u884c\u884c\u9996\u6dfb\u52a0\u884c\u53f7\nawk '{print NR, $0}' file.txt\n\n# \u63d0\u53d6XML\u6807\u7b7e\u5185\u5bb9\ngrep -oP '&lt;title&gt;\\K[^&lt;]+' page.html\n\n# \u8f6c\u6362CSV\u4e3aTSV\nsed 's\/,\/\\t\/g' data.csv\n<\/code><\/pre>\n<h3>3. \u6027\u80fd\u4f18\u5316\u6280\u5de7<\/h3>\n<pre><code class=\"language-bash\"># \u5904\u7406\u5927\u6587\u4ef6\u65f6\u4f7f\u7528LC_ALL=C\u52a0\u901f\nLC_ALL=C grep &quot;pattern&quot; huge.log\n\n# \u4f7f\u7528mawk\u4ee3\u66ffgawk\u52a0\u901f\u5904\u7406\nsudo apt install mawk\nmawk '{print $1}' bigfile.txt\n<\/code><\/pre>\n<h3>4. \u5b89\u5168\u6ce8\u610f\u4e8b\u9879<\/h3>\n<pre><code class=\"language-bash\"># \u4f7f\u7528\u5907\u4efd\u8fdb\u884c\u539f\u5730\u7f16\u8f91\nsed -i.bak 's\/password\/******\/' config.txt\n\n# \u6d4b\u8bd5\u540e\u518d\u6267\u884c\nsed 's\/danger\/safe\/g' file | less  # \u786e\u8ba4\u65e0\u8bef\u540e\u518d\u52a0-i\n<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<ol>\n<li>\n<p><strong>\u6b63\u5219\u9009\u62e9<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7b80\u5355\u5339\u914d \u2192 \u57fa\u7840\u6b63\u5219<\/li>\n<li>\u590d\u6742\u903b\u8f91 \u2192 Perl\u6b63\u5219<\/li>\n<li>\u517c\u5bb9\u6027\u597d \u2192 \u6269\u5c55\u6b63\u5219<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5de5\u5177\u9009\u7528<\/strong>\uff1a<\/p>\n<ul>\n<li>\u6587\u672c\u641c\u7d22 \u2192 <code>grep<\/code><\/li>\n<li>\u6d41\u7f16\u8f91 \u2192 <code>sed<\/code><\/li>\n<li>\u6570\u636e\u5904\u7406 \u2192 <code>awk<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9ad8\u6548\u7ec4\u5408<\/strong>\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u5206\u6790Nginx\u8bbf\u95ee\u65e5\u5fd7\nawk '{print $1}' access.log | sort | uniq -c | sort -nr | head -10\n\n# \u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\nls | awk '{print &quot;mv&quot;, $0, tolower($0)}' | bash\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u9677\u9631<\/strong>\uff1a<\/p>\n<ul>\n<li>\u901a\u914d\u7b26\u7528\u4e8e\u6587\u4ef6\u540d\uff0c\u6b63\u5219\u7528\u4e8e\u6587\u672c\u5185\u5bb9<\/li>\n<li>\u4f7f\u7528<code>-i<\/code>\u524d\u52a1\u5fc5\u5907\u4efd\u6216\u6d4b\u8bd5<\/li>\n<li>\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u6ce8\u610f\u8f6c\u4e49<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-linux"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":1,"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions\/86"}],"wp:attachment":[{"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangjianjia.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}