grep is very useful tool used by a lot of tech guys. grep provides different functions to match given text. We have all ready mentioned these useful options of grep. In this tutorial we will look different ways to exclude in grep.
The basic way to exclude given term line from given text is
-v option. This will look for given term and remove line which contains term. In this example we will remove lines contains IP address
192.168.1.1 by using
$ grep -v "192.168.115.128" syslog.1
Multiple Exclude with Multiple Grep
What if we need multiple excludes? We can use multiple
grep too. We can pipe multiple grep end to end. We will exclude
com terms from file named
syslog.1 with the following command.
$ grep -v "poftut" syslog.1 | grep "com"
Multiple Exclude with Extended Regex
grep command provides extended regex functionality. This is used to provide multiple regex pattern in a single shot. We can use this feature in order to exclude multiple terms. We will use
-e option and provide regex patterns. We will exclude
com with the following command.
$ grep -v -e "dhclient" -e "com" syslog.1
Exclude Tail Command Output
tail command provides the ability to read log files by streaming. But if there is a lot of logs this may be hard to read. We can use
tail command to gather to filter unwanted lines. We will pipe the
tail command output to the
grep command. We will filter lines those provides
$ tail -f syslog.1 | grep -v "anacron"