next up previous contents
Next: Listing the files in Up: Files and the File Previous: Viewing a file, one   Contents

Searching a file (grep)

Often you will want to find a certain string in a file, but you do not want to spend your time looking through the whole file. The command grep will search through a file for the string you want and then print the line(s) that the string happens to be in. A word of caution: grep is very particular; you must make the distinction between upper and lower case letters. You can force grep to ignore case distinctions using the -i option. To look for multi-word strings enclose the string in double quotes. The format for the command is:

grep ``string'' filename

Suppose you wished to find the word ``boulevard'' in the file ``sharky'', you would type the following command:

malama% grep boulevard sharky
Disappearing behind the boulevard
malama%

You may use grep to search for a string in a whole group of files. To look in more than one file use the wild card character ``*'' (to find out about wild cards see the section in this file on Commands). The format is:

grep string *

grep will search through all the files in your current directory looking for each occurrence of the specified string.

Note that the format for grep is the reverse of its counterpart in VMS, the command search (the format for search is ``search filename `string' '').

To make more complicated searches, such as matching more than one expression, you will probably have to use the other commands in the grep family, egrep and fgrep. For more details on their usage, check the on-line man pages.

In case you are wondering where the name ``grep'' comes from, it is short for ``get regular expression and print''. There are two other versions of ``grep'', one faster but more restrictive in allowed expressions fgrep, one more extensive egrep.


next up previous contents
Next: Listing the files in Up: Files and the File Previous: Viewing a file, one   Contents
h.stilmack@jach.hawaii.edu