Grep
lastmod 2026-02-06created 2015-01-2835 edits
grep is a command with text search functionality, made for Unix. Its name comes from g/re/p, a Unix ed command that performs a similar function.
Strictly speaking, grep is not an acronym (a word formed from initial letters), but it takes the initial letters of Global / Regular Expression / Print, which is a sequence of directives used in the ed text editor.
The grep command searches files or standard input for lines matching the given regular expression and prints them to the program's standard output.
Options
-
-c: Prints the number of lines matching the pattern. -
-n: Prints line numbers along with the lines. -
-i: Ignores case. -
-v: Prints lines that do not contain the searched content. (This option is used to remove specific content from the input file.) -
-l: Prints only the names of files where the pattern was found, instead of line numbers. -
-w: Finds the pattern only when it is a whole word, not part of another word. (Prints only lines containing the word north, not words like northwest or northeast.) -
"": When enclosed in double quotes, shell variables are substituted. -
-I: (uppercase i) Excludes binary files from the search. Same as thebinary-files=without-matchoption. -
-A10: Additionally prints the 10 lines after the matched pattern. 1 -
-B10: Additionally prints the 10 lines before the matched pattern.
Pattern Syntax
-
-E,--extended-regexp - Interpret PATTERNS as extended regular expressions (EREs).
-
-F,--fixed-strings - Interpret PATTERNS as fixed strings, not regular expressions.
-
-G,--basic-regexp - Interpret PATTERNS as basic regular expressions (BREs). This is the default.
-
-P,--perl-regexp - Interpret PATTERNS as Perl-compatible regular expressions (PCREs).
- This option is experimental when combined with the
-z(--null-data) option, and grep-Pmay warn of unimplemented features.
Examples
-
grep NW d*: Finds all lines containing NW in all files starting with d. -
grep '^n' [FILE]: Prints all lines starting with n. -
grep '4$' [FILE]: Prints all lines ending with 4. -
grep '.5' [FILE]: Prints all lines where .5 appears. -
grep '^[we]' [FILE]: Prints all lines starting with w or e. -
grep '[^0-9]' [FILE]: Prints all lines containing at least one non-digit character. -
grep '[A-Z][A-Z] [A-Z]' [FILE]: Prints lines containing a string of 2 uppercase letters, 1 space, and then one uppercase letter in a row. -
grep 'ss* ' [FILE]: Prints all lines containing a string where s appears once, followed by s zero or more times, and then a space. -
grep '[a-z]{9}' [FILE]: Prints all lines containing a string of 9 or more repeated lowercase letters. -
grep '<[a-z].*n>' [FILE]: Prints all lines containing a word that starts with one lowercase letter, followed by any number of characters, and ends with n (here .* means any characters, including spaces) -
grep -i "$LOGNAME" [FILE]: Prints all lines containing the value of the environment variable LOGNAME. -
grep -r "string" ./*: Finds all files containing string.
grep "com.androidclient" src -r | grep -v .svn: In the src directory, finds all files containing the string "com.androidclient" whose file names do not contain .svn. How to check for an exact match without using regex?
How do you make grep match only when the entire line matches?
From the grep man page:
-
-F,--fixed-strings - Interpret PATTERN as a (list of) fixed strings
-
-x,--line-regexp - Select only those matches that exactly match the whole line.
Negating a word
To find words other than "21":
Getting search results across multiple lines
It's complicated, so let me explain with an example...
Given the following content,
2023-12-22 14:04:44.576 1/140201166767936 ddrm.plc INFO Recv 2+100 bytes: b'{"lifecount":22308,"plateno":3718,"fronttemp":1,"reartemp":800,"passno":19,"camera":1,"event":"end"}'
2023-12-22 14:04:45.246 1/140201166767936 ddrm.plc INFO Send 2+76 bytes: b'{"type":"camber","event":"real","camera":1,"error":0,"data":{"value":-2336}}'
2023-12-22 14:04:45.246 1/140201166767936 ddrm.plc DEBUG on_recv_end(camera=1,lifecount=22308,passno=19,plateno=3718,fronttemp=1,reartemp=800)
2023-12-22 14:04:45.246 1/140201166767936 ddrm.cam1 INFO on_end(lifecount=22308,passno=19,plateno=3718,fronttemp=1,reartemp=800)
2023-12-22 14:04:45.246 1/140201166767936 ddrm.plc INFO Send 2+160 bytes: b'{"type":"camber","event":"event","camera":1,"error":0,"lifecount":22308,"data":{"cambers":[-21],"avg":-95,"min":-168,"max":-21}}'
2023-12-22 14:04:46.733 1/140201166767936 ddrm.plc INFO Send 2+74 bytes: b'{"type":"camber","event":"real","camera":1,"error":0,"data":{"value":216}}'
If you want to find the plateno value in ddrm.plc where, on the line following fronttemp=1, the first value of the cambers array is not 21:
Combine the -A1 and -B1 options:
cat cam1 \
| grep -v ddrm.cam1 \
| grep -A1 fronttemp=1 \
| grep -B1 -E '"cambers":\[([^2][^1])' \
| grep 'plateno='
How to join results from multiple lines
You can use the following tools:
- sed#How to join results from multiple lines
- awk#How to join results from multiple lines
- tr#How to join results from multiple lines
When using -A or -B, -- is added at the split points, so you can use it like this:
How to use Lookaround syntax
You must use Perl-compatible expressions with the -P flag.
A simple example:
Color output
grep is capable of color-highlighting the matched string in its output. But, by default, that option is turned off. There are 3 color options available to you:
-
--color=auto -
--color=always -
--color=never
Lastly, you can specify the color parameter in a grep-specific environment variable. Then, you don't have to enter it in the command line.
Similar commands
For the many operating systems in existence, the grep command has numerous supplementary and derived features. Early variants of grep included egrep and fgrep. The former provided the extended regular expression syntax developed after Ken Thompson's original regular expression implementation. The latter could search for a list of fixed strings using the Aho-Corasick algorithm. These variants have been incorporated into recent grep implementations, and most have become option features. Under such an integrated implementation, grep may perform different functions depending on the name it is invoked by, but egrep, fgrep, and grep all function as commands linked to the same program.
Tcgrep is an enhanced form of grep that uses the Perl language. Other commands containing the word 'grep' imply that they have search functionality. For example, pgrep shows a list of processes whose names match a given regular expression.
In Perl, grep is a built-in function that searches for elements in a list. In descriptive programming languages, such a function is usually named 'filter'.
DOS, OS/2, and Microsoft Windows-based systems provide commands with simple search functionality. Windows has a command called findstr, which is very similar to "grep" in functionality, or you can use a variant version of grep on Windows via cygwin. typically named "filter" instead.
- grep : Searches through entire files and prints all lines matching a regular expression.
- egrep : Called 'Extended grep', an extended version of grep that supports additional regular expression metacharacters. Supports the same command-line options as grep.
- fgrep : Called fixed grep or fast grep, it treats all characters literally. That is, it treats even regular expression metacharacters as ordinary characters.
egrep example
-
egrep 'NW|EA' [FILE]- Example that prints lines containing NW or EA. -
egrep '3+' [FILE]- Example that prints lines where the digit 3 appears one or more times. -
egrep '2.?[0-9]' [FILE]- Example that prints lines where the digit 2 is followed by zero or one period, and then a digit. -
egrep ' (no)+' [FILE]- Example that prints lines where the pattern no appears one or more times in a row. -
egrep 'S(h|u)' [FILE]- Example that prints lines where the letter S is followed by h or u. -
egrep 'Sh|u' [FILE]- Example that prints lines containing the pattern Sh or u.
Related text editing utilities
- grep: A utility for pattern searching.
- awk: A utility that can convert characters of a specific pattern into a desired format.
- sed: Short for 'Stream EDitor', a utility that can edit data without opening the file.
Text Processing Commands
|
See also
- ack
- Regular expression
- tr
- awk
- sed
- fgrep
- egrep
- zipgrep - zip file
- zgrep - gz file
- Krep - A string search tool 5 times faster than grep
- mgrep - A semantic grep-style CLI search tool for exploring code and documents in natural language
Favorite site
- GNU grep user manual
- Wikipedia (en) grep
- grep, egrep, fgrep
- [Recommended] Linux grep command 2
- [Recommended] grep, egrep, fgrep commands
- The Open Group Base Specifications Issue 7
- How to use the Linux grep command
- grep, egrep, fgrep features 3
- About egrep 4
References
-
Using grep to print lines containing a specific keyword + a few lines before and after ↩
-
Linux_grep_command.pdf ↩
-
Grep_egrep_fgrep.pdf ↩
-
Egrep.pdf ↩