使用批处理文件计算每小时的事务数

时间:2016-09-08 14:40:22

标签: batch-file

我的日志是文本文件格式,看起来像这样

UID,      Provider,     MobileNo,      Result Time,        Outcome
443,        ABC,        65437261,   2016-08-09 23:59:59,     Pass
445,        ABC,        87658905,   2016-08-09 23:58:05,     Pass
023,        ABC,        87658905,   2016-08-09 21:58:05,     Pass
023,        ABC,        87658905,   2016-08-09 19:58:05,     Pass

我想知道如何创建一个读取文件并计算每小时事务数的批处理文件。

我希望我的结果如下:

2300Hrs 345 transactions
2200Hrs 2891 transactions
0700Hrs 8873 transactions

2 个答案:

答案 0 :(得分:3)

我不理解您的请求("每小时的交易次数"是速度期限)。但是,如果您希望计算 的数量 ,那么这是一个解决方案:

@echo off
setlocal

rem Count transactions in the same hour
for /F "skip=1 tokens=5 delims=: " %%a in (theLogFile.txt) do set /A "trans[%%a]+=1"

rem Report them
for /F "tokens=2,3 delims=[]=" %%a in ('set trans[') do echo %%a00Hrs %%b transactions

具有给定数据的输出示例:

1900Hrs 1 transactions
2100Hrs 1 transactions
2300Hrs 2 transactions

答案 1 :(得分:0)

...作为@Aacini优秀响应的延续,并预先阻止Provider和/或MobileNo字段中不可避免的空间,或许:

@Echo Off
SetLocal

Rem Count transactions in the same hour
For /F "Skip=1 Tokens=4 Delims=," %%a In (theLogFile.txt) Do (Set _=%%a
    Call Set/A "trans[%%_:~-8,2%%]+=1")

Rem Report them
For /F "Tokens=2,3 Delims=[]=" %%a In ('Set trans[') Do (
    Echo( %%a00Hrs %%b transaction(s^))
Timeout -1