{"id":631,"date":"2014-07-18T15:30:58","date_gmt":"2014-07-18T15:30:58","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=631"},"modified":"2014-07-18T15:30:58","modified_gmt":"2014-07-18T15:30:58","slug":"awk%e5%ad%a6%e4%b9%a0_1","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/631.html","title":{"rendered":"awk\u5b66\u4e60_1"},"content":{"rendered":"<p>\u56e0\u4e3a\u5728\u7f51\u4e0a\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"http:\/\/heylinux.com\/archives\/3252.html\" target=\"_blank\">\u811a\u672c\u5206\u4eab \u4e4b \u5929\u671d\u5168\u9759\u6001\u8def\u7531 \u00ab Hey! Linux<\/a>\uff0c\u6bd4\u8f83\u611f\u5174\u8da3\u5176\u4e2d\u7684shell\u811a\u672c\uff0c\u6240\u4ee5\u770b\u4e86\u770b\u6e90\u7801\uff0c\u89c9\u5f97\u5176\u4e2d\u7684\u7528awk\u8ba1\u7b97\u5b50\u7f51\u63a9\u7801\u7684\u65b9\u6cd5\u7279\u522b\u597d\uff0c\u5728\u6b64\u8bb0\u5f55\u4e00\u4e0b\uff08\u4e0b\u9762\u622a\u53d6\u7684\u662f\u6211\u6bd4\u8f83\u611f\u5174\u8da3\u7684\u4ee3\u7801\u7247\u6bb5\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#!\/bin\/bash\n\nbasedir=$(dirname $0)\nwget http:\/\/ftp.apnic.net\/apnic\/stats\/apnic\/delegated-apnic-latest\nconf=\"${basedir}\/delegated-apnic-latest\"\n\nfunction check_conf(){\n  if [ ! -f ${conf} ]; then\n    echo \"No such file: ${conf}\"\n    echo \"Please download it from http:\/\/ftp.apnic.net\/apnic\/stats\/apnic\/delegated-apnic-latest\"\n    exit 1\n  fi\n}\n\nfunction add_routes(){\n  oldgw=$(netstat -nr | grep '^default' | grep -v 'ppp' | sed 's\/default *([0-9.]*) .*\/1\/' | grep -Ev '^$')\n  #dscacheutil -flushcache\n\n  all_subs=$(grep CN ${conf} | grep ipv4 | awk -F '|' '{print $4\"\/\"$5}')\n  echo -n \"Adding the routes...\"\n  for subnet in ${all_subs}\n  do\n    subneti=$(echo ${subnet} | cut -d\/ -f1)\n    rawnetm=$(echo ${subnet} | cut -d\/ -f2)\n    subnetm=$(awk -v c=${rawnetm} 'function log2(x){if(x&lt;2)return(pow);pow--;return(log2(x\/2))}BEGIN{pow=32;print log2(c)}')\n    route add ${subneti}\/${subnetm} \"${oldgw}\" &gt; \/dev\/null\n  done\n  echo \" Done\"\n}<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<p>all_subs=$(grep CN delegated-apnic-latest\u00a0| grep ipv4 | awk -F &#8216;|&#8217; &#8216;{print $4&#8243;\/&#8221;$5}&#8217;)<br \/>\nfor subnet in ${all_subs}<br \/>\ndo<br \/>\nsubneti=$(echo ${subnet} | cut -d\/ -f1)<br \/>\nrawnetm=$(echo ${subnet} | cut -d\/ -f2)<br \/>\nsubnetm=$(awk <span style=\"color: #ff0000;\">-v c=${rawnetm}<\/span> &#8216;<strong><span style=\"color: #0000ff;\">function log2(x){if(x&lt;2)return(pow);pow&#8211;;return(log2(x\/2))}<\/span><\/strong><span style=\"color: #ff0000;\">BEGIN{pow=32;print log2(c)}<\/span>&#8216;)<br \/>\ndone<br \/>\necho &#8221; Done&#8221;<\/p>\n<p>\u56e0\u4e3adelegated-apnic-latest\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7684\u7b2c5\u5217\u4e3a\u5212\u5206\u7ed9\u7684IP\u5730\u5740\u5757\uff0c\u7b2c6\u5217\u4e3a\u5730\u5740\u5757\u4e2d\u7684\u4e2a\u6570\uff0c\u7531\u6b64\u8ba1\u7b97\u5bf9\u5e94\u7684\u5b50\u7f51\u63a9\u7801\uff0c\u8fd9\u91cc\u7528\u7684awk\u65b9\u6cd5\u5bf9\u4e8e\u6211\u6765\u8bf4\u611f\u89c9\u975e\u5e38\u60ca\u8273\uff01<\/p>\n<hr \/>\n<p>\u8fd8\u6709\u5c31\u662fdirname\u547d\u4ee4\u7684\u5b66\u4e60\uff08\u793a\u4f8b\u8bf4\u660e\u4e86\u4e00\u5207\uff09\uff1a<\/p>\n<p>NAME<br \/>\ndirname &#8211; strip last component from file name<\/p>\n<p>SYNOPSIS<br \/>\ndirname NAME<br \/>\ndirname OPTION<\/p>\n<p>DESCRIPTION<br \/>\nOutput NAME with its last non-slash component and trailing slashes removed; if NAME contains no \/&#8217;s, output `.&#8217; (meaning the current directory).<\/p>\n<p>&#8211;help display this help and exit<\/p>\n<p>&#8211;version<br \/>\noutput version information and exit<\/p>\n<h5><span style=\"color: #ff0000;\">EXAMPLES<\/span><\/h5>\n<p>dirname \/usr\/bin\/<br \/>\nOutput &#8220;\/usr&#8221;<\/p>\n<p>dirname stdio.h<br \/>\nOutput &#8220;.&#8221;<\/p>\n<p>SEE ALSO<br \/>\nbasename(1), readlink(1)<\/p>\n<p>GNU coreutils 8.12.197-032bb September 2011 DIRNAME(1)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56e0\u4e3a\u5728\u7f51\u4e0a\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff1a\u811a\u672c\u5206\u4eab \u4e4b \u5929\u671d\u5168\u9759\u6001\u8def\u7531 \u00ab Hey! Linux\uff0c\u6bd4\u8f83\u611f\u5174\u8da3\u5176\u4e2d\u7684shell\u811a\u672c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[74,244],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-linux","tag-awk","tag-dirname"],"views":2181,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}