2017-03-16 45 views
1

我的文件包含如示例中所示的信息。从这个文件中,我需要打印行中的日期,时间和连接数,并将其导出为.csv格式。如何使用AWK在日期间打印

 03/10/17 08:19:32 Timer-10 INFO: 
     Current Connection Pool Statistics 
     Total Connections Count  : 88 (Highest=92) 
     Connections Closed Count  : 30473 
     Available Connections Count : 10 
     Borrowed Connections Count : 78 
     Connections Created Count  : 30561 
     Remaining Pool Capacity Count : 712 (Lowest=708 
     03/10/17 08:19:32 Timer-11 INFO: 
     Current Connection Pool Statistics 
     Total Connections Count  : 10 (Highest=10) 
     Connections Closed Count  : 19174 
     Available Connections Count : 10 
     Borrowed Connections Count : 0 
     Connections Created Count  : 19184 
     Remaining Pool Capacity Count : 590 (Lowest=590) 

产量预计是:

 Date     TotalConnection Count Closed ConnectionCount 
    03/10/17 08:19:32 Timer-10 88      30473 
    03/10/17 08:19:32 Timer-11 10      19174 

任何帮助表示赞赏

谢谢 SKM

+1

欢迎堆栈溢出。 请尽快阅读[关于]和[问]页面。你有什么尝试?你有什么问题?问题标题所隐含的“日期范围”在哪里出现?请阅读如何创建MCVE([MCVE])。你已经显示了输入和期望的输出,这是很好的;你没有显示你有问题的地方。 (日期格式将使日期范围比日期范围更难(如果日期为ISO 8601格式2017-03-10)。 –

回答

0
$ awk '/Timer-[0-9]+/{s = $1 OFS $2 OFS $3 } 
     /Total Connections/{ s = s OFS $(NF-1)} 
    /Connections Closed/{ print s, $NF}' file 

03/10/17 08:19:32 Timer-10 88 30473 
03/10/17 08:19:32 Timer-11 10 19174 

从这个文件,我需要只是打印日期,时间和连接 按行计数并将其导出为.csv格式。

awk只需设置-v OFS=','将产生:

$ awk -v OFS=',' ' 
    /Timer-[0-9]+/{s = $1 OFS $2 OFS $3 } 
    /Total Connections/{ s = s OFS $(NF-1)} 
    /Connections Closed/{ print s, $NF}' file 

03/10/17,08:19:32,Timer-10,88,30473 
03/10/17,08:19:32,Timer-11,10,19174 

如果你想放头再加入print语句中BEGIN块象下面这样:

$ awk -v OFS=',' ' 
    BEGIN{ 
     print "date","time","timer","total_conn","conn_closed" 
    } 
    /Timer-[0-9]+/{s = $1 OFS $2 OFS $3 } 
    /Total Connections/{ s = s OFS $(NF-1)} 
/Connections Closed/{ print s, $NF}' file 

date,time,timer,total_conn,conn_closed 
03/10/17,08:19:32,Timer-10,88,30473 
03/10/17,08:19:32,Timer-11,10,19174 
+0

谢谢您的工作。 – skm

+0

这将是巨大的,如果你能解释这AWK“/ Timer- [0-9] +/{S = $ 1 OFS $ 2个OFS $ 3} /总连接/ {S = S OFS $(NF-1)} /连接已关闭/ {print s,$ NF}' – skm

+0

@skm:如果找到了'timer-'行,则将字段/列1,2,3保存在变量's'中,然后如果发现有'total connections'添加第二个最后一个字段'$(NF-1)'到's','NF'在行/记录中给出全部的字段,最后如果找到连接关闭的行,则打印变量's'和最后一个字段'$ NF')。 'OFS'是输出字段分隔符 –

0

假设一秒钟,你的数据在in.txt,你可以做的一件事情是:

1)尝试将数据转换为面向行的格式,其中每行包含所有需要的信息以隔离处理该行,并且每个以空格分隔的列具有固定的含义。

awk '/Timer/ { 
    date = $1 ; time = $2 ; timer = $3 ; preamble = date " " time " " timer 
} 
/Total Connections/ { 
    print preamble " total-connections " $5 
} 
/Closed Count/ { 
    print preamble " closed-connections " $5 
}' 

这里是这样做的结果是:

03/10/17 08:19:32 Timer-10 total-connections 88 
03/10/17 08:19:32 Timer-10 closed-connections 30473 
03/10/17 08:19:32 Timer-11 total-connections 10 
03/10/17 08:19:32 Timer-11 closed-connections 19174 

然后,您可以管你的解决方案,它组合到每个日期,时间的完整产品线的另一个awk程序,定时组合

awk 'BEGIN { 
    print "date time timer total closed"} {key = $1" "$2" "$3 
} 
/total-connections/ { 
    total[key] = $5} /closed-connections/ {closed[key] = $5 
} END { 
    for (key in total) {print key " " total[key] " " closed[key]} 
}' 

和输出

03/10/17 08:19:32 Timer-10 88 30473 
03/10/17 08:19:32 Timer-11 10 19174 

把他们放在一起:

cat in.txt | awk '/Timer/ {date = $1 ; time = $2 ; timer = $3 ; preamble = date " " time " " timer} /Total Connections/ {print preamble " total-connections " $5} /Closed Count/ {print preamble " closed-connections " $5}' | awk 'BEGIN {print "date time timer total closed"} {key = $1" "$2" "$3} /total-connections/ {total[key] = $5} /closed-connections/ {closed[key] = $5} END {for (key in total) {print key " " total[key] " " closed[key]}}' 

03/10/17 08:19:32 Timer-10 88 30473 
03/10/17 08:19:32 Timer-11 10 19174