如果接下来的X行不包含特定字符串,则awk搜索字符串 [英] awk searching for string if next X lines doesn't contain a specific string

查看:104
本文介绍了如果接下来的X行不包含特定字符串,则awk搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我上一个问题之后 grep接下来的X行不包含特定字符串 我想获得帮助以使awk更加简单.

Following my previous question grep if the next X lines doesn't contain a specific string I'd like to get help in order to make the awk more simple.

给出以下日志:

2018-04-04 04:37:41,916 [main] DEBUG com.zaxxer.hikari.HikariConfig - autoCommit......................true
    2018-04-04 04:37:41,916 [main] DEBUG com.zaxxer.hikari.HikariConfig - catalog.........................null
    2018-04-04 04:37:41,916 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionInitSql...............null
    2018-04-04 04:37:41,916 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTestQuery.............null
    2018-04-04 04:48:43,209 [housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - Before cleanup  stats (total=13, active=2, idle=11, waiting=0)
    2018-04-04 05:16:19,226 [housekeeper] DEBUG not-relevant...
    2018-04-04 05:45:28,383 [housekeeper] WARN  com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@2f350071, stack trace follows
    java.lang.Exception: Apparent connection leak detected
        at com.sql.HikariConnectionPool.getConnection(java:)
        at com.DBConnection.getConn(java:)
        at com.DBConnection.getConn(java:)
        at com.EAgent.checkER(aaa.java:)
        at com.EAgent$EExecuter.run(aaa.java:)
    2018-04-04 05:55:54,425 [housekeeper] DEBUG not-relevant...
2018-04-04 04:48:13,208 [housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - Before cleanup stats (total=13, active=0, idle=13, waiting=0)
2018-04-04 04:48:13,208 [housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - After cleanup  stats (total=13, active=0, idle=13, waiting=0)
    2018-04-04 05:58:16,814 [DBPool housekeeper] WARN  com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@45df031, stack trace follows
    java.lang.Exception: Apparent connection leak detected
        at com.HikariConnectionPool.getConnection(HikariConnectionPool.java:)
        at com.DBConnection.getConn(aaa.java:)
        at com.DBConnection.getConn(aaa.java:)
        at com.m.checkUC(aaa.java:)
        at com.m.run(aaa.java:)
        at java.c.ThreadPoolExecutor.runWorker(aaa.java:)
        at java.c.ThreadPoolExecutor$Worker.run(aaa.java:)
        at java.lang.Thread.run(aaa.java:)
    2018-04-04 04:37:41,921 [main] INFO  com.zaxxer.hikari.HikariDataSource - Started.
    2018-04-04 04:49:43,209 [housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - Before cleanup stats (total=11, active=0, idle=11, waiting=0)
    2018-04-04 04:49:43,209 [housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - After cleanup  stats (total=11, active=0, idle=11, waiting=0)

我想要一个可通过以下条件过滤的awk代码:
1.行以数字开头.
2.包含:超时失败"或连接泄漏检测"或"WARN"
3.不包含EAgent(排除-即使包含WARN或#2中的其他术语)

I'd like an awk code that filter by the following conditions:
1. Line starts with digits.
2. Contains: "Timeout failure" or "Connection leak detection" or "WARN"
3. Does not contain EAgent (exclude - even if contains WARN or other term from #2)

通过这种方式,无需在公式中添加INFO和DEBUG以及其他不相关的日志行-它会被自动忽略.
(仅搜索我们需要的,而不是我们不需要的...)

In this way, there will be no need to add INFO and DEBUG and other irrelevant log lines to the formula - it will be ignored automatically.
(Search only what we need, not what we do not need ...)

输出应仅显示第二个WARN堆栈跟踪,而不显示"EAgent":

The output should presents only the second WARN stack-trace without the "EAgent":

        2018-04-04 05:58:16,814 [DBPool housekeeper] WARN  com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for com.mysql.jdbc.JDBC4Connection@45df031, stack trace follows
    java.lang.Exception: Apparent connection leak detected
        at com.HikariConnectionPool.getConnection(HikariConnectionPool.java:)
        at com.DBConnection.getConn(aaa.java:)
        at com.DBConnection.getConn(aaa.java:)
        at com.m.checkUC(aaa.java:)
        at com.m.run(aaa.java:)
        at java.c.ThreadPoolExecutor.runWorker(aaa.java:)
        at java.c.ThreadPoolExecutor$Worker.run(aaa.java:)
        at java.lang.Thread.run(aaa.java:)

谢谢

推荐答案

能否请您关注并告诉我,这是否对您有帮助.

Could you please try following and let me know if this helps you.

awk '{line=$0;gsub(/^ +/,"")} /^[0-9]+/{flag=""} /^[0-9]+/ && (/WARN/ || /Timeout failure/ || /Connection leak detection/){flag=1;if(val && val !~ /EAgent/){print val};val=""}  flag{val=val?val ORS line:line} END{if(val){print val}}' Input_file

现在也添加非单一衬里形式的解决方案.

Adding a non-one liner form of solution too now.

awk '
{
  line=$0;
  gsub(/^ +/,"")
}
/^[0-9]+/{
  flag=""}
/^[0-9]+/ && (/WARN/ || /Timeout failure/ || /Connection leak detection/){
  flag=1;
  if(val && val !~ /EAgent/){
    print val};
  val=""
}
flag{
  val=val?val ORS line:line}
END{
  if(val){
    print val}
}'   Input_file



说明: 现在也添加了代码说明,以供所有人理解和学习.



Explanation: Adding explanation of code too now for understanding and learning for all.

awk '
{
  line=$0;                                                                ##Creating variable named line which will have current line value in it.
  gsub(/^ +/,"")                                                          ##using gsub for removing initial space from each line to match.
}
/^[0-9]+/{                                                                ##Checking condition if a line starts with digits then do following.
  flag=""}                                                                ##Nullifying variable flag here.
/^[0-9]+/ && (/WARN/ || /Timeout failure/ || /Connection leak detection/){##Checking if a line starting from 0 to 9 and having either WARN, timeout or leak ones.
  flag=1;                                                                 ##Setting variable named flag value as 1 here.
  if(val && val !~ /EAgent/){                                             ##Checking condition if variable val is NOT NULL and val NOT having string EAgent do follows.
     print val};                                                          ##Printing variable named val here.
  val=""}                                                                 ##Nullifying variable val here.
  flag{                                                                   ##Checking condition if variable flag is NOT NULL here then do following.
     val=val?val ORS line:line}                                           ##Creating variable vale and concatenating its value with own value or have line variable in.
END{                                                                      ##Staring END section of awk code here now.
  if(val){                                                                ##Checking if variable val value is NOT NULL then do following.
     print val}                                                           ##Printing the variable val value here.
}' Input_file                                                    ##Mentioning Input_file name here.

这篇关于如果接下来的X行不包含特定字符串,则awk搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆