{"id":181243,"date":"2020-05-15T09:04:05","date_gmt":"2020-05-15T01:04:05","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=181243"},"modified":"2020-05-02T17:34:31","modified_gmt":"2020-05-02T09:34:31","slug":"awk-command-script","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/awk-command-script.html","title":{"rendered":"awk\u547d\u4ee4\u548c\u811a\u672c\u7684\u7f16\u5199\u542f\u8499"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nawk\u7684\u547d\u4ee4\u662f\u4e00\u79cd\u5904\u7406\u6216\u5206\u6790\u6587\u672c\u6587\u4ef6\u7684\u5f3a\u5927\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u6309\u884c\u548c\u5217\u7ec4\u7ec7\u7684\u6570\u636e\u6587\u4ef6\u3002\u53ef\u4ee5\u4ece\u547d\u4ee4\u884c\u8fd0\u884c\u7b80\u5355\u7684awk\u547d\u4ee4\u3002\u66f4\u590d\u6742\u7684\u4efb\u52a1\u5e94\u8be5\u4f5c\u4e3aawk\u7a0b\u5e8f\uff08\u6240\u8c13\u7684awk\u811a\u672c\uff09\u5199\u5165\u6587\u4ef6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

awk\u547d\u4ee4\u7684\u57fa\u672c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

awk 'pattern {action}' input-file > output-file<\/pre>\n

\u8fd9\u610f\u5473\u7740\uff1a\u53d6\u8f93\u5165\u6587\u4ef6\u7684\u6bcf\u4e00\u884c; \u5982\u679c\u8be5\u884c\u5305\u542b\u8be5\u6a21\u5f0f\uff0c\u5219\u5c06\u8be5\u64cd\u4f5c\u5e94\u7528\u4e8e\u8be5\u884c\u5e76\u5c06\u7ed3\u679c\u884c\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u3002 \u5982\u679c\u7701\u7565\u8be5\u6a21\u5f0f\uff0c\u5219\u8be5\u64cd\u4f5c\u5c06\u5e94\u7528\u4e8e\u6240\u6709\u884c\u3002 \u4f8b\u5982\uff1a<\/p>\n

linuxidc@Ubuntu:~\/linuxidc.com$ awk '{ print $5 }' linuxidc.txt > linuxmi.txt<\/pre>\n

\u8be5\u8bed\u53e5\u83b7\u53d6\u6bcf\u884c\u7b2c5\u5217\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u884c\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u201coutput.txt\u201d\u4e2d\u3002 \u53d8\u91cf'$4'\u6307\u7684\u662f\u7b2c\u56db\u5217\u3002 \u7c7b\u4f3c\u5730\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528$1\uff0c$2\uff0c$3\u7b49\u8bbf\u95ee\u7b2c\u4e00\u5217\uff0c\u7b2c\u4e8c\u5217\u548c\u7b2c\u4e09\u5217\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5047\u5b9a\u5217\u7531\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff08\u6240\u8c13\u7684\u7a7a\u683c\uff09\u5206\u9694\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u8f93\u5165\u6587\u4ef6\u201clinuxidc.txt\u201d\u5305\u542b\u4ee5\u4e0b\u884c\uff1a<\/p>\n

    \n1, linuxidc com, Title 599, Price $9.30
    \n2, ninhao linuxidc, Title 761, Price $5.90
    \n3, naike xie, Title 880, Price $9.10
    \n4, Lady Gaga, Title 198, Price $1.30
    \n5, Johnny Cash, Title 582, Price $6.50
    \n6, Elvis Presley, Title 370, Price $9.30
    \n7, John Lennon, Title 571, Price $8.90
    \n8, Michael Jackson, Title 673, Price $7.50
    \n9, linuxidc com, Title 585, Price $1.80<\/ul>\n

    \u7136\u540e\u8be5\u547d\u4ee4\u5c06\u4ee5\u4e0b\u884c\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u201clinuxmi.txt\u201d\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ cat linuxmi.txt\r\n599,\r\n761,\r\n880,\r\n198,\r\n582,\r\n370,\r\n571,\r\n673,\r\n585,<\/pre>\n

    \"\"<\/p>\n

    \u5982\u679c\u5217\u5206\u9694\u7b26\u4e0d\u662f\u7a7a\u683c\u6216\u5236\u8868\u7b26\uff08\u4f8b\u5982\u9017\u53f7\uff09\uff0c\u5219\u53ef\u4ee5\u5728awk\u8bed\u53e5\u4e2d\u6307\u5b9a\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ awk -F, '{ print $3 }' linuxidc.txt > linuxmi.txt<\/pre>\n

    \u5982\u679c\u8ba4\u4e3a\u5217\u88ab\u9017\u53f7\u5206\u9694\uff0c\u8fd9\u5c06\u4ece\u6bcf\u884c\u7684\u7b2c3\u5217\u4e2d\u9009\u62e9\u5143\u7d20\u3002 \u56e0\u6b64\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ cat linuxmi.txt\r\n Title 599\r\n Title 761\r\n Title 880\r\n Title 198\r\n Title 582\r\n Title 370\r\n Title 571\r\n Title 673\r\n Title 585<\/pre>\n

    \"\"<\/p>\n

    \u5927\u62ec\u53f7\u5185\u7684\u8bed\u53e5\u5217\u8868\uff08'{'\uff0c'}'\uff09\u79f0\u4e3a\u5757\u3002 \u5982\u679c\u5c06\u6761\u4ef6\u8868\u8fbe\u5f0f\u653e\u5728\u5757\u524d\u9762\uff0c\u5219\u53ea\u6709\u6761\u4ef6\u4e3a\u771f\u65f6\u624d\u4f1a\u6267\u884c\u5757\u5185\u7684\u8bed\u53e5\u3002<\/p>\n

    awk '$7==\"\\$7.30\" { print $3 }' linuxidc.txt<\/pre>\n

    \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6761\u4ef6\u662f$7==\"\\$7.30\"\uff0c\u8fd9\u610f\u5473\u7740\u7b2c7\u5217\u7684\u5143\u7d20\u7b49\u4e8e$7.30\u3002 \u7f8e\u5143\u7b26\u53f7\u524d\u9762\u7684\u53cd\u659c\u6760\u7528\u4e8e\u963b\u6b62\u7cfb\u7edf\u5c06$7\u89e3\u91ca\u4e3a\u53d8\u91cf\uff0c\u800c\u662f\u4ece\u5b57\u9762\u4e0a\u7406\u89e3\u7f8e\u5143\u7b26\u53f7\u3002<\/p>\n

    \u56e0\u6b64\uff0c\u8fd9\u4e2aawk\u8bed\u53e5\u6253\u5370\u51fa\u6bcf\u884c\u7b2c3\u5217\u7684\u5143\u7d20\uff0c\u7b2c7\u5217\u7684\u201c$7.30\u201d\u3002<\/p>\n

    \u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u6761\u4ef6\u3002 \u4f8b\u5982\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ awk '$7==\"\\$7.30\" { print $3 }' linuxidc.txt<\/pre>\n

    \u4e24\u4e2a\u659c\u6760\uff08'\/'\uff09\u4e4b\u95f4\u7684\u5b57\u7b26\u4e32\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ea\u662f\u5b57\u7b26\u4e32\u201c30\u201d\u3002 \u8fd9\u610f\u5473\u7740\u5982\u679c\u4e00\u884c\u5305\u542b\u5b57\u7b26\u4e32\u201c30\u201d\uff0c\u7cfb\u7edf\u5c06\u5728\u8be5\u884c\u7684\u7b2c3\u5217\u6253\u5370\u51fa\u8be5\u5143\u7d20\u3002 \u4e0a\u4f8b\u4e2d\u7684\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ awk '\/30\/ { print $3 }' linuxidc.txt\r\ncom,\r\nGaga,\r\nPresley,<\/pre>\n

    \"\"<\/p>\n

    \u5982\u679c\u8868\u5143\u7d20\u662f\u6570\u5b57\uff0cawk\u53ef\u4ee5\u5728\u5b83\u4eec\u4e0a\u8fd0\u884c\u8ba1\u7b97\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ awk '{ print ($2 * $3) + $7 }' linuxidc.com.txt<\/pre>\n

    \"\"<\/p>\n

    \u9664\u4e86\u8bbf\u95ee\u5f53\u524d\u884c\u5143\u7d20\uff08$1\uff0c$2\u7b49\uff09\u7684\u53d8\u91cf\u4e4b\u5916\uff0c\u8fd8\u6709\u53d8\u91cf$0\u8868\u793a\u5b8c\u6574\u884c\uff08\u884c\uff09\uff0c\u53d8\u91cfNF\u8868\u793a\u5b57\u6bb5\u6570\u3002<\/p>\n

    \u60a8\u8fd8\u53ef\u4ee5\u5728\u6b64\u793a\u4f8b\u4e2d\u5b9a\u4e49\u65b0\u53d8\u91cf\uff1a<\/p>\n

    linuxidc@ubuntu:~\/linuxidc.com$ awk '{ sum=0; for (col=1; col<=NF; col++) sum += $col; print sum; }' linuxidc.com.txt<\/pre>\n

    \"\"<\/p>\n

    \u8fd9\u5c06\u8ba1\u7b97\u5e76\u6253\u5370\u6bcf\u884c\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002<\/p>\n

    awk\u8bed\u53e5\u7ecf\u5e38\u4e0esed\u547d\u4ee4\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    awk\u547d\u4ee4\u7684\u57fa\u672c\u683c\u5f0f\u5982\u4e0b\uff1a awk ‘pattern {action}’ input-file > outpu […]<\/p>\n","protected":false},"author":310,"featured_media":181260,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-181243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181243","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\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=181243"}],"version-history":[{"count":10,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181243\/revisions"}],"predecessor-version":[{"id":181295,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/181243\/revisions\/181295"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/181260"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=181243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=181243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=181243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}