What is grep?
Grep is a UNIX text search command used to search files or standard input for lines that match a regular expression and print them to standard output. The name comes from the phrase “global / regular expression / print”, which originates from early text editors such as ed.
There are countless implementations and variations of the grep program available for many operating systems. Some of the earliest variants of grep were egrep and fgrep.
Key Points
- Grep is a core UNIX command used to search files and standard input for lines matching regular expressions or fixed strings.
- The grep family includes grep, fgrep and egrep, each designed for different types of pattern matching and performance needs.
- Search and output behaviour can be customized using options such as -i, -v, -w, -n and -c.
- Grep becomes especially powerful when combined with regular expressions for advanced text processing.
The Global Regular Expression Print family allows searching for strings defined by regular expressions across a list of files. The result can be displayed directly or used as input for another UNIX command.
The three commands in the grep family are grep, fgrep and egrep. They differ in the generality of the search expression and search speed:
- grep searches for basic regular expressions, using a compact and non-deterministic algorithm.
- fgrep (Fast grep) searches for fixed strings, using a compact and fast algorithm.
- egrep (Extended grep) searches for extended regular expressions, using a fast and deterministic algorithm.
Typically, fgrep is used to search for fixed strings, while egrep is used for extended regular expressions. Standard grep is commonly used when grouped or repeated searches are required.
Regular expressions used by grep require escaping grouping characters using \( and \), allowing repeated group searches. In contrast, egrep does not require escaping grouping characters and does not support repeated group searches in the same way.
When working with command-line tools such as grep on servers, many users rely on VPS Apps Hosting to manage applications and system environments efficiently.
1. Grep command syntax
Basic syntax
The basic syntax of grep commands is as follows:
grep [options] string [file(s)]egrep [options] string [file(s)]fgrep [options] string [file(s)]
2. Options affecting search
The following options modify how the search is performed:
- -w – search by whole word.
- -v – select lines that do not contain the string.
- -i – treat uppercase and lowercase letters as equivalent.
3. Options affecting output
These options control how results are displayed:
- -c – display only the number of matching lines.
- -l – display only the names of files containing the string.
- -n – display file name and line number for each match.
- -s – suppress error messages (grep only).
4. Options supported by fgrep and egrep
- -e string – used to search for strings starting with a dash.
- -f file – read search patterns from a file.
These examples cover only a small subset of grep functionality. Grep becomes especially powerful when used with regular expressions, which require more experience. For more details, consult the grep manual page.
Regular expressions in grep
A regular expression is a sequence of characters where some characters have special meaning. The backslash (\) removes the special meaning of a character, while quoting prevents interpretation by the shell.
Characters of special significance
. – any character
[characters] – any character in the list
[c1-c2] – any character between c1 and c2
[^characters] – negation of the character set
^ – beginning of line
$ – end of line
\< – beginning of a word
\> – end of a word
* – repeat previous expression zero or more times
\{n\} – repeat exactly n times
\{n,\} – repeat at least n times
\{n,m\} – repeat between n and m times
\(expr\) – grouped expression
\n – reference to a matched group


