How to use grep

grep is a unix command that allows you to search for a pattern in a list of files. For more information on using a Unix environment, see the Unix Tutorial. For information on using regular expressions (another way to search for words) in your searches, see Using regular expressions in the CQP Tutorial

You use grep in the following manner:
grep pattern file-name1 file-name2

Here is an example:
bash % grep 'quite the' /usr/local/wsj/1994/*

/usr/local	- this is a directory in which some corpora are stored
wsj 		- the Wall Street Journal corpus
1994  	        - the year 1994 … we also have 1995 and 1996
*               - the star in this case is saying that you want the search to be conducted 
                  over ALL the files found in /usr/local/wsj/1994

Notice that we put the words we were searching for together in single quotes. This is the way we search for a string with grep. This is quite different from the way we search for two adjacent words using cqp (for more about cqp, see the CQP Tutorial.)

results - you will get all the lines (including the file from which it came) containing this search string:

ws940802:quite the opposite. Daily trading volume in mortgage pass-through securities on
ws940818:owner's life cycle, which is quite the reverse of a butterfly's: The owner
ws940818:tell quite the whole tale. Over and above that sum, Mr. Braman had transferred
ws940914:unique to Judaism the way the Israel cause was. And none have had quite the
ws941005:may never be quite the same. The shooting confirmed growing fears that
ws941024:Service Insurance System, or GSIS -- argues quite the opposite: Political links
ws941027:quite the reverse of VW's: Because of strong demand for its cars, factories are
ws941101:does not make government more popular -- quite the contrary.
ws941102: But the gun lobby's opposition no longer is quite the powerful political

These results will print to the screen. If you want to save your search to a file, you can redirect the information in the following way:

grep pattern file-name1 file-name2 > results-file

Here is an example:
bash % grep 'quite the' /usr/local/wsj/1994/* > quite_the

Here, we saved the results to a file which we named "quite_the" (the name makes it easy for us to find it later). We can then open the file and look at it either using emacs, more, less, etc. For more information on how to use these, and other important ways of using your account to the fullest, look at the Unix Tutorial . Click on the reference manual for quick answers to questions, or read the whole tutorial for a more complete introduction.

Some options:
grep gives us options that we can use with it. Here's a list of some of the most useful:

-h	- if you search more than one file at a time, the results contain the name of the file
	  from which the string was found.  (See the example using 'quite the').  This option
	  turns off that feature, giving you only the lines without the file name. 
-n	- precedes each line with the line number where it was found
-i	- tells grep to ignore case so that it treats "the" and "The" as the same word
-l	- displays a list of files that contain the string
-w	- restricts the search to whole words only
The option (always preceded by a "-") goes between the grep command and the search pattern.

grep -option pattern file-name1 file-name2 > results-file

Here is an example:
bash % grep -h 'quite the' /usr/local/wsj/1994/*

the results then look like this:

quite the opposite. Daily trading volume in mortgage pass-through securities on
owner's life cycle, which is quite the reverse of a butterfly's: The owner
tell quite the whole tale. Over and above that sum, Mr. Braman had transferred
unique to Judaism the way the Israel cause was. And none have had quite the
may never be quite the same. The shooting confirmed growing fears that
Service Insurance System, or GSIS -- argues quite the opposite: Political links
quite the reverse of VW's: Because of strong demand for its cars, factories are
does not make government more popular -- quite the contrary.
But the gun lobby's opposition no longer is quite the powerful political

You can use multiple options like this:

bash % grep -hi 'quite the' /usr/local/wsj/1994/*

etc... You can keep adding them up (but only use one "-" for all the options)