我的文件包含示例中显示的信息。从这个文件我需要打印行中的日期,时间和连接数,并将其导出为.csv格式。
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
答案 0 :(得分: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格式。
只需在-v OFS=','
中设置awk
即可生成:
$ 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
答案 1 :(得分: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