NickTheGreek 160 Report post Posted August 23, 2018 INDIANA UNIVERSITY 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? Related documents In Unix, what is the man command, and how do I use it to read manual pages? 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 Quote Share this post Link to post Share on other sites