---
title: "Logparse工具的iCodepage使得统计效率提高"
author: "leng_cn"
date: "2012-04-18T12:00:23Z"
tags: []
categories: ["开源软件"]
slug: "logparse-icodepage"
draft: false
lastmod: "2012-04-18T12:04:40Z"
id: 1489269
url: "https://leng.iteye.com/blog/1489269"
---

最近要对IIS的访问流量进行统计，由于数据量太大（按小时分，最小的的日志文件也得一百多兆），所以采用了Logparse进行统计。
这两天需要对访问的IP进行过滤（c-ip)，通过命令行执行LOGPARSE命令，会抛出一个未知异常(Log row terminates unexpectedly），十分郁闷，排查日志文件发现，在日志文件里面有一个乱码，就尝试着看看通过编码能不能解决这个问题，查阅了LOGPARSE的帮助文档，发现里面有个参数是iCodepage，通过此编码可以按照不同的格式进行文件解析，格式如下：
Logparse iCodepage:

原本的查询语句及结果是：
E:\LogParserRelease\LogPackage__2012_04_15>LogParser "select c-ip as
 c-ip,sum(cs-bytes) as cs-bytes from ex12041511.log where c-ip in('111.16.115.12
0';'221.1.119.4';'58.250.241.224';'211.138.100.177';'211.137.180.234';'122.74.11
.121') group by c-ip" -e:10
c-ip            cs-bytes
--------------- --------
211.138.100.177 661792
211.137.180.234 471792
221.1.119.4     69433
122.74.11.121   1459
Task completed with parse errors.
Parse errors:
Log row terminates unexpectedly
  LogFile "E:\LogParserRelease\LogPackage__2012_04_15\ex12041511.
  log", Row number 157485

Statistics:
-----------
Elements processed: 473979
Elements output:    4
Execution time:     6.67 seconds
可以发现，执行这个语句花费了6秒多的时间，加上iCodepage后的语句是：
E:\LogParserRelease\LogPackage__2012_04_15>LogParser<span style="color:red">** -iCodepage:437**</span>
"select c-ip as c-ip,count(c-ip) as cnt-ip,sum(cs-bytes) as cs-bytes from ex1204
1511.log where c-ip in('111.16.115.120';'221.1.119.4';'58.250.241.224';'211.138.
100.177';'211.137.180.234';'122.74.11.121') group by c-ip" -e:10
c-ip            cnt-ip cs-bytes
--------------- ------ --------
211.138.100.177 887    661792
211.137.180.234 610    472657
221.1.119.4     182    69433
122.74.11.121   4      1459

Statistics:
-----------
Elements processed: 473980
Elements output:    4
Execution time:     1.16 seconds
执行的时间变成了1秒多，简直是神速呀，哈哈