|
Grep Fgrep Egrep
Using grep, fgrep, and egrep to search for strings of words The grep utility, which allows files to be searched for strings of words, uses a syntax similar to the regular expression syntax of the vi, ex, ed, and sed editors. grep comes in three flavors, grep, fgrep, and egrep, all of which I'll cover in this article. The name grep is derived from the editor command g/re/p, which literally translates to "globally search for a regular wxpression and print what you find." Regular expressions are at the core of grep, and I'll cover them after a brief description of some of the utility's command options. The simplest grep command is grep (search pattern) (files list), as in: grep hello *
The output of this command might be something like this:
$ grep hello * story.txt: so I said hello and she smiled back intro.txt: use the hello.c program as an example of C programming $ $ grep -i hello *
story.txt: so I said hello and she smiled back
story.txt: I could hear my echo, "HELLO."
intro.txt: use the hello.c program as an example of C programming
hello.c: printf("Hello, world. \n");
$
The output of grep varies depending on whether you're searching one or several files. If only one file is named on the command line, the output doesn't include the file name, as in the following example: $ grep -i hello hello.c
printf("Hello, world. \n");
$
$ grep -i hello *.c
printf("Hello, world. \n");
$
$ grep -il hello *.c hello.c: $ $ grep -il hello * hello.c: intro.txt: story.txt: $
$ grep -in hello *
hello.c:7: printf("Hello, world. \n");
intro.txt:44: use the hello.c program as an example of C programming
story.txt:110: so I said hello and she smiled back
story.txt:187: I could hear my echo, "HELLO."
$
$ grep -iv hello intro.txt You will be able to get more practice if you at its simplest $ $ grep -ic hello * data.txt:0 hello.c:1: intro.txt:1 intro2.txt:0 story.txt:2 $
So far I've covered some of the input and output options, but the real power of grep is in its search pattern, which uses regular expressions. grep can match simple strings, as we saw in the "hello" example we played with above; but it can also use a variety of wild cards and special symbols to create a regular expression to search for more complex strings. I will begin with some of the simpler characters in a regular expression. A ^ (caret) character means the start of a line and a $ (dollar) character means the end of one. The wild cards used by grep frequently clash with the special symbols that the shell uses, so the usual practice is to enclose complex search strings within single quotes. The two following examples would match any case version of "hello" at the start and end of a line, respectively. $ grep '^hello' * $ grep 'hello$' * $ grep '.ello' * $ grep '[hcj]ello' * $ grep '[b-d]ay' * $ grep '[^b-d]ay' * Any single character match (including a single character matched by a option/range specification) can be repeated by using the asterisk character (*). An asterisk following a single character means "zero or more occurrences" of the preceding match. The following search requests any line containing "hello" followed by "dolly" where the words are separated by zero or more spaces. Note that the asterisk follows the space after "hello" and therefore applies to the space character. $ grep 'hello *dolly' * hellodolly hello dolly hello dolly $ grep 'c[aeiou]*t' somewords.txt cat coat coot cot cout cut ct $ |