->Title Page
->Intro
->Unix
-->Overview
-->File Structure
-->Online Manual
-->Basic Commands
-->Additional Commands
--->alias
--->ap
--->biff
--->chmod
--->compress/uncompress
--->df
--->diff
--->du
--->echo
--->find
--->finger
--->ftp
->grep
--->history
--->kill
--->look
--->mail
--->ps
--->script
--->setenv
--->source
--->spell
--->tar
--->telnet
--->umask
--->who
--->Miscellaneous
-->Login files
-->Special characters
-->Miscellaneous Tips
-->Things to try
->Vi editor
->Mirror sites
->Further reading

[ Up ]
[Prev][Home][Next]
[Author]

Unix ---> grep

The grep (global regular expression print) program searches for an expression in a file or group of files. There are three versions: grep, egrep (extended grep), and fgrep (fixed-string grep). The grep program expands wildcard characters in the given expression. The egrep program searches for the expression including alternations. The fgrep program searches for fixed-strings only and does not expand wildcard characters. The egrep program has more sophisticated internal algorithms, and is usually faster than grep or fgrep. The syntax for all three versions is:


    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 *.c
would 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.c
searches 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.c
would find eureka, amateur, chauffeur, etc... in the file fft.c.

Characters placed inside square brackets are each compared when searching.


    grep '[cm]an' fft.c
would 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.

Site Statistics

Find this site useful? Want to give something back?

t a y l o r@l i n u x a v e.n e t

© 1993-2000 Christopher C. Taylor