Grep is a command-line utility for Unix used for searching text documents. It can perform basic searches and advanced pattern matching using regular expressions. The grep utility is included with all major Unix distributions, including Linux and Mac OS X.
Grep can be run by simply typing grep at the command prompt. The command requires specific parameters, including the string to be located and the filename of the file to be searched. For example, if you want to check if the file ” readme.text ” has the word “license” in it, you would type the following command:
grep license readme.txt
If the word is not found, grep will not produce any output. If the string is located in the document, grep will list each occurrence of the string. If you need to perform a more complex search, you can use a regular expression. For example, the command below will search the file ” us.txt ” and list all lines that begin with “Me” or end with “you” (case-sensitive).
grep -E “^Me|you$” us.txt
The “-E” parameter in the example above indicates the pattern is an “extended regular expression.” By using this option, you can make sure grep processes your search pattern as a regular expression instead of a basic string. Grep also supports several other command-line options, which can be used to create more specific queries and customize the output. You can view these options and learn more about grep by typing ” man grep at the Unix command prompt.
NOTE: The name “grep” is an acronym that comes from the phrase, “Globally search a regular expression and print.”