Using grep to Exclude Lines Containing Certain Characters/Text

I like to watch Apache log files using tail -f but they often get filled up with data I don’t want/need to see – like, when I access the web site.

grep is a great tool to search for certain information in text files – but it can also exclude certain pieces of information which can be equally as useful.

So, for example, if I want to watch a log file using tail -f, but exclude any information relating to the IP address “192.168.1.101” then we can use the grep -v switch:

tail -f access.log | grep -v "192.168.1.101"

What this will do is show everything, apart from a line with that IP address in.

Of course, this works with any other command with text being piped into grep:

cat file.txt | grep -v "heh"

This would output the contents of file.txt but remove any lines with “heh” in them.

You can find out more about the various grep options in it’s man page (man grep from the command line)