首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用AWK在日期之间打印

如何使用AWK在日期之间打印
EN

Stack Overflow用户
提问于 2017-03-16 16:38:48
回答 2查看 185关注 0票数 1

我的文件包含了如示例中所示的信息。从该文件中,我只需要打印日期、时间和连接计数,并将其导出为.csv格式。

代码语言:javascript
运行
复制
      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)

预期产出如下:

代码语言:javascript
运行
复制
       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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-16 17:07:08

代码语言:javascript
运行
复制
$ 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,就会产生:

代码语言:javascript
运行
复制
$ 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块中,如下所示:

代码语言:javascript
运行
复制
$ 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
EN

Stack Overflow用户

发布于 2017-03-16 17:09:30

假设您的数据在in.txt中,您可以做的一件事是:

1)尝试将数据转换成面向行的格式,在这种格式中,每一行都有单独处理行所需的所有信息,并且每个空格分隔的列都有固定的含义。

代码语言:javascript
运行
复制
 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
 }'

这样做的结果如下:

代码语言:javascript
运行
复制
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程序,该程序将其组装成一个完整的行,用于每个日期、时间、计时器组合。

代码语言:javascript
运行
复制
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]}
}'

以及输出

代码语言:javascript
运行
复制
03/10/17 08:19:32 Timer-10 88 30473
03/10/17 08:19:32 Timer-11 10 19174

把这一切结合在一起:

代码语言:javascript
运行
复制
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]}}'

给出

代码语言:javascript
运行
复制
03/10/17 08:19:32 Timer-10 88 30473
03/10/17 08:19:32 Timer-11 10 19174
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42839912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档