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.