发布:2022/12/12 15:23:16作者:管理员 来源:本站 浏览次数:945
前言
一直以来都认为AWK只能在Linux\Unix中才能使用,今天偶然查到有Window版本的AWK。
下载地址:http://sourceforge.net/projects/gnuwin32/files/gawk/3.1.6-1/
准备
下载gawk-3.1.6-1-setup.exe
安装
设置Path,包含安装目录:C:\Program Files (x86)\GnuWin32\bin
应用
说明: 源数据是模拟售票机的历史记录,字段含义:销售日期,出发站名称,到达站名称,购买票的数量,总金额(Day_pass表示通票)
源数据文件history.txt
2015-11-16 16:40:SS,AAA,DDD,2 tickets,4 CNY
2015-11-16 16:40:SS,AAA,Day_pass,1 ticket,10 CNY
2015-11-16 16:40:SS,AAA,EEE,3 tickets,6 CNY
2015-11-16 16:45:SS,BBB,Day_pass,2 tickets,20 CNY
2015-11-16 16:49:SS,CCC,Day_pass,1 ticket,10 CNY
2015-10-17 19:54:34,AAA,DDD,1 tickets,2 CNY
2015-10-17 19:55:10,AAA,EEE,1 tickets,2 CNY
2015-10-17 19:56:15,AAA,EEE,1 tickets,2 CNY
2015-10-17 20:14:13,AAA,EEE,1 tickets,2 CNY
2015-10-17 20:20:35,AAA,DDD,1 tickets,2 CNY
2015-10-17 20:29:24,AAA,DDD,1 tickets,2 CNY
2015-10-17 20:29:55,AAA,Day_pass,1 tickets,10 CNY
2015-10-17 20:49:02,AAA,Day_pass,1 tickets,10 CNY
2015-10-17 20:51:36,AAA,DDD,1 tickets,2 CNY
2015-10-03 23:47:23,AAA,CCC,1 Ticket,2 CNY
2015-10-03 23:47:36,AAA,CCC,1 Ticket,2 CNY
2015-10-03 23:47:54,AAA,Day_pass,1 Ticket,10 CNY
2015-10-03 23:48:10,AAA,BBB,2 Tickets,4 CNY
2015-10-06 11:10:07,AAA,EEE,1 Ticket,2 CNY
2015-10-09 09:14:51,AAA,EEE,2 Tickets,4 CNY
2015-10-09 09:25:12,AAA,FFF,1 Ticket,2 CNY
2015-10-10 03:32:26,AAA,DDD,1 tickets,4 CNY
2015-10-10 03:32:32,AAA,EEE,3 tickets,12 CNY
2015-10-10 03:35:16,AAA,Day_pass,1 tickets,10 CNY
2015-10-10 03:35:21,AAA,Day_pass,1 tickets,10 CNY
2015-10-10 04:02:44,AAA,CCC,1 tickets,2 CNY
2015-10-10 04:06:32,AAA,CCC,1 tickets,2 CNY
2015-10-10 04:38:57,AAA,CCC,1 tickets,2 CNY
2015-10-11 02:23:35,AAA,CCC,1 tickets,2 CNY
2015-10-11 02:26:05,AAA,CCC,1 tickets,2 CNY
2015-10-11 02:27:16,AAA,CCC,1 tickets,2 CNY
2015-09-17 20:26:19,AAA,DDD,1 ticket,2 CNY
2015-09-17 20:30:20,AAA,DDD,2 tickets,2 CNY
2015-09-17 20:30:39,AAA,FFF,1 ticket,2 CNY
2015-09-17 20:36:29,AAA,DDD,1 ticket ,2 CNY
2015-09-17 20:46:42,AAA,DDD,1 ticket ,2 CNY
2015-09-17 20:47:13,AAA,EEE,4 tickets ,8 CNY
2015-09-17 20:49:16,DDD,Day_pass,1 ticket,10 CNY
2015-09-17 21:01:08,AAA,DDD,1 ticket ,2 CNY
2015-09-17 21:01:25,AAA,EEE,3 tickets ,2 CNY
2015-09-17 21:03:41,DDD,FFF,1 ticket ,2 CNY
2015-09-17 21:06:57,AAA,FFF,2 tickets ,4 CNY
2015-09-17 21:27:20,FFF,Day_pass,1 ticket ,10 CNY
2015-09-17 21:51:58,AAA,FFF,1 ticket ,2 CNY
2015-09-18 16:43:18,AAA,DDD,2 tickets ,4 CNY
2015-09-18 17:32:28,AAA,EEE,1 ticket ,2 CNY
2015-09-18 18:06:49,AAA,EEE,1 ticket ,2 CNY
2015-09-25 09:46:42,AAA,FFF,1 ticket ,2 CNY
2015-09-25 09:47:13,BBB,EEE,1 ticket ,2 CNY
2015-09-25 09:48:09,BBB,FFF,1 ticket ,2 CNY
2015-09-25 10:06:32,AAA,DDD,8 tickets ,16 CNY
业务逻辑说明:从历史记录中查询一段时间内,某个出发站累计卖出了多少票(按照日期和到达站进行分组统计)
批处理文件 query.cmd
gawk -F "[, ]" "$3~/(%1)/ && ($1 >=\"%2\" && $1 <=\"%3\") { print $1,$3,$4,$5}" history.txt|gawk "{group[$1\" \"$3] += $4 }END{for (i in group) print i,group[i]}"| sort > temp.txt
使用安装包中的AWK和GAWK都可以
CMD调用
query.cmd AAA 2015-10-01 2015-10-31
-结果
2015-10-03 BBB 2
2015-10-03 CCC 2
2015-10-03 Day_pass 1
2015-10-06 EEE 1
2015-10-09 EEE 2
2015-10-09 FFF 1
2015-10-10 CCC 3
2015-10-10 Day_pass 2
2015-10-10 DDD 1
2015-10-10 EEE 3
2015-10-11 CCC 3
2015-10-17 Day_pass 2
2015-10-17 DDD 4
2015-10-17 EEE 3
Console程序调用
static void Main(string[] args)
{
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "query.cmd";//设定需要执行的命令
startInfo.Arguments = "AAA 2015-10-10 2015-11-20";//“/C”表示执行完命令后马上退出
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
startInfo.RedirectStandardInput = false;//不重定向输入
startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
try
{
if (process.Start())//开始进程
{
process.WaitForExit();//这里无限等待进程结束
using (var fs = File.Open("temp.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var bs = new BufferedStream(fs))
using (var sr = new StreamReader(bs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
finally
{
process.Close();
}
Console.Read();
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4