我的文件包含了如示例中所示的信息。从该文件中,我只需要打印日期、时间和连接计数,并将其导出为.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
发布于 2017-03-16 17:07:08
$ 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发布于 2017-03-16 17:09:30
假设您的数据在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 19174https://stackoverflow.com/questions/42839912
复制相似问题