regex 正则表达式在多行后查找匹配

p8h8hvxi  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(129)

我正在尝试查找每个服务的错误日志计数。此日志文件非常大。但log模式如下所示:

[DEFService@]: connect failed: not authorized to connect
WARNING: Closing connection from <ipaddress>>  due to timeout, exceeded timeout of 20.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
[ABCService]: connect failed: not authorized to connect
WARNING: Connection timeouts delayed around 15 seconds.
WARNING: Closing connection from <ipaddress>>  due to timeout, exceeded timeout of 20.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
[XYZService@]: connect failed: not authorized to connect
WARNING: Closing connection from <<ipaddress>> due to timeout, exceeded timeout of 20.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
[DEFService@]: connect failed: not authorized to connect
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open 

files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.
ERROR: Accept() failed:  many open files. Please check each process and system limits on the number of open files.

上面的错误消息“许多打开的文件”,已经发生了很多次为每个服务,我需要采取的计数每个服务,多少次的“许多打开的文件”错误消息已发生为该服务.例如DEFService
我试过:(DEFService.*)$(\r\n)(.*many open files.*) .我得到的计数为“DEFService”为1,因为只有“许多打开的文件”错误消息发生在服务名称后的下一行,但实际计数为2,因为还有一个,但中间有一个警告行,由于Regex无法识别,需要采取该计数。我没有得到Regex。请帮帮忙

ljo96ir5

ljo96ir51#

我需要计算每个服务的数量,该服务出现“许多打开的文件”错误消息的次数。
没有必要使用正则表达式,因为你可以这样做。

  • 创建一个Map来保存计数
  • 从文件中读取字符串。
  • 如果字符串以服务开头,则捕获它并将其用作Map键
  • 否则,如果行包含错误,则对Map中该服务的计数求和

这假定日志文件是一致写入的。例如,如果行以[开始,则服务将封装在结束的]中。

Map<String, Integer> count = new HashMap<>();
String currentService = "";
String logFile = <source of error messages>

try ( Scanner readFile = new Scanner(new File(logFile))) {
    while (readFile.hasNextLine()) {
        String line = readFile.nextLine();
        if (line.startsWith("[")) {
             currentService = line.substring(1, line.indexOf("]"));
        } else if (line.contains("many open files.")) {
            count.merge(currentService, 1, Integer::sum);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

count.entrySet().forEach(System.out::println);

印刷品

XYZService@=12
DEFService@=23
ABCService=12

请注意,上述内容可以合并到当前正在阅读错误消息的任何代码中。

相关问题