Paring Down What the Search Finds

Problem

Your search is returning way more than you expected, including many results you don’t want.

Solution

Pipe the results into grep -vwith an expression that describes what you don’t want to see.

Let’s say you were searching for messages in a log file, and you wanted all the messages from the month of December. You know that your logfile uses the 3-letter abbreviation for December as Dec, but you’re not sure if it’s always written as Dec,so to be sure to catch them all you type:

...
error on Jan 01: not a decimal number
error on Feb 13: base converted to Decimal
warning on Mar 22: using only decimal numbers
error on Dec 16 : the actual message you wanted
error on Jan 01: not a decimal number
...

A quick and dirty solution in this case is to pipe the first result into a second grep and tell the second grep to ignore any instances of “decimal”:

$ grep -i dec logfile | grep -vi decimal

It’s not uncommon to string a few of these together (as new, unexpected matches are also discovered) to filter down the search results to what you’re really looking for:

$ grep -i dec logfile | grep -vi decimal | grep -vi decimate

Discussion

The “dirty” part of this “quick and dirty” solution is that the solution here might also get rid of some of the December log messages, ones that you wanted to keep—if they have the word “decimal” in them, they’ll be filtered out by the grep-v.

The -v option can be handy if used carefully; you just have to keep in mind what it might exclude. ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.