grep 字符串并从日志文件中查找更改总数,写入 CSV 文件

Posted

技术标签:

【中文标题】grep 字符串并从日志文件中查找更改总数,写入 CSV 文件【英文标题】:grep string and find total no of changes from log file,write in CSV file 【发布时间】:2018-05-13 14:37:14 【问题描述】:

我有一个输入文件(generator_file.log):

[+++]          Added names:          [+++]
     -> Add- response.names (3):
     -> Add- chat.names (2):
     -> Add- events.names (2):
[///]     Modified active rules:     [///]
     -> Mod- events.names (2):
     -> Mod- mal.names (1):
[---]         Removed names:         [---]
     -> Del- chat.names (2):
     -> Del- events.names (8):
     -> Del- jan.names (4):

预期的输出应如下所示:

Add,response,3
Add,chat,2
Add,events,2
Del,chat,2
Del,events,8
Del,jan,4
Total no of Added Names: 7
Total no of Deleted Names: 14

下面是我尝试过但没有得到想要的输出的shell脚本:

egrep "Del-|Add-" generator_file.log |awk '
BEGINAddname=0; Delname=0;

      if(match($0,"Add-")>0)
       
         split($0,addcountb,"(");
         split(addcountb[2], addcount, ")");
         Addname+=addcount[1] ;
       
     if(match($0,"Del-")>0)
      
         split($0,delcountb,"(");
         split(delcountb[2], delcount, ")");
         Delname+=delcount[1] ;
      

END print "Number of names Added: "Addname; print "Number of names Deleted: "Delnames;'

【问题讨论】:

【参考方案1】:

awk 来救援!

$ awk -F' +|[-.()]' -v OFS=, 'c=$(NF-1) 
                       /Del-/ del+=c; f=1 
                       /Add-/ add+=c; f=1 
                       f      print $4,$6,c; f=0 
                       END    print "Added: " add; 
                               print "Deleted: " del' file

Add,response,3
Add,chat,2
Add,events,2
Del,chat,2
Del,events,8
Del,jan,4
Added: 7
Deleted: 14

或者可能是

$ awk -F' +|[-.()]' -v OFS=, '/(Del|Add)-/c=$(NF-1); cs[$4]+=c;
                                           print $4,$6,c
                              END         print "Added: "   cs["Add"];
                                           print "Deleted: " cs["Del"]' file

【讨论】:

以上是关于grep 字符串并从日志文件中查找更改总数,写入 CSV 文件的主要内容,如果未能解决你的问题,请参考以下文章

linux如何在日志中查找关键字

在容器的 docker 日志中查找字符串

Linux grep 命令初步学习

linux下日志文件的查找(tail和grep)

Linux根据关键字查找日志文件上下文

Linux从日志文件中查找某个时间段