grep
Master the Grep command for efficient text searching in files. Learn useful options and regular expressions to find patterns, count matches, and more. Free online tool.
Grep Command
Grep: Powerful Text Searching in Files
The grep
command is a fundamental utility in Unix-like operating systems used for searching plain-text data sets for lines that match a regular expression. It's an indispensable tool for developers and system administrators to quickly find specific information within files.
The basic syntax for grep
is:
grep [options] <pattern> <filenames>
grep
can be used to search in a single file or in multiple files, making it highly versatile for various tasks.
Useful grep
Options
grep
offers a wide array of options to refine your searches. Here are some of the most commonly used:
Option | Description |
---|---|
-i |
Ignore case distinctions in both the pattern and the input files. |
-n |
Prefix each line of output with the 1-based line number within its input file. |
-v |
Invert the sense of matching, to select non-matching lines. |
-c |
Suppress normal output; instead print a count of matching lines. |
-r or -R |
Recursively search all files under each directory, following symbolic links only if they are on the command line. |
-l |
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. |
-o |
Show only the part of a line that matches the pattern. |
-I |
Process a binary file as if it did not contain matching data; this is equivalent to the --binary-files=without-match option. |
-A <num> |
Print <num> lines of trailing context after matching lines. |
-B <num> |
Print <num> lines of leading context before matching lines. |
-C <num> |
Print <num> lines of output context. Equivalent to -A <num> -B <num> . |
grep
Option Examples
Let's consider a demo file named demo.txt
with the following content:
THIS IS UPPER CASE LINE
this is lower case line
This is regular line
This line is also regular
Line number four
Line #5
last line
- Search for a string in a file:
grep "this" demo.txt
- Search for a string in a file, ignoring case:
grep -i "this" demo.txt
- Search for a string and display line numbers:
grep -n "this" demo.txt
- Count the number of lines matching the searched string:
grep -c "this" demo.txt
- Get the filename(s) containing the searched string:
grep -l "this" demo.txt
- Get 2 lines after the matching line:
grep -A2 "This" demo.txt
- Get 2 lines before the matching line:
grep -B2 "This" demo.txt
- Get 2 lines before and after the matching line:
grep -C2 "This" demo.txt
- Search recursively in all files in subdirectories, ignoring case and binary files:
grep -inrI 'some text' /path/to/dir
Regular Expressions in grep
A regular expression (regex) is a sequence of characters that defines a search pattern. grep
uses these patterns to find matching lines. Understanding regex is key to unlocking the full power of grep
.
Character/Pattern | Description |
---|---|
[abc] |
Matches any single character within the brackets (e.g., 'a', 'b', or 'c'). |
[a-d] |
Matches any single character within the specified range (e.g., 'a', 'b', 'c', or 'd'). |
^start |
Matches the pattern only if it appears at the beginning of a line. |
end$ |
Matches the pattern only if it appears at the end of a line. |
[^abc] |
Matches any single character that is NOT present within the brackets. |
[^a-d] |
Matches any single character that is NOT present within the specified range. |
. |
Matches any single character (except newline). |
* |
Matches zero or more occurrences of the preceding character or group. |
.* |
Matches zero or more of any character. |
Regular Expression Examples
- Match lines containing "This" or "this":
grep "[Tt]his" demo.txt
- Search for lines starting with "last":
grep "^last" demo.txt
- Search for lines ending with "regular":
grep "regular$" demo.txt
- Search for lines containing any digit (0-9):
grep "[0-9]" demo.txt
- Search for lines NOT containing any digit:
grep "[^0-9]" demo.txt
- Search for lines where "line" is followed by any characters and then "regular":
grep "line.*regular" demo.txt
Note: grep
can effectively combine regular expressions in the search pattern with wildcards in the filenames to search section, offering immense flexibility.
For more in-depth information, refer to the official grep
man page and resources on regular expressions.