Taking the time to learn grep is one of the smartest things you can do, think of grep as the best personal assistant you'll ever have and it can find anything for you, if only you know how to ask. With a little bit of help from the interwebs, Google and even a couple of real life books you'll be grepping like you've never grepped before.
What is grep?
grep searches for whatever you ask it and shows the results on the screen.
grep is command line search utility that was originally written for Unix. It's name actually comes from the words global / regular expression / print (pretty geeky hey). The grep command will search files for lines that have the search term (or regular expression) that you supply, it then prints them to the standard output.
How to grep with a computer
If you're on a unix/linux computer or an apple mac (osx) then you can enter grep commands in the terminal. Assuming you've got a terminal window open all you have to do is ask grep to find you something.
Lets say you have a file called fruit-list.txt and you want to know if the word apple is in that list. The command you'd use to achieve this would look something like this.
grep apple fruit-list.txt
Pretty simple right? grep is the command, apple is the search term, and fruit-list.txt is the file you'd like to search in. Grep would print all the lines containing apple from the file fruit-list.txt. grep will also show you results for pineapple or apples becuase the word apple is contained in those words. By default grep is case sensitive so a search for apple will not include Apple (note the capital A).
What if you don't care about case? What if you want apple, APPLE and Apple to count as a match when you search. That's simple too, just add the -i argument to print all lines containing apple regardless of capitalization. This is called a "case insensitive" search or "ignoring case".
grep -i apple fruit-list.txt
If you're wanting to grep for the word apple only, without matching words like pineapple or apples then you can use the -w argument. (Note: This command will still match words with hyphens (-) such as apple-fruit). If you'd like to ignore case at the same time you can include the -i argument in the same command.
grep -wi apple fruit-list.txt
If you're familiar at all with man (which is short for manual) you can type man grep at your command line or check out a grep manual on the interwebs. This will give you detailed information about grep and all of it's arguments, as well as regular expressions and environment variables.
A couple of arguments I use from time to time include:
-R which does a recursive match (used when searching multiple files)
-H which prints the filename for each match
If you'd like to search all the files (-R) from your present working directory (*), for the word apple ('apple'), showing the filenames for each match (-H), your command would look like this.
grep -RH 'apple' *
As soon as you start to delve into advanced grepping (that's the fun stuff) you'll want to use regular expressions. I won't go into too much detail in this blog post, regular expressions really deserves a blog post of it's own. If you really want to get your hands dirty with grep I'd suggest checking out regular-expressions.info. You'll want to read their tutorial and quick start pages then after you've had enough theory check out the examples, I've found them to be the best regular expression buddy on the whole of the interwebs.
How to grep without a computer
This is usually done by purchasing this thing called a book (no not the type you download, an actual physical book, and yes they still exist). While there's nothing wrong with learning online, and reading e-books there's just something about having a real life book in front of you that computers can quite replace.
A great book to get you started with grep is O'Reilly's grep pocket reference book. They go through the basics and give you a great footing into the fundamentals of grep.
grep pocket reference starts by giving you a conceptual overview (I only get things when I can conceptualize them), runs you through regular expressions, some grep basics, extended regular epressions, prel-style regular expressions and some advanced tips and tricks.
Because the book is dedicated to grep you won't have to wade through pages of irrelevent content, and you're not paying for anything you wont use.
Once you go in-depth into unix commands like grep you'll really want to master regular expressions. While it's great to have the interwebs at your finger tips it's in nice to get away once in a while and enjoy the fresh air, or maybe even your couch.
There are a couple of regular expression books that you might find handy. A couple that I recommend are "Regular Expression Pocket Reference" which is a practical reference book for Perl, Ruby, PHP, Python, C, Java, and .NET (so you're pretty much covered on all bases). If you're wanting to get serious with regular expressions you should checkout "Mastering Regular Expressions" and finally we have "Regular Expressions Cookbook". Details of all three books are below so you can find out more about each book.