【问题标题】:Grep syslog configuration for remote logging entries用于远程日志记录条目的 Grep syslog 配置
【发布时间】:2018-05-03 15:24:36
【问题描述】:

我想 grep 我的 rsyslog 配置文件以检查是否配置了远程日志记录条目。

我希望 grep 带来的行如下:

auth.*,authpriv.*               @loghost.example.com:10514 # One "@" means UDP
*.*                             @@loghost.example.com      # Two "@" means TCP

到目前为止,我已经设法生成了以下正则表达式,它只给我带来了以一组字母或星号开头的行,后跟一个点,然后是另一组字符或星号:

grep "^[A-Za-z|*]*\.[A-Za-z|*]" /etc/rsyslog.conf

如何让 grep 正确识别该行的其余部分?由于该行之后可以跟一个逗号,更多的东西,一个空格序列和一个“@”符号,或者它可以后面跟着一个空格序列和一个“@”符号。

编辑:我目前的正则表达式能得到什么:

[root@RHEL7lab /]# grep "^[A-Za-z|*]*\.[A-Za-z|*]" /etc/rsyslog.conf
*.info;mail.none;authpriv.none;cron.none                /var/log/messages
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
cron.*                                                  /var/log/cron
*.emerg                                                 :omusrmsg:*
auth.*,authpriv.*               @loghost.example.com:10514 # One "@" means UDP
*.*                             @@loghost.example.com      # Two "@" means TCP
[root@RHEL7lab /]#

我想要一个只产生上面显示的最后两行的正则表达式。

我已经成功地将它传递给另一个 grep,如下所示:

[root@RHEL7lab /]# grep "^[A-Za-z|*]*\.[A-Za-z|*]" /etc/rsyslog.conf | grep "\s@"
auth.*,authpriv.*               @loghost.example.com:10514 # One "@" means UDP
*.*                             @@loghost.example.com      # Two "@" means TCP
[root@RHEL7lab /]#

但我认为我可以一次完成,我只是缺乏技能。

【问题讨论】:

标签: regex linux grep syslog


【解决方案1】:

您可以匹配以字母数字开头的行,* 或 . 字符,然后可以有 0+ 字符而不是空格,然后在任何 1+ 空格字符之后有 @:

grep -E '^[[:alnum:]*.]+[^[:space:]]*[[:space:]]+@' file

请参阅regex demo。

更具体的正则表达式看起来像

grep -E '^([[:alnum:]]+|\*)\.([[:alnum:]]+|\*)[^[:space:]]*[[:space:]]+@' file

见this regex demo。

模式详情

  • ^ - 字符串开头
  • [[:alnum:]*.]+ - 1 个或多个字母数字字符、* 或 .
  • ([[:alnum:]]+|\*) - 1+ 个字母数字字符或 * 符号
  • \. - 一个点
  • ([[:alnum:]]+|\*) - 1+ 个字母数字字符或 * 符号
  • [^[:space:]]* - 0+ 字符而不是空格字符
  • [[:space:]]+ - 1+ 个空格字符
  • @ - @ 字符。

请注意,-E 选项允许使用 POSIX ERE 语法(无需转义 +,例如)。

【讨论】:

  • 很棒的解释,我当然还有很多正则表达式要学习!
猜你喜欢
  • 2013-09-12
  • 2011-04-27
  • 1970-01-01
  • 2019-04-24
  • 2011-07-24
  • 2023-03-08
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多