awk \u63d0\u4f9b\u7684\u6700\u7b80\u5355\u7684\u7528\u6cd5\u662f\u4ece\u6587\u4ef6\u6216\u901a\u8fc7\u7ba1\u9053\u4f20\u8f93\u5230\u5b83\u7684\u6570\u636e\u4e2d\u9009\u62e9\u7279\u5b9a\u5b57\u6bb5\u3002\u9ed8\u8ba4\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5b57\u6bb5\u5206\u9694\u7b26\uff1a<\/p>\n
\r\n[root@localhost ~]# echo one two three four five | awk '{print $4}'\r\nfour\r\n[root@localhost ~]# who | awk '{print $1}'\r\nroot\r\nroot\r\n<\/pre>\n\u5728\u4e0a\u9762\u663e\u793a\u7684\u547d\u4ee4\u4e2d\uff0cawk \u4ec5\u4ece\u63d0\u4f9b\u7684\u6570\u636e\u4e2d\u63d0\u53d6\u7b2c\u56db\u4e2a\u5b57\u6bb5\u548c\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u3002<\/p>\n
awk \u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728 awk \u547d\u4ee4\u540e\u6dfb\u52a0\u6587\u4ef6\u540d\u6765\u4ece\u6587\u4ef6\u4e2d\u63d0\u53d6\u6587\u672c\u3002<\/p>\n
\r\n[root@localhost ~]# awk -F ':' '{print \"user: \"$1,\"home: \"$6,\"shell: \"$NF}' \/etc\/passwd\r\n<\/pre>\n
\n\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cawk \u4f7f\u7528-F ':'<\/code>\u9009\u9879\u6307\u5b9a \u5192\u53f7 \u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u9009\u51fa\u4e86\u6587\u672c\u4e2d\u7684\u7b2c\u4e00\u5217\u3001\u7b2c\u516d\u5217\u548c\u6700\u540e\u4e00\u5217\u3002<\/p>\n\u547d\u4ee4\u4e2d\u7684 $NF<\/code> \u9009\u62e9\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u3002\u8fd9\u662f\u56e0\u4e3a NF \u8868\u793a\u4e00\u884c\u4e2d\u7684\u5b57\u6bb5\u6570\uff0c\u800c$NF<\/code> \u8868\u793a\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n\u5b57\u6bb5\u53ef\u4ee5\u6309\u4f60\u8ba4\u4e3a\u6709\u7528\u7684\u4efb\u4f55\u987a\u5e8f\u6253\u5370\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6b63\u5728\u91cd\u65b0\u6392\u5217 date \u547d\u4ee4\u8f93\u51fa\u4e2d\u7684\u5b57\u6bb5\u3002<\/p>\n
\r\n[root@localhost ~]# date | awk '{print \"Date:\",$6,$2,$3}'\r\nDate: 2021 Sep 26\r\n<\/pre>\n
\n\u5982\u679c\u4f60\u5e0c\u671b\u8f93\u51fa\u7531\u5176\u4ed6\u7b26\u53f7\u6765\u505a\u5206\u9694\u7b26\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 OFS<\/code>\uff08\u8f93\u51fa\u5b57\u6bb5\u5206\u9694\u7b26\uff09\u6307\u5b9a\u8f93\u51fa\u5206\u9694\u7b26\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\r\n[root@localhost ~]# date | awk '{OFS=\"-\"; print $6,$2,$3}'\r\n2021-Sep-26\r\n<\/pre>\n<\/p>\n
\u8bc4\u4f30\u5185\u5bb9<\/strong><\/div>\n\u8fd8\u53ef\u4ee5\u4f7f\u7528 awk \u8bc4\u4f30\u5b57\u6bb5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u5728 \/etc\/passwd \u6587\u4ef6\u4e2d\u5217\u51faUID\u5927\u4e8e1000\u7684\u7528\u6237\u4fe1\u606f\uff1a<\/p>\n
\r\n[root@localhost ~]# awk -F ':' '$3 >= 1000' \/etc\/passwd\r\nnobody:x:65534:65534:Kernel Overflow User:\/:\/sbin\/nologin\r\nuser01:x:1000:1000::\/home\/user01:\/bin\/bash\r\ntest01:x:1001:1001::\/home\/test01:\/bin\/bash\r\nnatasha:x:1002:1002::\/home\/natasha:\/bin\/bash\r\n<\/pre>\n
\n\u5982\u679c\u8981\u4e3a\u5217\u8868\u6dfb\u52a0\u6807\u9898\uff0c\u53ef\u4ee5\u6dfb\u52a0 BEGIN \u5b50\u53e5\uff0c\u5982\u679c\u6807\u9898\u60f3\u8981\u591a\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\\n<\/code>\u6362\u884c\u7b26\uff1a<\/p>\n\r\n[root@localhost ~]# awk -F ':' 'BEGIN {print \"user accounts\\n==============\"} $3 >= 1000' \/etc\/passwd\r\nuser accounts\r\n==============\r\nnobody:x:65534:65534:Kernel Overflow User:\/:\/sbin\/nologin\r\nuser01:x:1000:1000::\/home\/user01:\/bin\/bash\r\ntest01:x:1001:1001::\/home\/test01:\/bin\/bash\r\nnatasha:x:1002:1002::\/home\/natasha:\/bin\/bash\r\n<\/pre>\n<\/p>\n
\u7528awk\u505a\u6570\u5b66\u8ba1\u7b97<\/strong><\/div>\nawk \u63d0\u4f9b\u4e86\u7b97\u6570\u80fd\u529b\uff0c\u53ef\u4ee5\u8ba1\u7b97\u5e73\u65b9\u6839\u3001\u5bf9\u6570\u3001\u5207\u7ebf\u7b49\u3002<\/p>\n
\r\n[root@localhost ~]# awk 'BEGIN {print sqrt(2021)}'\r\n44.9555\r\n[root@localhost ~]# awk 'BEGIN {print log(2019)}'\r\n7.61036\r\n<\/pre>\n<\/p>\n
\u7f16\u5199 awk \u811a\u672c<\/strong><\/div>\n\u8fd8\u53ef\u4ee5\u4f7f\u7528 awk \u7f16\u5199\u72ec\u7acb\u811a\u672c\u3002\u8fd9\u662f\u4e00\u4e2a\u6a21\u4eff\u524d\u9762\u63d0\u4f9b\u7684\u793a\u4f8b\u4e4b\u4e00\u7684\u793a\u4f8b\uff0c\u4f46\u4e5f\u8ba1\u7b97\u4e86\u7cfb\u7edf\u4e0a\u62e5\u6709\u5e10\u6237\u7684\u7528\u6237\u6570\u3002<\/p>\n
\r\n[root@localhost ~]# vim list_users\r\n#!\/usr\/bin\/awk -f\r\n# This line is a comment\r\n\r\nBEGIN {\r\n printf \"%s\\n\",\"User accounts:\"\r\n print \"==============\"\r\n FS=\":\"\r\n n=0\r\n}\r\n\r\n# Now we'll run through the data\r\n{\r\n if ($3 >= 1000) {\r\n print $1\r\n n ++\r\n }\r\n}\r\n \r\nEND {\r\n print \"==============\"\r\n print n \" accounts\"\r\n}\r\n<\/pre>\n
\n\u8bf7\u6ce8\u610f\uff0c\u4ec5\u5728\u811a\u672c\u542f\u52a8\u65f6\u8fd0\u884c\u7684 BEGIN \u90e8\u5206\uff0c\u63d0\u4f9b\u6807\u9898\u3001\u6307\u793a\u5b57\u6bb5\u5206\u9694\u7b26\u5e76\u8bbe\u7f6e\u4ece 0 \u5f00\u59cb\u7684\u8ba1\u6570\u5668\u3002\u8be5\u811a\u672c\u8fd8\u5305\u62ec\u4e00\u4e2a END \u90e8\u5206\uff0c\u8be5\u90e8\u5206\u4ec5\u5728\u811a\u672c\u6700\u540e\u8fd0\u884c\u3002<\/p>\n
\r\n[root@localhost ~]# .\/list_users \/etc\/passwd\r\nUser accounts:\r\n==============\r\nnobody\r\nsystemd-coredump\r\nsystemd-resolve\r\npolkitd\r\nsssd\r\nchrony\r\nunbound\r\nuser01\r\ntest01\r\nnatasha\r\n==============\r\n10 accounts\r\n<\/pre>\n<\/p>\n
\u8ba1\u7b97\u6587\u4ef6\u4e2d\u7684\u884c\u6570<\/strong><\/div>\n\u8981\u4f7f\u7528 awk \u6253\u5370\u6587\u4ef6\u4e2d\u7684\u884c\u6570\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n
\r\n[root@localhost ~]# awk 'END {print NR}' \/etc\/passwd\r\n26\r\n<\/pre>\n
\n\u547d\u4ee4\u4e2d\u5305\u542b END \u610f\u5473\u7740\u5728\u5904\u7406\u884c\u4e4b\u540e\u63d0\u4f9b\u8f93\u51fa\u3002NR\uff08\u8bb0\u5f55\u6570\uff09\u8868\u793a\u6587\u4ef6\u4e2d\u7684\u884c\u6570\u3002<\/p>\n
\u83b7\u53d6\u6700\u5e38\u4f7f\u7528\u7684\u547d\u4ee4<\/strong><\/div>\n\u8fd8\u53ef\u4ee5\u5c06 awk \u4e0e\u8bb8\u591a\u5176\u4ed6\u547d\u4ee4\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u67e5\u770b\u5728\u5f53\u524d\u5386\u53f2\u6587\u4ef6\u91cc\u9762\u4f60\u6700\u5e38\u4f7f\u7528\u7684\u547d\u4ee4\u3002<\/p>\n
\r\n[root@localhost ~]# history | awk '{print $2}'|sort|uniq -c|sort -nr|head -5\r\n 58 lvs\r\n 48 ll\r\n 31 vim\r\n 29 vgs\r\n 29 df\r\n<\/pre>\n<\/p>\n
\u603b\u7ed3<\/strong><\/div>\nawk \u547d\u4ee4\u63d0\u4f9b\u7684\u4e0d\u4ec5\u4ec5\u662f\u4ece\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u9009\u62e9\u5b57\u6bb5\uff0c\u8fd8\u5305\u62ec\u63d0\u53d6\u6570\u636e\u5217\u3001\u6253\u5370\u7b80\u5355\u6587\u672c\u3001\u8bc4\u4f30\u5185\u5bb9\u2014\u2014\u751a\u81f3\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5982\u679c\u53ea\u5728\u9700\u8981\u4ece\u6587\u672c\u884c\u4e2d\u9009\u62e9\u7279\u5b9a\u5b57\u6bb5\u65f6\u624d\u4f7f\u7528 awk\uff0c\u4f60\u53ef\u80fd\u4f1a\u9519\u8fc7\u8be5\u547d\u4ee4\u53ef\u4ee5\u63d0\u4f9b\u7684\u8bb8\u591a\u5176\u4ed6\u7528\u5904\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06 […]<\/p>\n","protected":false},"author":309,"featured_media":228616,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-228615","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\/228615","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\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=228615"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228615\/revisions"}],"predecessor-version":[{"id":228627,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/228615\/revisions\/228627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/228616"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=228615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=228615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=228615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}