简体   繁体   中英

How to cut the output of grep command?

I am running a salt state and using grep to retrieve any checks that have failed. Upon every failure salt by default outputs the following text:

ERROR: Minions returned with non-zero exit code

I want to cut this output from my command so that I can use the actual failure (ie, the details of which check failed) to be output to a .html file which will be used to display the current state of our environment.

Here is my command:

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

And here is the output:

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

I would like to be left with just the path of the failed check (/var/log/stunnel.stunnel.log)

EDIT: As requested here is the output of:

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

If you want to use single command then awk could help here too.

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

EDIT: As per your latest edit could you please try following command.

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

试试这个代码:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM