Last Updated: 23 Jun 2009

   |   

Author: dordal

Quick Introduction to grep/egrep/fgrep

grep is a command line utility that lets you search for certain patterns in files, and pull out the lines that match said patterns. grep supports a limited set of regular expressions, but egrep (it's cousin) supports a more complete set. fgrep (the other cousin) is grep without regular expression support, making it much faster. Realistically, you'll almost certainly want to use egrep or fgrep, not normal grep.

Standard grep/egrep/fgrep output isn't terribly useful, so you'll want to specify some options to make it more so:

egrep -rin regexp files

That says use egrep recursively, ignore case, and print line numbers with the output.

grep usually uses a POSIX style regular expression parser, but may not always, depending on your platform. Modern versions (GNU grep 2.5.1 and greater) can use Perl-style regular expressions with the -P flag.

grep -v will get all lines that don't match your regular expression, which can frequently be useful.

grep is particularly useful for parsing log files (e.g. apache logs, etc.). Angie of ShowMeAnalytics has written a great article on using grep and other tools to parse log files.

Discussion

Enter your comment. Wiki syntax is allowed: