logstash - 在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
解决方案
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)/
推荐阅读
- pandas - 当我们描述分组数据框时,25%、50%、75% 的值是什么?
- tcl - 如何在 Tcl 中创建嵌套字典
- excel - 当搜索值肯定在范围内时,VBA Range.Find() 方法找不到任何东西
- c# - 如何通过串行通信从 Arduino 按钮获取标志?
- docker - AWS ECS 如何在私有桥接网络中启动容器
- android - 谷歌只加载一次原生广告横幅
- kubernetes - 水平 pod Autoscaler 在 GKE 上过于激进地扩展自定义指标
- sql-server - 如何从 SQL 数据透视表中删除空值
- ios - 检测通话时长
- php - 在一页中显示所有用户帖子但查询太慢