Indiana University
University Information Technology Services
What are archived documents?

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.


What is grep, and how do I use it?

The grep utilities are a family of Unix tools, including grep, egrep, and fgrep, that perform repetitive searching tasks. The tools in the grep family are very similar, and all are used for searching the contents of files for information that matches particular criteria. For most purposes, you'll want to use fgrep, since it's generally the fastest.

The general syntax of the grep commands is:

grep [-options] pattern [filename]

You can use fgrep to find all the lines of a file that contain a particular word. For example, to list all the lines of a file named myfile in the current directory that contain the word "dog", enter at the Unix prompt: fgrep dog myfile

This will also return lines where "dog" is embedded in larger words, such as "dogma" or "dogged". You can use the  -w  option with the grep command to return only lines where "dog" is included as a separate word: grep -w dog myfile

To search for several words separated by spaces, enclose the whole search string in quotes, for example: fgrep "dog named Checkers" myfile

The fgrep command is case sensitive; specifying "dog" will not match "Dog" or "DOG". You can use the  -i  option with the grep command to match both upper- and lowercase letters: grep -i dog myfile

To list the lines of myfile that do not contain "dog", use the  -v  option: fgrep -v dog myfile

If you want to search for lines that contain any of several different words, you can create a second file (named secondfile in the following example) that contains those words, and then use the  -f  option: fgrep -f secondfile myfile

You can also use wildcards to instruct fgrep to search any files that match a particular pattern. For example, if you wanted to find lines containing "dog" in any of the files in your directory with names beginning with "my", you could enter: fgrep dog my*

This command would search files with names such as myfile, my.hw1, and mystuff in the current directory. Each line returned will be prefaced with the name of the file where the match was found.

By using pipes and/or redirection, you can use the output from any of these commands with other Unix tools, such as more, sort, and cut. For example, to print the fifth word of every line of myfile containing "dog", sort the words alphabetically, and then filter the output through the more command for easy reading, you would enter at the Unix prompt: fgrep dog myfile | cut -f5 -d" " | sort | more

If you want to save the output in a file in the current directory named newfile, enter: fgrep dog myfile | cut -f5 -d" " | sort > newfile

For more information about grep, egrep, and fgrep, enter: man grep

At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?

This is document afiy in domain all.
Last modified on May 13, 2009.

I need help with a computing problem

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

I have a comment for the Knowledge Base

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.