【问题标题】:How to cut the output of grep command?如何剪切 grep 命令的输出?
【发布时间】:2017-10-06 07:13:41
【问题描述】:

我正在运行盐状态并使用 grep 检索所有失败的检查。默认情况下,每次失败 salt 都会输出以下文本:

ERROR: Minions returned with non-zero exit code

我想从我的命令中删除这个输出,以便我可以使用实际失败(即检查失败的详细信息)输出到一个 .html 文件,该文件将用于显示我们环境的当前状态.

这是我的命令:

salt --state-output=terse -C 'ServerName' state.sls ldapchecker test=True | grep 'Result: Failed'

这是输出:

ERROR: Minions returned with non-zero exit code 
Name: /var/log/stunnel.stunnel.log - Function: file.exists - Result: Failed

我希望只留下检查失败的路径 (/var/log/stunnel.stunnel.log)

编辑:这里要求的是输出:

salt --state-output=terse -C 'BCA-AJT-LD-01' state.sls ldapchecker test=True 2>/dev/null

BCA-AJT-LD-01:
  Name: /opt/checkservices.sh - Function: file.managed - Result: Clean
  Name: /var/log/openldap/slapd.log - Function: file.exists - Result: Clean
  Name: /var/log/stunnel.stunnel.log - Function: file.exists - Result: Failed
  Name: /etc/openldap/ldap.conf - Function: file.exists - Result: Clean
  Name: /etc/openldap/certs/cert8.db - Function: file.exists - Result: Clean
  Name: /etc/openldap/certs/key3.db - Function: file.exists - Result: Clean
  Name: /etc/openldap/certs/secmod.db - Function: file.exists - Result: Clean
  Name: /etc/stunnel/stunnel.conf - Function: file.exists - Result: Clean
  Name: /etc/stunnel/stunnel.pem - Function: file.exists - Result: Clean
  Name: /etc/rsyslog.conf - Function: file.exists - Result: Clean
  Name: salt-master - Function: service.running - Result: Clean
  Name: /opt/serverdetails/serverdetails.sh - Function: file.exists - Result: Clean
  Name: /opt/serverdetails/servers_list - Function: file.exists - Result: Clean
  Name: /opt/serverdetails/style.css - Function: file.exists - Result: Clean
  Name: /opt/serverdetails/test.htm - Function: file.exists - Result: Clean
  Name: /opt/serverversions/serverversions.sh - Function: file.exists - Result: Clean
  Name: /opt/serverversions/servers_list - Function: file.exists - Result: Clean
  Name: /opt/serverversions/style.css - Function: file.exists - Result: Clean
  Name: /opt/serverversions/test.htm - Function: file.exists - Result: Clean
  Name: rsyslog - Function: service.running - Result: Clean
  Name: salt-minion - Function: service.running - Result: Clean
  Name: sshd - Function: service.running - Result: Clean
  Name: ntpd - Function: service.running - Result: Clean

Summary
-------------
Succeeded: 22
Failed:     1
-------------
Total states run:     23

【问题讨论】:

  • 总是占2行吗?
  • 不,它会根据错误的数量而有所不同,实际上可能超过 50 行,但这不太可能。
  • 如果有 50 多个错误行,它是否应该提取并打印 /var/log/stunnel.stunnel.log 行?
  • 不,它应该打印每一个失败的检查。看看我的编辑,应该会更清楚。
  • @jto,所以你想打印所有路径正确的文件名?

标签: bash grep output cut


【解决方案1】:

如果您想使用单个命令,那么 awk 也可以在这里提供帮助。

your_command | awk '/^Name:/{print $2}' 

编辑:根据您的最新编辑,请尝试以下命令。

your_command | awk '/Name:/ && $2 ~ /\// && $0 ~ /Result: Failed/{print $2}'

【讨论】:

  • 确实如此,谢谢您的帮助!如果有多个“结果:失败”输出,这是否可以扩展? @RanvinderSingh13
  • @jto,不客气,很高兴它对您有所帮助。是的,它应该选择每个 Result: failed 字符串,并且其第二列有一个路径并且有字符串 Name: 在其中,所以如果其中 3 个条件在任何一行中都满足,那么它应该飞行。如果您对此有任何疑问,请告诉我。
【解决方案2】:

试试这个代码:

salt --state-output=terse -C 'ServerName' state.sls ldapchecker test=True | grep 'Result: Failed' | awk -F':|-' '{print $2}'

【讨论】:

  • 这对我不起作用,它只输出 ERROR: line
  • 在提示部分2>/dev/null在grep前添加显示,然后试试。我检查了提供的输出,它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多