USING THE grep UTILITY

[This was originally published by Mike Brooks as a wiki entry and I have moved it here – Wolf]
USING THE grep UTILITY

Since the acquisition of certain data is many times critical, the grep utility is a valuable tool for the Linux user. It allows you to search through one or more files to see whether any contain a specified string of characters. This utility does not change the file it searches but simply displays each line that contains the string.

Originally, the name grep was a play on an ed— an original UNIX editor, available on Fedora/RHEL— command: g/re/p. In this command g stands for global, re is a regular expression delimited by slashes, and p means print.

The following is an example of the proper syntax.

grep ‘string you want to search for’ file (name of file you want to search)

So, let’s say you were looking for the name of a person in a memo so you could replace that name with another without retyping the entire memo or document, a simple example could be:

grep ‘Halton’ thankyou.txt

This would return the specified name which could then be changed to the next name on the list of professors you wanted to thank for their contribution and influence on your education.

Options available for this command are extensive here are a few:

-H print the filename for each match

-I Ignore case

-l print only names of FILEs containing matches

-n print line number with output lines

-o only matching; show only the part of the line matching PATTERN

-w match only whole words

-x force PATTERN to match only whole lines

To see a complete listing of the options and a more complete detailed explanation of this utility it can be obtained by typing the following in the command prompt in Linux terminal.

$ man grep

(Mike Brooks)

Leave a Reply