{"id":140250,"date":"2019-03-27T09:25:42","date_gmt":"2019-03-27T01:25:42","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=140250"},"modified":"2019-03-25T09:27:10","modified_gmt":"2019-03-25T01:27:10","slug":"linux-awk-clever","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-awk-clever.html","title":{"rendered":"Linux\uff1a\u201cawk\u201d\u547d\u4ee4\u7684\u5999\u7528"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nawk\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\uff0c\u7b80\u5355\u6765\u8bf4awk\u5c31\u662f\u628a\u6587\u4ef6\u9010\u884c\u8bfb\u5165\uff0c\uff08\u7a7a\u683c\uff0c\u5236\u8868\u7b26\uff09\u4e3a\u9ed8\u8ba4\u5206\u9694\u7b26\u5c06\u6bcf\u884c\u5207\u7247\uff0c\u5207\u5f00\u7684\u90e8\u5206\u518d\u8fdb\u884c\u5404\u79cd\u5206\u6790\u5904\u7406\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
0\u3001\u57fa\u672c\u7528\u6cd5<\/strong><\/div>\n

awk\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\uff0c\u7b80\u5355\u6765\u8bf4awk\u5c31\u662f\u628a\u6587\u4ef6\u9010\u884c\u8bfb\u5165\uff0c\uff08\u7a7a\u683c\uff0c\u5236\u8868\u7b26\uff09\u4e3a\u9ed8\u8ba4\u5206\u9694\u7b26\u5c06\u6bcf\u884c\u5207\u7247\uff0c\u5207\u5f00\u7684\u90e8\u5206\u518d\u8fdb\u884c\u5404\u79cd\u5206\u6790\u5904\u7406
\nawk\u547d\u4ee4\u683c\u5f0f\u5982\u4e0b<\/p>\n

awk [-F field-separator] 'commands' input-file(s)<\/pre>\n

[-F \u5206\u9694\u7b26]\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3aawk\u4f7f\u7528\u7a7a\u683c\uff0c\u5236\u8868\u7b26\u4f5c\u4e3a\u7f3a\u7701\u7684\u5b57\u6bb5\u5206\u9694\u7b26\uff0c\u56e0\u6b64\u5982\u679c\u8981\u6d4f\u89c8\u5b57\u6bb5\u95f4\u6709\u7a7a\u683c\uff0c\u5236\u8868\u7b26\u7684\u6587\u672c\uff0c\u4e0d\u5fc5\u6307\u5b9a\u8fd9\u4e2a\u9009\u9879\uff0c\u4f46\u5982\u679c\u8981\u6d4f\u89c8\u8bf8\u5982\/etc\/passwd\u6587\u4ef6\uff0c\u6b64\u6587\u4ef6\u5404\u5b57\u6bb5\u4ee5\u5192\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5219\u5fc5\u987b\u6307\u660e-F\u9009\u9879<\/p>\n

echo \"this is a test\" | awk '{ print $0 }'  \r\n## \u8f93\u51fa\u4e3a  \r\nthis is a test<\/pre>\n

shell<\/code>\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53d1\u73b0|\uff0c\u4ee3\u8868\u6709\u7ba1\u9053\u3002|\u5de6\u53f3\u88ab\u7406\u89e3\u4e3a\u7b80\u5355\u547d\u4ee4\uff0c\u5373\u524d\u4e00\u4e2a\uff08\u5de6\u8fb9\uff09\u7b80\u5355\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u6307\u5411\u540e\u4e00\u4e2a\uff08\u53f3\u8fb9\uff09\u6807\u51c6\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u5165
\nawk<\/code>\u4f1a\u6839\u636e\u5206\u9694\u7b26\u5c06\u884c\u5206\u6210\u82e5\u5e72\u4e2a\u5b57\u6bb5\uff0c$0\u4e3a\u6574\u884c\uff0c$1\u4e3a\u7b2c\u4e00\u4e2a\u5b57\u6bb5\uff0c$2 \u4e3a\u7b2c2\u4e2a\u5730\u6bb5\uff0c\u4f9d\u6b64\u7c7b\u63a8\u2026
\n\u4e3a\u6253\u5370\u4e00\u4e2a\u5b57\u6bb5\u6216\u6240\u6709\u5b57\u6bb5\uff0c\u4f7f\u7528print\u547d\u4ee4\u3002\u8fd9\u662f\u4e00\u4e2aawk<\/code>\u52a8\u4f5c<\/p>\n

echo \"this is a test\" | awk '{ print $1 }'  \r\n## \u8f93\u51fa\u4e3a  \r\nthis  \r\necho \"this is a test\" | awk '{ print $1, $2 }'  \r\n## \u8f93\u51fa\u4e3a  \r\nthis is<\/pre>\n

\/etc\/passwd<\/code>\u7684\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b<\/p>\n

root:x:0:0:root:\/root:\/bin\/bash  \r\nbin:x:1:1:bin:\/bin:\/sbin\/nologin  \r\ndaemon:x:2:2:daemon:\/sbin:\/sbin\/nologin  \r\nadm:x:3:4:adm:\/var\/adm:\/sbin\/nologin  \r\nlp:x:4:7:lp:\/var\/spool\/lpd:\/sbin\/nologin<\/pre>\n
\u4e3e\u51e0\u4e2a\u7b80\u5355\u7684\u5c0f\u9700\u6c42<\/strong><\/div>\n
1\u3001\u53ea\u663e\u793a\/etc\/passwd\u7684\u8d26\u6237<\/strong><\/span><\/div>\n
awk -F : '{ print $1 }' \/etc\/passwd  \r\n## \u8f93\u51fa\u4e3a  \r\nroot  \r\nbin  \r\ndaemon  \r\nadm  \r\nlp<\/pre>\n

\"\"<\/p>\n

2\u3001\u663e\u793a\/etc\/passwd\u7684\u7b2c1\u5217\u548c\u7b2c7\u5217\uff0c\u7528\u9017\u53f7\u5206\u9694\u663e\u793a\uff0c\u6240\u6709\u884c\u5f00\u59cb\u524d\u6dfb\u52a0\u5217\u540dstart1\uff0cstart7\uff0c\u6700\u540e\u4e00\u884c\u6dfb\u52a0\uff0cend1\uff0cend7<\/strong><\/span><\/div>\n
awk -F ':' 'BEGIN {print \"start1,start7\"} {print $1 \",\" $7} END {print \"end1,end7\"}' \/etc\/passwd  \r\n## \u8f93\u51fa\u4e3a  \r\nstart1,start7  \r\nroot,\/bin\/bash  \r\nbin,\/sbin\/nologin  \r\ndaemon,\/sbin\/nologin  \r\nadm,\/sbin\/nologin  \r\nlp,\/sbin\/nologin  \r\nend1,end7<\/pre>\n

BEGIN\u8bed\u53e5\u5728\u6240\u6709\u6587\u672c\u5904\u7406\u52a8\u4f5c\u6267\u884c\u4e4b\u524d\u88ab\u6267\u884c\uff0cEND\u5728\u6240\u6709\u6587\u672c\u5904\u7406\u52a8\u4f5c\u6267\u884c\u4e4b\u540e\u88ab\u6267\u884c<\/p>\n

3\u3001\u7edf\u8ba1\/etc\/passwd\u6587\u4ef6\u4e2d\uff0c\u6bcf\u884c\u7684\u884c\u53f7\uff0c\u6bcf\u884c\u7684\u5217\u6570\uff0c\u5bf9\u5e94\u7684\u5b8c\u6574\u884c\u5185\u5bb9<\/strong><\/span><\/div>\n
awk -F : '{ print NR \"  \" NF \"  \" $0 }' \/etc\/passwd  \r\n## \u8f93\u51fa\u4e3a  \r\n1    7   root:x:0:0:root:\/root:\/bin\/bash  \r\n2    7   bin:x:1:1:bin:\/bin:\/sbin\/nologin  \r\n3    7   daemon:x:2:2:daemon:\/sbin:\/sbin\/nologin  \r\n4    7   adm:x:3:4:adm:\/var\/adm:\/sbin\/nologin  \r\n5    7   lp:x:4:7:lp:\/var\/spool\/lpd:\/sbin\/nologin<\/pre>\n
1\u3001\u652f\u6301\u5185\u7f6e\u53d8\u91cf<\/strong><\/span><\/div>\n

\u4e0a\u9762\u793a\u4f8b\u4e2dNR<\/code>\uff0c\u548cNF<\/code>\u5176\u5b9e\u5c31\u662fawk<\/code>\u7684\u5185\u7f6e\u53d8\u91cf\uff0c\u4e00\u4e9b\u5185\u7f6e\u53d8\u91cf\u5982\u4e0b<\/p>\n

\u53d8\u91cf\u540d \u89e3\u91ca\r\nFILENAMEawk\u6d4f\u89c8\u7684\u6587\u4ef6\u540d\r\nFS\u8bbe\u7f6e\u8f93\u5165\u5b57\u6bb5\u5206\u9694\u7b26\uff0c\u7b49\u4ef7\u4e8e\u547d\u4ee4\u884c-F\u9009\u9879\r\nNF \u6d4f\u89c8\u8bb0\u5f55\u7684\u5b57\u6bb5\u4e2a\u6570\r\nNR \u5df2\u8bfb\u7684\u8bb0\u5f55\u6570<\/pre>\n
2\u3001\u652f\u6301\u51fd\u6570<\/strong><\/span><\/div>\n

\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/p>\n

awk 'BEGIN { print length(\"this is a text\") }'<\/pre>\n

## \u8f93\u51fa\u4e3a
\n14
\n\u5c06\/etc\/passwd<\/code>\u7684\u7528\u6237\u540d\u53d8\u6210\u5927\u5199\u8f93\u51fa<\/p>\n

awk -F ':' '{ print toupper($1) }' \/etc\/passwd<\/pre>\n

## \u8f93\u51fa\u4e3a<\/p>\n

ROOT  \r\nBIN  \r\nDAEMON  \r\nADM  \r\nLP<\/pre>\n

\u5e38\u7528\u51fd\u6570\u5982\u4e0b<\/p>\n

\u51fd\u6570\u540d \u4f5c\u7528\r\ntoupper(s)\u8fd4\u56des\u7684\u5927\u5199\r\ntolower(s) \u8fd4\u56des\u7684\u5c0f\u5199\r\nlength(s) \u8fd4\u56des\u957f\u5ea6\r\nsubstr(s,p) \u8fd4\u56de\u5b57\u7b26\u4e32s\u4e2d\u4ecep\u5f00\u59cb\u7684\u540e\u7f00\u90e8\u5206<\/pre>\n
3\u3001\u652f\u6301\u6761\u4ef6\u64cd\u4f5c\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/strong><\/span><\/div>\n

\u663e\u793a\/etc\/passwd\u4e2d\u6709daemon\u7684\u884c<\/p>\n

awk -F ':' '$0 ~ \/daemon\/' \/etc\/passwd
\n## \u8f93\u51fa\u4e3a<\/p>\n

daemon:x:2:2:daemon:\/sbin:\/sbin\/nologin \r\nawk\u6761\u4ef6\u64cd\u4f5c\u7b26\r\n\r\n\u64cd\u4f5c\u7b26\t\u63cf\u8ff0\r\n< <\/code> \u5c0f\u4e8e < =<\/code> \u5c0f\u4e8e\u7b49\u4e8e ==<\/code> \u7b49\u4e8e !=<\/code> \u4e0d\u7b49\u4e8e ~<\/code> \u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f !~<\/code> \u4e0d\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f 
4\u3001\u652f\u6301\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\uff0c\u7c7bC\u8bed\u8a00<\/strong><\/span><\/div><\/pre>\n
if while do\/while for break continue<\/pre>\n

\u8f93\u51fa\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5927\u4e8ed\u7684\u884c<\/p>\n

awk -F ':' '{ if ($1 > \"d\") { print $1 } else { print \"-\" } }' \/etc\/passwd<\/pre>\n

## \u8f93\u51fa\u4e3a<\/p>\n

root  \r\n-  \r\ndaemon  \r\n-  \r\nlp<\/pre>\n

\u53ef\u4ee5\u628a\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u653e\u5230\u4e00\u4e2a\u811a\u672c\u4e2d\uff0c\u7136\u540e\u8c03\u7528\u811a\u672c\u6267\u884c\uff0c\u5982test.sh\u7684\u5185\u5bb9\u5982\u4e0b<\/p>\n

{   \r\n    if ($1 > \"d\") {  \r\n        print $1   \r\n    } else {  \r\n        print \"-\"   \r\n    }   \r\n}<\/pre>\n

\u7528\u5982\u4e0b\u65b9\u5f0f\u6267\u884c\uff0c\u6548\u679c\u4e00\u6837<\/p>\n

awk -F ':' -f test.sh \/etc\/passwd<\/pre>\n

## \u8f93\u51fa\u4e3a<\/p>\n

root  \r\n-  \r\ndaemon  \r\n-  \r\nlp<\/pre>\n
5\u3001\u5e94\u7528\u573a\u666f<\/strong><\/span><\/div>\n

\u5c0f\u7f16\u7528awk\u8fdb\u884c\u6587\u672c\u5206\u6790\u6bd4\u8f83\u5c11\uff0c\u4e3b\u8981\u7528\u6765\u5199\u811a\u672c
\n\u5982\u4e00\u4e2aweibo-interface-1.0.jar\u5e94\u7528\uff0c\u542f\u52a8\u811a\u672c\u5982\u4e0b<\/p>\n

start.sh\r\nnohup java -jar weibo-interface-1.0.jar >out 2>&1 &<\/pre>\n

\u5173\u95ed\u811a\u672c\u5982\u4e0b\uff0ckill.sh<\/code><\/p>\n

kill -9 `jps -l | grep 'weibo-interface-1.0.jar' | awk '{print $1}'`<\/pre>\n

jps -l\u7684\u8f93\u51fa\u5982\u4e0b<\/p>\n

70208 com.st.kmp.main.KmpService  \r\n31036 com.st.cis.main.BaiduAnalysisService  \r\n66813 weibo-interface-1.0.jar<\/pre>\n

\u8fd8\u6709\u5c31\u662f\u5173\u95edhadoop\u96c6\u7fa4\u7684\u6240\u6709DataNode\u8282\u70b9\uff08\u4e0d\u77e5\u9053hadoop\u7684\u53ef\u4ee5\u8ba4\u4e3aDataNode\u662f\u4e00\u4e2a\u96c6\u7fa4\u5e94\u7528\uff09\uff0c\u5047\u5982\u4e00\u4e2a\u4e2a\u673a\u5668jps\uff0c\u67e5\u770bpid\uff0ckill\u3002\u5f88\u9ebb\u70e6\uff0c\u76f4\u63a5\u5199\u4e86\u4e00\u4e2a\u811a\u672c\uff0c\u4f9d\u6b21ssh\u5230\u5404\u4e2a\u8282\u70b9\uff0c\u7136\u540e\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u5373\u53ef<\/p>\n

kill `jps | grep 'DataNode' | awk '{print $1}'`<\/pre>\n

jps\u7684\u8f93\u51fa\u4e3a<\/p>\n

508 DataNode  \r\n31481 JournalNode  \r\n31973 NodeManager<\/pre>\n","protected":false},"excerpt":{"rendered":"

awk\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5206\u6790\u5de5\u5177\uff0c\u7b80\u5355\u6765\u8bf4awk\u5c31\u662f\u628a\u6587\u4ef6\u9010\u884c\u8bfb\u5165\uff0c\uff08\u7a7a\u683c\uff0c\u5236\u8868\u7b26\uff09\u4e3a\u9ed8\u8ba4\u5206\u9694\u7b26\u5c06\u6bcf\u884c\u5207\u7247\uff0c\u5207\u5f00 […]<\/p>\n","protected":false},"author":1893,"featured_media":140253,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-140250","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\/140250","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\/1893"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=140250"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/140250\/revisions"}],"predecessor-version":[{"id":140657,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/140250\/revisions\/140657"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/140253"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=140250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=140250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=140250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}