首页 > 解决方案 > 在logstash中使用正则表达式按名称过滤文件

问题描述

在将匹配的文件输出到命名目录之前,我正在提取一系列文件并使用logstash根据正则表达式仅过滤掉我需要的文件,但是正则表达式不起作用,并且没有任何内容被过滤掉。

output {
    if [filename] =~ /.*[abc|def].*/
 {
        file {
        path => "/my/directory/%{filename}-%{+YYYY-MM-dd}.log"
        codec => line { format => "%{message}"}
        }
    }
}

传入文件的格式为:

P01_abc_stdout.log-2020-11-10-11.log
P01_rtf_stdout.log-2020-11-10-11.log
P01_ccc_stdout.log-2020-11-10-11.log
P01_def_stdout.log-2020-11-10-11.log
P01_ces_stdout.log-2020-11-10-11.log

但是所有这些文件都通过了,即使我希望它只匹配

P01_abc_stdout.log-2020-11-10-11.log
P01_def_stdout.log-2020-11-10-11.log

标签: logstashlogstash-configuration

解决方案


if [filename] =~ /.*[abc|def].*/

Square brackets define a character group, so that will match any file that contains one of the characters a through f, or pipe. All of your filenames contain stdout, which includes a d, so they all match. The leading and trailing .* are not needed, since the pattern is not anchored. Try

if [filename] =~ /(abc|def)/

推荐阅读