grep

grep is a Unix utility written by Ken Thompson. It is a powerful utility for searching patterns in files that match a regular expression. The name grep comes from the ed command g/re/p (globally search a regular expression and print), which has the same effect: doing a global search with the regular expression and printing all matching lines.

grep [option...] [patterns] [file...]
    -i or --ignore-case: Ignore case distinctions in both the pattern and input files.
    -r or -R or --recursive: Read all files under each directory, recursively.
    -n or --line-number: Prefix each line with the line number within its input file.
    -l or --files-with-matches: Suppress normal output; instead, print the name of each input file containing one or more matches.
    -v or --invert-match: Invert the sense of matching, to select non-matching lines
Some Examples:
#print all lines that contain some some string in file1 and file 2
grep 'some string' file1 file2

# print the lines containing  the word john and igonore cases i.e. (John, JOHN, john...)
grep -i 'john' /var/log/syslog

# print the lines containing  the word john and igonore cases i.e. (John, JOHN, john...)
grep -i 'john' /var/log/syslog

# search for 'exit 0' recursively on /var/log directory
grep -r 'exit 0' /var/log

# print lines containing the string 'hello' as a distinct word
grep -w 'hello' /var/log/syslog

# egrep (extended grep can be used) for searching many words at once or escaping the |
egrep -w "krishna|bhattarai|god" /var/log/syslog

# print n lines after the match
grep -A 3 "fun" playful.txt

# print n lines before the match
grep -B 3 "fun" playful.txt

# print n lines for before and after the match
grep -C 3 "fun" playful.txt

# Show everything that does not match the search query
arp -an | grep -v incomplete

# Print only the first line that matches the pattern
grep "Regular Expression" | head -n 1
Some more examples:
Search for a pattern in a file
grep "pattern" filename
Case-insensitive search
grep -i "pattern" filename
Display line numbers
grep -n "pattern" filename
Count the number of matches
grep -c "pattern" filename
Search for whole words only
grep -w "word" filename
Invert match (exclude lines containing a pattern)
grep -v "pattern" filename
Recursive search in subdirectories
grep -r "pattern" /path/to/directory
Display only matching part of lines
grep -o "pattern" filename
Display lines before and after match
grep -A 2 -B 2 "pattern" filename
Display line numbers and content
grep -n -e "pattern1" -e "pattern2" filename
Search for patterns in a compressed file
zcat filename.gz | grep "pattern"
Search for patterns in multiple files
grep "pattern" file1 file2 file3
Display lines that do not contain a pattern
grep -v "pattern" filename
Search for patterns in a command output
ps aux | grep "process_name"
Search for patterns in a specific file type
grep "pattern" --include=*.txt /path/to/files
Options:
Option Description
-i Ignore case
-v Show everything that does not match the search query
-o Only show matched string not the entire line
-r Recursive search [on all files in a directory]
-h Suppress filenames from output
-w Search for whole words
-c Print a count of matching lines
-l Print the name of each file that contains a match
-n Print the line number
-A N Print N lines after matching lines
-B N Print N lines before matching lines
-C N Print N lines before and after matching lines
-E Interpret pattern as an extended regular expression (ERE)
-F Interpret pattern as a fixed string (not a regular expression)
-q Quiet mode. Just return a status code, don't write anything to output
-m N Stop reading a file after N matching lines
-q Quiet mode. Just return a status code, don't write anything to output
-s Silent mode. Suppress error messages about nonexistent or unreadable files
-u Search for patterns that are not repeated
-x Consider only input lines that use whole words
--color=WHEN Colorize the output; WHEN can be 'never', 'always', or 'auto'
--exclude=PATTERN Skip files and directories matching PATTERN
--include=PATTERN Search only files that match PATTERN
--exclude-dir=DIRECTORY Exclude directories matching DIRECTORY from recursive search
Regular expressions:
Expression Description Example
. A single character gr.y matches "gray", "green", etc.
[abc] Any of these characters [aeiou] matches any vowel
[^abc] A character that is not one of the enclosed [^0-9] matches any non-digit
(abc) Group these characters and remember later (\d{3})-\d{2}-\d{4} matches "123-45-6789"
\n Replace n with a number \d{2,} matches two or more digits
| Logical or cat|dog matches "cat" or "dog"
\ Escape character \. matches a literal period
\d Any digit (equivalent to [0-9]) \d{3} matches "123"
\w Any word character (equivalent to [a-zA-Z0-9_]) \w+ matches one or more word characters
\s Any whitespace character \s{2} matches two consecutive whitespace characters
Regular expression Multipliers
Multiplier Description
? The preceding item is optional, it is matched zero or one times.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item will be matched exactly n times.
{n,} The preceding item will be matched n or more times.
{n,m} The preceding item will be matched between n and m times.
Regular expression Anchors
Anchor Description
^ The beginning of a line
$ The end of a line
\< The beginning of a word
\> The end of a word
\b A word boundary
References:
GNU Grep 3.0. (n.d.). Retrieved from https://www.gnu.org/software/grep/manual/grep.html
R. C. (2018). Linux Tutorial - Grep Cheat Sheet. Retrieved from https://ryanstutorials.net/linuxtutorial/cheatsheetgrep.php