How to Use the Grep Command
The grep command is a search command built into a variety of Unix based operating systems. This command line utility, whose name stems from the original Unix term which means “search globally for lines matching the regular expression, and print them,” can be accessed using the command line or terminal from anywhere in the Unix operating system. When given a particular list of files to search through, the grep command becomes especially useful, as it can search through any amount of text to find exactly what you are looking for. The corresponding lines are then outputted to the computer screen for easy access.
The grep command can be used in a variety of ways, from the most simplistic, usually only allowing for the location of the file and the term you are searching for, to highly complex, making use of a variety of operators and extra parameters.
Here is a basic grep command:
grep Hello letter.txt
This command will search the file, “letter.txt,” for all instances of the word “Hello.” It is important to note that the word “hello” will not be printed to the screen because the grep command is inherently case sensitive. This feature can be disabled by including the flag “-i” before the word you are searching for, as in the example below:
grep -i Hello letter.txt
Using a command like this will display all instances of the word “Hello” no matter where any capitalizations occur.
Here is another example of a grep command:
grep ‘^and’ mydocument.txt
Placing a caret before a word will make the Unix grep command only search for that word if it appears at the beginning of a column of text. This means that the grep command will not find any words such as “candy” or “band,” but will only find the word “and” as it appears by itself.
grep, fgrep, and egrep
The three search options available on Unix computers include grep, egrep, and fgrep. The general syntax used with a grep command is: grep -options pattern filename with options and filename being optional arguments to use with the command. GNU grep is the most popular version of the utility installed on computers running the LINUX operating system and it contains both a regex-directed and text-directed search engine.
The fgrep command can be used to find every line in a file that contains a word or phrase. For example, if you wanted to find every line in a file that had the phrase, “TopBits,” you would enter the following command at the computer’s command prompt:
fgrep TopBits myTestFile
When you use the fgrep command, it will return all matches for the phrase that you include as the search argument whether or not it is part of a bigger word or not. If you don’t desire these matches to be returned, then you can alternatively use the grep command with a -w switch to only return distinct matches for the search term in the file. For example,
grep -w TopBits myTestFile
will return only the matches in the file that are distinct words. If you want to find multiple words that are separated by a space, then you will need to enclose your search arguments in quotes. For example,
fgrep “Top Bits file management” myTestFile
will return all strings matching the information contained in the quotations. You can also use the “-v” switch with the fgrep command to list lines of a program that do not contain a given argument. To find all of the lines of a program that do not contain the phrase, “TopBits” the following command can be used:
fgrep -v TopBits myTestFile
Are Grep Searches Case Sensitive?
The short answer is yes. Unix and LINUX are both case sensitive operating systems. If you want to do a grep search on a file and don’t want to worry about matching case on your search, you can use the “-i” switch on the command to find all combinations of the argument in the file. For example,
grep -i TopBits myTestFile
will return all matches on the “TopBits” search argument independent of letter case in the phrase.
What Are the Available Grep Options?
There are a number of additional options available to use with the grep command beyond the “v, w, and i” switches. These include:
Conducts the search match using extended regular expressions.
Conducts the match using a fixed string where each pattern is specified as a string instead of a regular expression.
Outputs a count of the matched lines to the standard output of the computer.
-e “pattern list”
Uses one or more patterns to use for matching the defined input in the command. Each pattern included will be separated by a “newline”
-f “pattern file”
Reads one to many patterns from a file passed to the command with the patterns terminated or separated by a “newline.”
Will write only names of files matching the search argument to the standard output of the computer.
Lists the line number of the file prior to the matching information starting at the first line of the file. Line numbers reset with each new file searched.
Will output nothing to the standard output of the computer regardless of the expression matching the information in the file.
Suppresses errors that will normally output for unreadable files.
Will only look at input lines that make use of all characters in the line except for “newline.”