Jump to content
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
NickTheGreek

What is grep, and how do I use it?

Recommended Posts

 

 

Knowledge Base

 

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 the Knowledge Base. 
Last modified on 2017-05-16 11:52:15.

Contact us

For help or to comment, email the UITS Support Center.

 

https://kb.iu.edu/d/afiy

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×