How To Exclude with Grep In Linux? – POFTUT

How To Exclude with Grep In Linux?


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.

Linux egrep Command Tutorial with Examples

Exclude with -v  Option

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 -v option.

$ grep -v "192.168.115.128" syslog.1
How To Exclude with Grep In Linux?
How To Exclude with Grep In Linux?

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 poftut  and 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 dhclient  and com with the following command.

$ grep -v  -e "dhclient" -e "com" syslog.1
Multiple Exclude with Extended Regex
Multiple Exclude with Extended Regex

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 grep  with 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 anacron

$ tail -f syslog.1 | grep -v "anacron"
Exclude with Tail Command
Exclude with Command Output

LEARN MORE  What Is "server_name" in Nginx Web Server Configuration?

Leave a Comment