{"id":742,"date":"2012-11-28T16:40:50","date_gmt":"2012-11-28T07:40:50","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=742"},"modified":"2018-11-14T16:33:59","modified_gmt":"2018-11-14T07:33:59","slug":"grep%e3%81%ae%e3%82%aa%e3%83%97%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%8a%e3%81%95%e3%82%89%e3%81%84","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2012\/11\/28\/grep%e3%81%ae%e3%82%aa%e3%83%97%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%8a%e3%81%95%e3%82%89%e3%81%84\/","title":{"rendered":"grep\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u304a\u3055\u3089\u3044"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>\u30ed\u30b0\u8abf\u67fb\u306e\u6642\u306b\u306fgrep\u4f7f\u3044\u307e\u3059\u3088\u306d\uff01\u3067\u3082\u826f\u304f\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u304a\u3055\u3089\u3044\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h2>\u30d5\u30a1\u30a4\u30eb\u306e\u6271\u3044<\/h2>\n<p>\u57fa\u672c\u306e\u6587\u6cd5\u3068\u3057\u3066\u306f\u3001<\/p>\n<pre class=\"lang:sh decode:true\">grep \u30aa\u30d7\u30b7\u30e7\u30f3 \u6761\u4ef6 \u30d5\u30a1\u30a4\u30eb<\/pre>\n<p>\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u306e\u6307\u5b9a\u304c\u3055\u308c\u306a\u3044\u5834\u5408\u306f\u6a19\u6e96\u5165\u529b\u3092\u5bfe\u8c61\u306b\u3057\u307e\u3059\u306e\u3067\u3001<\/p>\n<pre class=\"lang:sh decode:true\">cat access.log | grep \"GET \/ \"<\/pre>\n<p>\u3068\u3044\u3046\u4f7f\u3044\u65b9\u3092\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u5236\u5fa1<\/h3>\n<p>\u30d5\u30a1\u30a4\u30eb\u3092\u8907\u6570\u6307\u5b9a\u3057\u305f\u3068\u304d\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u5148\u982d\u306b\u51fa\u529b\u3055\u308c\u308b\u306e\u304c\u90aa\u9b54\u306a\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002\u305d\u3093\u306a\u6642\u306b\u306f -h \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u3064\u3051\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -h \"GET \/ \" *.log<\/pre>\n<p>\u6a19\u6e96\u5165\u529b\u304b\u3089\u53d7\u3051\u53d6\u3063\u305f\u5834\u5408\u306f\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u3042\u308a\u307e\u305b\u3093\u306e\u3067\u3001-H, --label\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">cat *.log | grep --label=log -H \"GET \/ \"<\/pre>\n<h2>\u51fa\u529b\u5185\u5bb9\u3092\u5909\u66f4\u3059\u308b<\/h2>\n<h3>\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u51fa\u529b<\/h3>\n<p>\u30de\u30c3\u30c1\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u304c\u5206\u304b\u308c\u3070\u826f\u3044\uff08\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u307b\u3057\u3044\uff09\u3068\u3044\u3046\u6642\u306b\u306f\u3001-l\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -l \"GET \/ \" *.log<\/pre>\n<p>\u9006\u306b\u4e00\u81f4\u3057\u306a\u3044\u30d5\u30a1\u30a4\u30eb\u304c\u6b32\u3057\u3044\u3068\u304d\u3001-L \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -L \"GET \/ \" *.log<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001l, L \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u521d\u56de\u306e\u4e00\u81f4\u3067\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\u3092\u505c\u6b62\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u8ce2\u3044\u3067\u3059\u306d\u3002<\/p>\n<p>\u30d5\u30a1\u30a4\u30eb\u5185\u3067\u306e\u4e00\u81f4\u56de\u6570\u304c\u6b32\u3057\u3044\u3068\u304d\u306f\u3001-c \u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u540d\u3068\u56de\u6570\u306e\u307f\u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -c \"GET \/ \" *.log<\/pre>\n<h3>\u691c\u7d22\u7d50\u679c\u306e\u51fa\u529b<\/h3>\n<p>\u4e00\u81f4\u3057\u305f\u5834\u6240\u306b\u8272\u3065\u3051\u3059\u308b\u306b\u306f\u3001--color \u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059<\/p>\n<pre class=\"lang:sh decode:true\">grep --color=auto \"GET \/ \" *.log<\/pre>\n<p>\u4e00\u81f4\u3057\u305f\u56de\u6570\u3092\u5236\u5fa1\u3059\u308b\u306b\u306f -m \u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -m 1 \"GET \/ \" *.log<\/pre>\n<p>\u4e00\u81f4\u3057\u305f\u524d\u5f8c\u306e\u884c\u3092\u51fa\u529b\u3059\u308b\u306b\u306f\u3001-A, -B, -C \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<br \/>\n-A: after (\u4e00\u81f4\u5f8c\uff09<br \/>\n-B: before \uff08\u4e00\u81f4\u524d\uff09<br \/>\n-C: context \uff08\u4e00\u81f4\u524d\u5f8c\uff09<\/p>\n<pre class=\"lang:sh decode:true\">grep -A 2 \"GET \/ \" *.log\r\ngrep -B 2 \"GET \/ \" *.log\r\ngrep -C 2 \"GET \/ \" *.log<\/pre>\n<p><code>-C 2<\/code> \u306f <code>-A 2 -B 2<\/code>\u3068\u540c\u3058\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u884c\u6570\u304c\u6b32\u3057\u3044\u5834\u5408\u306b\u306f\u3001-n \u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -n \"GET \/ \" *.log<\/pre>\n<h2>\u30d1\u30bf\u30fc\u30f3\u306b\u3064\u3044\u3066<\/h2>\n<h3>\u6b63\u898f\u8868\u73fe\u306e\u7a2e\u985e<\/h3>\n<p>-G\u30aa\u30d7\u30b7\u30e7\u30f3\uff08\u30c7\u30d5\u30a9\u30eb\u30c8\uff09\u3092\u4f7f\u3046\u3068\u3001\u57fa\u672c\u7684\u306a\u6b63\u898f\u8868\u73fe\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001-E \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3046\u3068\u62e1\u5f35\u3055\u308c\u305f\u6b63\u898f\u8868\u73fe\u304c\u4f7f\u3048\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u8a00\u3046\u3068\u3001<\/p>\n<p><code>? + { | ( )<\/code><\/p>\n<p>\u306e\u7279\u6b8a\u8a18\u53f7\u304c\u305d\u306e\u307e\u307e\u4f7f\u3048\u307e\u3059\u3002\u57fa\u672c\u6b63\u898f\u8868\u73fe\u3060\u3068\u3001\u30d0\u30c3\u30af\u30b9\u30e9\u30c3\u30b7\u30e5\u3067\u30a8\u30b9\u30b1\u30fc\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -E \"GET .*\\.(js|css)\" *.log\r\ngrep -G \"GET .*\\.\\(js\\|css\\)\" *.log<\/pre>\n<p>\u3053\u306e\uff12\u3064\u306f\u540c\u3058\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002\u6b63\u898f\u8868\u73fe\u3092\u4f7f\u3063\u3066\u63a2\u3059\u3068\u304d\u306b\u306f\u3001\u30a8\u30b9\u30b1\u30fc\u30d7\u304c\u9762\u5012\u306a\u306e\u3067\u3001-E \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3046\u3088\u3046\u306b\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u826f\u3044\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u306d\u3002<\/p>\n<p>\u3055\u3089\u306b\u3001-P \u30aa\u30d7\u30b7\u30e7\u30f3\u3067Perl\u4e92\u63db\u306e\u6b63\u898f\u8868\u73fe(PCRE)\u304c\u4f7f\u3048\u307e\u3059\u3002\\w\u3084\\d\u3068\u3044\u3063\u305f\u8868\u73fe\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -P \"HEAD \\S+ HTTP\" *.log<\/pre>\n<p>\u4f8b\u3048\u3070HEAD\u306e\u30a2\u30af\u30bb\u30b9\u306e\u307f\u62bd\u51fa\u3059\u308b\u3068\u304b\u3002<\/p>\n<p>\u307e\u305f\u3001\u6b63\u898f\u8868\u73fe\u3067\u306f\u306a\u304f\u56fa\u5b9a\u6587\u5b57\u5217\u3068\u3057\u3066\u6271\u3046\u3001-F \u30aa\u30d7\u30b7\u30e7\u30f3\u3082\u3042\u308a\u307e\u3059\u3002\u6b63\u898f\u8868\u73fe\u3068\u3057\u3066\u6271\u308f\u305b\u305f\u304f\u306a\u3044\u3001\u7279\u6b8a\u6587\u5b57\u304c\u3042\u308b\u5834\u5408\u306b\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -F \"[28\/Nov\" *.log<\/pre>\n<p>-i \u30aa\u30d7\u30b7\u30e7\u30f3\u3082\u3042\u308a\u307e\u3059\u3002\u5927\u6587\u5b57\u5c0f\u6587\u5b57\u3092\u8b58\u5225\u3057\u306a\u3044\uff08ignore case\uff09\u305f\u3081\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -i -e \"get \/ http\" *.log<\/pre>\n<h3>\u30d1\u30bf\u30fc\u30f3\u306eAND<\/h3>\n<p>AND \u3057\u305f\u3044\u3068\u304d\u306f\u3001\u30d1\u30a4\u30d7\u3092\u4f7f\u3063\u3066\u3001\u7d5e\u308a\u8fbc\u3093\u3067\u3044\u304d\u307e\u3059\u3002\u30aa\u30d7\u30b7\u30e7\u30f3\u3060\u3051\u3067\u306f\u5bfe\u5fdc\u3067\u304d\u306a\u3044\u3088\u3046\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep \"GET \/ \" *.log | grep iPhone<\/pre>\n<h3>\u30d1\u30bf\u30fc\u30f3\u306eOR<\/h3>\n<p>\u8907\u6570\u306e\u6761\u4ef6\u3067 OR \u3057\u305f\u3044\u6642\u306f\u3001\u6761\u4ef6\u3092\u8907\u6570\u8a18\u8ff0\u3057\u307e\u3059\u3002\u305d\u306e\u969b\u306b\u3001-e \u3092\u4f7f\u308f\u306a\u3044\u3068\u30d5\u30a1\u30a4\u30eb\u540d\u3068\u6271\u308f\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002\u666e\u6bb5\u306f\u7701\u7565\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u591a\u3044\u304b\u3082\u77e5\u308c\u307e\u305b\u3093\u304c\u3001-e\u306e\u5f8c\u306b\u8a18\u8ff0\u3055\u308c\u305f\u3082\u306e\u304c\u30d1\u30bf\u30fc\u30f3\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -e \"GET .*\\.js\" -e \"GET .*\\.css\" *.log<\/pre>\n<p>\u5916\u90e8\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u3063\u3066\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u6761\u4ef6\u3092\u8a2d\u5b9a\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ cat regex.txt\r\nGET .*\\.js\r\nGET .*\\.css<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u969b\u306b\u306f\u3001-f \u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -f regex.txt *.log<\/pre>\n<h3>\u30d1\u30bf\u30fc\u30f3\u306e\u9664\u5916<\/h3>\n<p>\u4e00\u81f4\u3057\u305f\u884c\u3092\u9664\u304d\u305f\u3044\u3068\u304d\u306b\u306f-v\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -v \"GET \/favicon.ico\" *.log<\/pre>\n<h2>--recursive, -r \u30aa\u30d7\u30b7\u30e7\u30f3<\/h2>\n<p>\u3053\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3001\u4eca\u56de\u306e\u305f\u3081\u306bman\u3092\u8aad\u3093\u3067\u3044\u3066\u77e5\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u5bfe\u8c61\u3068\u3057\u3066\u518d\u5e30\u7684\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u63a2\u7d22\u3059\u308b\u3068\u3044\u3046\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true\">grep -e \"GET \/ \" -r .\/log<\/pre>\n<p>\u3068\u3044\u3046\u611f\u3058\u3067\u4f7f\u3044\u307e\u3059\u3002log\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308b\u30d5\u30a1\u30a4\u30eb\u3092\u518d\u5e30\u7684\u306b\u63a2\u7d22\u3057\u307e\u3059\u3002\u3053\u308c\u306f\u3001<\/p>\n<pre class=\"lang:sh decode:true\">find .\/log -type f | xargs grep -e \"GET \/ \"<\/pre>\n<p>\u3068\u540c\u69d8\u306e\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u6700\u8fd1\u3088\u3046\u3084\u304ffind, xargs\u3068grep\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u6163\u308c\u3066\u304d\u305f\u3068\u3053\u308d\u306a\u306e\u3067\u3059\u304c\u3001grep\u306e-r\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3042\u308c\u3070\u3001\u305a\u3044\u3076\u3093\u697d\u306b\u306a\u308a\u305d\u3046\u3067\u3059\u3002<br \/>\n\u307e\u305f\u3001<code>--include, --exclude<\/code>\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<p>\u7279\u5b9a\u306e\u30d5\u30a1\u30a4\u30eb\u3060\u3051\u5bfe\u8c61\u306b\u3057\u305f\u3044<\/p>\n<pre class=\"lang:sh decode:true\">grep -e \"GET \/ \" --include *.log -r .\/log<\/pre>\n<p>\u7279\u5b9a\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u9664\u5916\u3057\u305f\u3044<\/p>\n<pre class=\"lang:sh decode:true\">grep -e \"GET \/ \" --exclude *.log -r .\/log<\/pre>\n<p>\u3053\u308c\u3067\u307e\u305f\u30ed\u30b0\u8abf\u67fb\u304c\u6357\u308a\u307e\u3059\u306d\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002<\/p>\n<p>\u30ed\u30b0\u8abf\u67fb\u306e\u6642\u306b\u306fgrep\u4f7f\u3044\u307e\u3059\u3088\u306d\uff01\u3067\u3082\u826f\u304f\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u304a\u3055\u3089\u3044\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/11\/28\/grep%e3%81%ae%e3%82%aa%e3%83%97%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%8a%e3%81%95%e3%82%89%e3%81%84\/\">\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":[56,23,28,58],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/742"}],"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=742"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":18015,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/742\/revisions\/18015"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}