Grep vs egrep
Here we searched for a word using its exact spelling. We can use the grep tool to search through the file for us in an automatic search: $ grep even file Using this method on a large file suffers because we could easily miss one word in a file of several thousand, or even several hundred thousand, words. Reading the file, we see that the file does indeed contain the letters “even”. For example, to see if a file contains the word “even”, you could just visually scan the file: $ cat file Under Linux, there are often several ways of accomplishing the same task.
Show All Occurrences of a String in a File Include two lines of context before and after the matched line. Search all files, recursing through directories. Instead, exit with a zero exit status if any match is found. Include line numbers along with matching lines. Print filenames that contain matching pattern only. (Helpful for specifying several alternate patterns.) Common Command Line Switches for the grep Command Switch Consult the grep man page (or invoke grep –help) for more information. The following table summarizes some of grep’s more commonly used command line switches. The pattern is expected to be a single argument, so if pattern contains any spaces, or other characters special to the shell, you must enclose the pattern in quotes to prevent the shell from expanding or word splitting it. The pattern argument supplies the template characters for which grep is to search.
What, you may well ask, is a regular expression and why on earth should I want to parse one? We will provide a more formal definition of regular expressions in a later post, but for now, it is enough to know that a regular expression is simply a way of describing a pattern, or template, to match some sequence of characters. The name grep stands for general regular expression parser. In this post, we introduce the grep program, a handy tool for searching text file contents for specific words or character sequences. In an earlier post, we saw how the wc program can be used to count the characters, words, and lines in text files.