如何剪切grep命令的输出?

时间:2017-10-06 07:13:42

标签: bash grep output cut

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

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 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:1)

试试这段代码:

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