command [options] expression [file] ...I have found these Unix commands to be very useful when programming. Suppose I had a C program with a number of subroutines and a global variable labeled chuck_wivell. Suppose further that Chuck found out about this and didn't like it. Of course I would change it immediately.
egrep chuck_wivell *.cwould give me a list of all source files where the offensive variable manifested itself. By placing a -n option in the command line I could also obtain the line numbers of the offenses.
The wildcard characters that grep handles are
\ [ ] . ^ * $and a delimiter used to mark the beginning and end of an expression. Delimiters are necessary only if the expression contains blanks or wildcard characters. Here are a few examples to help solidify this potential mumbo-jumbo:
grep 'Nostalgia is not what it used to be' fft.csearches through the file fft.c for the expression Nostalgia is not what it used to be.
The wildcard character "." matches any character. Therefore,
grep 'eur.' fft.cwould find eureka, amateur, chauffeur, etc... in the file fft.c.
Characters placed inside square brackets are each compared when searching.
grep '[cm]an' fft.cwould find any words with the sequence can or man, but would not locate sequences like ran or and.
Preceding a wildcard character by a "\" turns off the wildcard character feature and the character is treated normally, i.e., the expression eddie\. would yield all occurrences of eddie. but would not yield eddies or eddieboy.
Here are some useful options for all three of the greps:
-f Matches all the expressions in a given file as opposed to the one typed in the command line. -i Removes case sensitivity so that uppercase and lowercase letters match. -n Displays the line numbers containing a match. -l Displays the names of the files that contain a match but not the lines that contained a match. -v Displays the lines that don't match as opposed to those that do.
© 1993-1998 Christopher C. Taylor