Knowledge Base

Browse our knowledge base for free solutions to common problems

Reverse Grep / Invert Grep in Linux

Created On: 14 September 2022
Written by: Ben

Grep is a useful tool to filtering large quantities of data and acquiring just the data you need. In some circumstances the format of the data can be fine and it's easier to remove some other regular recurring lines that impact the structure of the data.

Take the following as an example, this data was generated using a bash script that checks the config on some devices via SSH:

cat script-output.log | more

======================================================================================
Host 1 - NTP Server
======================================================================================
Psuedo-terminal will not be allocated because stdin is not a terminal.
Zabbix Config: Present
Zabbix Config MD5: Correct
SNMP Config: Present
SNMP Config MD5: Correct
SSH Config: Present
SSH MD5: Correct

======================================================================================
Host 1 - Web Server
======================================================================================
Psuedo-terminal will not be allocated because stdin is not a terminal.
Zabbix Config: Present
Zabbix Config MD5: Correct
SNMP Config: Present
SNMP Config MD5: Correct
SSH Config: Present
SSH MD5: Correct

======================================================================================
Host 1 - Polling Server
======================================================================================
Psuedo-terminal will not be allocated because stdin is not a terminal.
Zabbix Config: Present
Zabbix Config MD5: Correct
SNMP Config: Present
SNMP Config MD5: Correct
SSH Config: Present
SSH MD5: Correct

The data in the above log does not look overly damaged but we do not want the following line appearing in there:

Psuedo-terminal will not be allocated because stdin is not a terminal.

To remove this we use grep -v with the repeated string of text we we to remove. So instead of just cat my log I use the following:

cat script-output.log | grep -v "Psuedo-terminal will not be allocated because stdin is not a terminal." | more

This will result in a  much cleaner log output:

cat script-output.log | more

======================================================================================
Host 1 - NTP Server
======================================================================================
Zabbix Config: Present
Zabbix Config MD5: Correct
SNMP Config: Present
SNMP Config MD5: Correct
SSH Config: Present
SSH MD5: Correct

======================================================================================
Host 1 - Web Server
======================================================================================
Zabbix Config: Present
Zabbix Config MD5: Correct
SNMP Config: Present
SNMP Config MD5: Correct
SSH Config: Present
SSH MD5: Correct

======================================================================================
Host 1 - Polling Server
======================================================================================
Zabbix Config: Present
Zabbix Config MD5: Correct
SNMP Config: Present
SNMP Config MD5: Correct
SSH Config: Present
SSH MD5: Correct

In this case preventing the line printing inside of the log should be looked into to see if it can be avoided, however grep-v provides a quick workaround for us.

ICTU LTD is a company registered England and Wales (Company No. 09344913) 15 Queen Square, Leeds, West Yorkshire, England, LS2 8AJ
Copyright © 2024 ICTU LTD, All Rights Reserved.
exit