#Shell脚本:读取access日志文件并解析url encode内容
抽取encode内容
$ head temp.txt127.0.0.1 - - [22/Jul/2015:20:04:26 +0800] "GET /goods/search?keyword=%E9%BA%BB%E8%B1%86%E8%85%90 HTTP/1.1" 200 76 "-" "-"127.0.0.1 - - [22/Jul/2015:20:04:30 +0800] "GET /goods/search?keyword=%E5%B0%8F%E7%B1%B3%E6%A4%92 HTTP/1.1" 200 1897 "-" "-"127.0.0.1 - - [22/Jul/2015:20:04:39 +0800] "GET /goods/search?keyword=%E8%8F%8C%E7%B1%BB%E9%87%91%E9%92%88%E8%8F%87 HTTP/1.1" 200 1038 "-" "-"127.0.0.1 - - [22/Jul/2015:20:04:49 +0800] "GET /goods/search?keyword=%E8%8C%84%E5%AD%90 HTTP/1.1" 200 1166 "-" "-"127.0.0.1 - - [22/Jul/2015:20:04:51 +0800] "GET /goods/search?keyword=%E9%A6%99%E8%8F%9C HTTP/1.1" 200 429 "-" "-"$ cat temp.txt | egrep -o '(%[A-Z0-9]{2})+' %E9%BA%BB%E8%B1%86%E8%85%90%E5%B0%8F%E7%B1%B3%E6%A4%92%E8%8F%8C%E7%B1%BB%E9%87%91%E9%92%88%E8%8F%87%E8%8C%84%E5%AD%90%E9%A6%99%E8%8F%9C
linux 命令行urldecode
$ alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.stdin.read())"'$ echo -n "%E7%99%BD%E8%90%9D%E5%8D%9C" | urldecode 白萝卜
综合上述步骤:
$ cat temp.txt | egrep -o '(%[A-Z0-9]{2})+' | urldecode麻豆腐小米椒菌类金针菇茄子香菜
补充:
按照出现次数降序排序并选择出现次数最多的10个
$ cat temp.txt | egrep -o '(%[A-Z0-9]{2})+' | urldecode | sort | uniq -c | sort -t ' ' -k 2 -n -r | head -n 10 1 麻豆腐 1 香菜 1 菌类金针菇 1 茄子 1 小米椒 1
参考文档: