{"id":841,"date":"2012-11-30T18:55:05","date_gmt":"2012-11-30T09:55:05","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=841"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"awk%e3%81%a7%e3%83%ad%e3%82%b0%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%a4%87%e6%95%b0%e3%81%ab%e5%88%86%e5%89%b2%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2012\/11\/30\/awk%e3%81%a7%e3%83%ad%e3%82%b0%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%a4%87%e6%95%b0%e3%81%ab%e5%88%86%e5%89%b2%e3%81%99%e3%82%8b\/","title":{"rendered":"awk \u3067\u30ed\u30b0\u30d5\u30a1\u30a4\u30eb\u3092\u8907\u6570\u306b\u5206\u5272\u3059\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>\u4eca\u65e5\u3082\u30ed\u30b0\u30cd\u30bf\u3067\u3059\u30021\u9031\u9593\u6bce\u306b\u4fdd\u5b58\u3055\u308c\u305f\u30ed\u30b0\u3092\u65e5\u3054\u3068\u306b\u5206\u3051\u305f\u3044\u306a\u3068\u601d\u3063\u305f\u306e\u3067\u3001awk\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u30ed\u30b0\u30d5\u30a1\u30a4\u30eb\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002\u308f\u304b\u308a\u3084\u3059\u304f\u3059\u308b\u305f\u30811\u65e5\u8fba\u308a1\u884c\u306e\u30ed\u30b0\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat weekly.log\r\n192.168.1.100 - - [19\/Nov\/2012:03:22:36 +0900] \"GET \/ HTTP\/1.1\" 301 178 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.000\r\n192.168.1.100 - - [20\/Nov\/2012:03:22:41 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.006\r\n192.168.1.100 - - [21\/Nov\/2012:03:22:41 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n192.168.1.100 - - [22\/Nov\/2012:03:23:31 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n192.168.1.100 - - [23\/Nov\/2012:03:25:15 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n192.168.1.100 - - [24\/Nov\/2012:03:25:15 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n192.168.1.100 - - [25\/Nov\/2012:03:25:15 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003<\/pre>\n<p>\u3053\u308c\u3092\u3001yyyy-mm-dd.log \u3068\u3057\u30667\u65e5\u5206\u306b\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u65e5\u4ed8\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u307e\u305a\u306f\u3001\u65e5\u4ed8\u90e8\u5206\u3092\u53d6\u5f97\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002awk\u306e\u52d5\u4f5c\u306f\u3001awk\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u306b\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat split_by_date\r\n#!\/usr\/bin\/awk -f\r\n{\r\n    date = substr($4, 2, 11)\r\n    print date\r\n}\r\n\r\n$ cat weekly.log | .\/split_by_date\r\n19\/Nov\/2012\r\n20\/Nov\/2012\r\n21\/Nov\/2012\r\n22\/Nov\/2012\r\n23\/Nov\/2012\r\n24\/Nov\/2012\r\n25\/Nov\/2012<\/pre>\n<h2>\u65e5\u4ed8\u3092\u5909\u63db\u3059\u308b<\/h2>\n<p>\u3061\u3083\u3093\u3068\u65e5\u4ed8\u304c\u53d6\u5f97\u3067\u304d\u307e\u3057\u305f\u3002\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u304c\u7570\u306a\u308b\u306e\u3067\u5909\u63db\u3057\u3066\u307f\u307e\u3059\u3002\u3068\u308a\u3042\u3048\u305a\u6708\u306e\u8868\u793a\u304c\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u3067\u3059\u304c\u3001\u305d\u3053\u306f\u3044\u3063\u305f\u3093\u305d\u306e\u307e\u307e\u3067\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat split_by_date\r\n#!\/usr\/bin\/awk -f\r\n{\r\n    date = substr($4, 2, 11)\r\n    split(date, params, \"\/\")\r\n    formatted_date = params[3] \"-\" params[2] \"-\" params[1]\r\n    print formatted_date\r\n}\r\n$ cat weekly.log | .\/split_by_date\r\n2012-Nov-19\r\n2012-Nov-20\r\n2012-Nov-21\r\n2012-Nov-22\r\n2012-Nov-23\r\n2012-Nov-24\r\n2012-Nov-25<\/pre>\n<p>\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u3060\u3051\u306b\u9650\u308c\u3070\u3001Nov\u309211\u306b\u3059\u308b\u3060\u3051\u3067\u89e3\u6c7a\u3059\u308b\u3093\u3067\u3059\u304c\u3001\u3061\u3083\u3093\u3068\u5bfe\u5fdc\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat split_by_date\r\n#!\/usr\/bin\/awk -f\r\nfunction num_month(alpha_month) {\r\n    return index(\"__,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec\", alpha_month) \/ 4\r\n}\r\n{\r\n    date = substr($4, 2, 11)\r\n    split(date, params, \"\/\")\r\n    formatted_date = params[3] \"-\" num_month(params[2]) \"-\" params[1]\r\n    print formatted_date\r\n}\r\n\r\n$ cat weekly.log | .\/split_by_date\r\n2012-11-19\r\n2012-11-20\r\n2012-11-21\r\n2012-11-22\r\n2012-11-23\r\n2012-11-24\r\n2012-11-25<\/pre>\n<p>\u3061\u3083\u3093\u3068\u5909\u63db\u3067\u304d\u307e\u3057\u305f\uff01\u95a2\u6570\u306e\u4ed5\u7d44\u307f\u306f\u3001\u9023\u7d50\u3057\u305f\u6587\u5b57\u5217\u304b\u3089index\u3067\u4f4d\u7f6e\u3092\u53d6\u5f97\u3057\u3001\u305d\u308c\u30924\u3067\u5272\u3063\u3066\u3044\u307e\u3059\u3002\u6700\u521d\u306b\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2\u3092\u4ed8\u3051\u3066\u3001\u7d50\u679c\u306e\u6570\u5b57\u304c1\uff5e12\u306b\u306a\u308b\u3088\u3046\u8abf\u6574\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u3082\u3046\u5c11\u3057\u3060\u3051\u8abf\u6574\u304c\u5fc5\u8981\u3067\u30011\uff5e9\u6708\u307e\u3067\u306f1\u6841\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u30012\u6841\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002sprintf\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat split_by_date\r\n#!\/usr\/bin\/awk -f\r\nfunction num_month(alpha_month) {\r\n    return index(\"__,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec\", alpha_month) \/ 4\r\n}\r\n{\r\n    date = substr($4, 2, 11)\r\n    split(date, params, \"\/\")\r\n    month = sprintf(\"%02d\", num_month(params[2]))\r\n    formatted_date = params[3] \"-\" month \"-\" params[1]\r\n    print formatted_date\r\n}<\/pre>\n<h2>\u500b\u5225\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b<\/h2>\n<p>\u3055\u3066\u3001\u65e5\u4ed8\u304c\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u306e\u3067\u3001\u5f8c\u306f\u30ed\u30b0\u3092\u65e5\u4ed8\u5225\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u3060\u3051\u3067\u3059\u3002awk\u3067\u3082\u3001\u30b7\u30a7\u30eb\u3068\u540c\u3058\u3088\u3046\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u304c\u4f7f\u3048\u308b\u306e\u3067\u3001\u53d7\u3051\u53d6\u3063\u305f\u884c\u3092\u5168\u90e8\u65e5\u4ed8\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u6d41\u3057\u8fbc\u3080\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat split_by_date\r\n#!\/usr\/bin\/awk -f\r\nfunction num_month(alpha_month) {\r\n    return index(\"__,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec\", alpha_month) \/ 4\r\n}\r\n{\r\n    date = substr($4, 2, 11)\r\n    split(date, params, \"\/\")\r\n    month = sprintf(\"%02d\", num_month(params[2]))\r\n    filename = params[3] \"-\" month \"-\" params[1]\r\n    print $0 &gt;&gt; filename \".log\"\r\n}\r\n\r\n$ cat weekly.log | .\/split_by_date<\/pre>\n<p>\u3061\u3083\u3093\u3068\u30ed\u30b0\u304c\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\u305f\u305f\u3081\u3001\u4eca\u307e\u3067\u3068\u7570\u306a\u308a\u4f55\u3082\u51fa\u529b\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ ls -1\r\n2012-11-19.log\r\n2012-11-20.log\r\n2012-11-21.log\r\n2012-11-22.log\r\n2012-11-23.log\r\n2012-11-24.log\r\n2012-11-25.log\r\nsplit_by_date\r\nweekly.log\r\n\r\n$ for file in 2012*.log; do echo $file; cat $file; echo ; done\r\n2012-11-19.log\r\n192.168.1.100 - - [19\/Nov\/2012:03:22:36 +0900] \"GET \/ HTTP\/1.1\" 301 178 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.000\r\n\r\n2012-11-20.log\r\n192.168.1.100 - - [20\/Nov\/2012:03:22:41 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.006\r\n\r\n2012-11-21.log\r\n192.168.1.100 - - [21\/Nov\/2012:03:22:41 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n\r\n2012-11-22.log\r\n192.168.1.100 - - [22\/Nov\/2012:03:23:31 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n\r\n2012-11-23.log\r\n192.168.1.100 - - [23\/Nov\/2012:03:25:15 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n\r\n2012-11-24.log\r\n192.168.1.100 - - [24\/Nov\/2012:03:25:15 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003\r\n\r\n2012-11-25.log\r\n192.168.1.100 - - [25\/Nov\/2012:03:25:15 +0900] \"GET \/ HTTP\/1.1\" 200 26994 \"-\" \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\" \"-\" 0.003<\/pre>\n<p>\u3061\u3083\u3093\u3068\u5206\u304b\u308c\u3066\u4fdd\u5b58\u3055\u308c\u307e\u3057\u305f\uff01<\/p>\n<p>linux\u3067\u4f7f\u3048\u308b\u30b3\u30de\u30f3\u30c9\u3092\u30d1\u30a4\u30d7\u3068\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3067\u7e4b\u3050\u3060\u3051\u3067\u306f\u3001\u7570\u306a\u308b\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3059\u308b\u306e\u306f\u3061\u3087\u3063\u3068\u5927\u5909\u3067\u3059\u3002\u304b\u3068\u3044\u3063\u3066\u3001Python\u3084Ruby\u306a\u3069LL\u3092\u4f7f\u3046\u307b\u3069\u3067\u306f\u2026\u3068\u3044\u3046\u6642\u306b\u306fawk\u304c\u4f7f\u3048\u308b\u3068\u601d\u3044\u307e\u3059\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>\u4eca\u65e5\u3082\u30ed\u30b0\u30cd\u30bf\u3067\u3059\u30021\u9031\u9593\u6bce\u306b\u4fdd\u5b58\u3055\u308c\u305f\u30ed\u30b0\u3092\u65e5\u3054\u3068\u306b\u5206\u3051\u305f\u3044\u306a\u3068\u601d\u3063\u305f\u306e\u3067\u3001awk\u3067\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u30ed\u30b0\u30d5\u30a1\u30a4\u30eb\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002\u308f\u304b\u308a\u3084\u3059\u304f\u3059\u308b\u305f\u30811\u65e5\u8fba\u308a1\u884c\u306e\u30ed\u30b0\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/11\/30\/awk%e3%81%a7%e3%83%ad%e3%82%b0%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%a4%87%e6%95%b0%e3%81%ab%e5%88%86%e5%89%b2%e3%81%99%e3%82%8b\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[54,23,58],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/841"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"predecessor-version":[{"id":18012,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/841\/revisions\/18012"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}