LFCS 23 ๐ง Searching Content โ grep in Depth
The earlier chapter introduced grep as a line-oriented filter. This chapter goes deeper. The basic options โ -i, -v, -c, -n, -l, -q, -r โ are the foundation, but the real power of grep is in the regular expressions and the options that control the matching, the output, and the file selection. The LFCS exam tests the command as part of the “Search files and content” objective, and the depth is the difference between the basic searches and the searches that solve the real problems. The grep variants โ egrep, fgrep, and the -E and -F flags โ the context options, the recursive search with the file filters, and the -P for the PCRE are the tools that separate the beginner from the administrator.
The regular expressions are the core. The basic regular expressions (BRE) are the default, and the extended regular expressions (ERE) are enabled with the -E flag or the egrep command. The difference is the metacharacters: in the BRE, the +, the ?, the |, the (, and the ) are the literal characters, and the escaped forms (\+, \?, \|, \(, \)) are the special. In the ERE, the same characters are the special, and the escaped forms are the literal. The -P flag enables the Perl-compatible regular expressions (PCRE), which adds the \d, the \w, the lookahead, and the other features. The LFCS exam expects the knowledge of the BRE and the ERE, and the -P is the bonus.
The other depth is the file selection. The -r and the -R recurse into the directories, and the --include and the --exclude filter the files. The -l and the -L list the files with and without the matches. The -c counts the matching lines, and the -o prints only the matched part. The context options โ the -A, the -B, and the -C โ show the surrounding lines, which is essential for the log analysis. The -q is the silent mode for the scripts, and the -s suppresses the error messages. The combination of these options is the difference between the simple search and the precise one.
This chapter covers three areas. First, why the depth matters โ the regular-expression dialects and the file-selection problem. Second, the regular expressions โ the BRE, the ERE, the -P, the anchors, the character classes, the quantifiers, and the backreferences. Third, the file selection and the output control โ the recursive search, the include and exclude, the context lines, the -o and the -c, and the practical patterns. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the regular-expression matching.
Key point: grep searches the input for the lines matching a pattern. The default pattern is a basic regular expression (BRE). The -E flag or the egrep command enables the extended regular expressions (ERE), where the +, the ?, the |, the (, and the ) are the special characters. The -P flag enables the Perl-compatible regular expressions (PCRE). The -r recurses into the directories, and the --include and the --exclude filter the files. The -A, the -B, and the -C show the context lines.
Why the depth matters
The regular-expression-dialect problem. The grep default is the BRE, and the grep -E is the ERE. The two dialects have the different metacharacters, and a pattern that works in one may not work in the other. The + in the BRE is the literal plus, and the \+ is the one-or-more quantifier. In the ERE, the + is the quantifier, and the \+ is the literal. The confusion is the common source of the bugs, and the understanding of the dialects is the key to the correct patterns .
The PCRE problem. The -P flag enables the Perl-compatible regular expressions, which add the \d for the digit, the \w for the word character, the \s for the whitespace, the lookahead, the lookbehind, and the other features. The PCRE is not the POSIX standard, and the -P is the GNU extension. The portability is the trade-off: the PCRE patterns work on the GNU grep, but not on the BSD or the busybox versions.
The recursive-search problem. The -r flag recurses into the directories, and the --include and the --exclude filter the files. The grep -r 'pattern' /etc searches every file under /etc, which is slow and noisy. The grep -r --include='*.conf' 'pattern' /etc searches only the configuration files, which is fast and precise. The file selection is the difference between the useful and the unusable.
The context problem. The matching line is often not enough. The error in the log is preceded by the request and followed by the stack trace. The -A, the -B, and the -C show the after, the before, and the both, and the context is the difference between the log line and the diagnosis.
The output-control problem. The -o prints only the matched part, which is the way to extract the strings like the email addresses or the IP addresses. The -c counts the matching lines, and the -l lists the files. The -q is the silent mode for the scripts, and the exit status is the answer. The combination of the options is the way to tailor the output to the task.
The trade-off. The grep is fast and universal, but the regular expressions are the limited subset of the PCRE. The awk and the perl are the full languages, and the ripgrep is the modern alternative with the better defaults. The grep is the POSIX tool, the universal, and the one that the LFCS exam tests. The trade-off is between the simplicity and the portability of the grep and the power of the full languages.
a. The regular expressions
The anchors. The ^ matches the start of the line, and the $ matches the end. The ^$ matches the empty lines.
grep '^error' log.txt # the lines starting with error
grep 'error$' log.txt # the lines ending with error
grep '^$' log.txt # the empty lines
grep -v '^#' file.conf # the non-comment lines
The character classes. The . matches any single character. The [abc] matches one of the a, b, or c. The [^abc] matches any character except the a, b, or c. The [a-z] matches the range. The POSIX classes are the [[:alpha:]], the [[:digit:]], the [[:alnum:]], the [[:space:]], the [[:upper:]], the [[:lower:]], and the [[:punct:]].
grep 'a.c' file.txt # a, any char, c
grep '[aeiou]' file.txt # any vowel
grep '[^0-9]' file.txt # any non-digit
grep '[[:digit:]]' file.txt # any digit
grep '[[:upper:]]' file.txt # any uppercase
The quantifiers. The * matches zero or more of the previous. The \? (the BRE) or the ? (the ERE) matches zero or one. The \+ (the BRE) or the + (the ERE) matches one or more. The \{n\} (the BRE) or the {n} (the ERE) matches exactly n.
grep 'ab*c' file.txt # the BRE: the a, the b's, the c
grep -E 'ab?c' file.txt # the ERE: the abc or the ac
grep -E 'ab+c' file.txt # the ERE: the ab, one or more b's, the c
grep -E 'a{3}' file.txt # the ERE: exactly three a's
grep -E 'a{2,4}' file.txt # the ERE: two to four a's
The alternation. The \| in the BRE and the | in the ERE match the either-or.
grep 'cat\|dog' file.txt # the BRE: the cat or the dog
grep -E 'cat|dog' file.txt # the ERE: the same
The grouping. The \( and the \) in the BRE and the ( and the ) in the ERE group the subexpressions. The groups are the way to apply the quantifiers to the multiple characters.
grep '\(ab\)\+' file.txt # the BRE: the ab repeated
grep -E '(ab)+' file.txt # the ERE: the same
grep -E '(cat|dog)s' file.txt # the ERE: the cats or the dogs
The backreferences. The \1 to the \9 refer to the captured groups. The pattern \(a\)\1 matches the aa, and the \(ab\)\1 matches the abab.
grep '\([a-z]\)\1' file.txt # the repeated letters
grep -E '(\w+)\s+\1' file.txt # the repeated words
The PCRE. The -P flag enables the Perl-compatible regular expressions, which add the \d for the digit, the \w for the word, the \s for the whitespace, the \b for the word boundary, the lookahead, and the lookbehind.
grep -P '\d{3}-\d{4}' file.txt # the phone number
grep -P '\bword\b' file.txt # the word boundary
grep -P 'foo(?=bar)' file.txt # the lookahead
grep -P '(?<=foo)bar' file.txt # the lookbehind
The -P is the GNU extension, and it is not available on the BSD or the busybox versions. The portability is the trade-off.
b. The file selection
The recursive search. The -r flag recurses into the directories, and the -R follows the symbolic links. The -r is the common choice, and the -R is the special case.
grep -r 'pattern' /etc
grep -r --include='*.conf' 'pattern' /etc
grep -r --exclude='*.log' 'pattern' /var
grep -r --exclude-dir='.git' 'pattern' .
The --include and the --exclude filter the files by the glob, and the --exclude-dir skips the directories. The patterns can be repeated, and the --include is the whitelist while the --exclude is the blacklist.
The file list. The -l lists the files that contain the match, and the -L lists the files that do not. The -l is the way to find the files, and the -L is the way to find the missing.
grep -l 'pattern' *.conf
grep -L 'pattern' *.conf
grep -rl 'pattern' /etc
The file-name argument. The grep accepts the multiple files, and the -- separates the options from the file names. The file names that start with the - need the -- or the ./ prefix.
grep 'pattern' file1.txt file2.txt
grep 'pattern' -- -file.txt
grep 'pattern' ./-file.txt
The -f file. The -f reads the patterns from a file, one per line. The multiple patterns are the OR.
grep -f patterns.txt file.txt
grep -f patterns.txt -r /etc
c. The output control
The line numbers. The -n prefixes the line with the line number, and the -b prefixes with the byte offset. The -H forces the file name prefix, and the -h suppresses it.
grep -n 'error' log.txt
grep -b 'error' log.txt
grep -H 'error' log.txt
The count. The -c prints the number of the matching lines, not the number of the matches. The -m N stops after the N matches.
grep -c 'error' log.txt
grep -m 5 'error' log.txt
The only-matching. The -o prints only the matched part, one match per line. The -o is the way to extract the strings.
grep -o '[a-z]*@[a-z]*\.[a-z]*' file.txt
grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' file.txt
The context. The -A N shows the N lines after the match, the -B N shows the N lines before, and the -C N shows the N lines on both sides. The context is the essential for the log analysis.
grep -A 3 'error' log.txt
grep -B 3 'error' log.txt
grep -C 3 'error' log.txt
The color and the binary. The --color=auto highlights the matches, and the -I ignores the binary files. The --line-buffered is for the streaming.
grep --color=auto 'error' log.txt
grep -I 'pattern' /etc
tail -f log.txt | grep --line-buffered 'error'
The silent mode. The -q suppresses the output and exits with the status 0 on the first match. The -s suppresses the error messages. The two are for the scripts.
if grep -q 'pattern' file.txt; then
echo 'found'
fi
grep -s 'pattern' missing.txt
Complete Example Session
# ============================================
# PART 1: THE BASIC REGULAR EXPRESSIONS
# ============================================
grep '^error' log.txt
grep 'error$' log.txt
grep '^$' log.txt
grep -v '^#' file.conf
# ============================================
# PART 2: THE CHARACTER CLASSES
# ============================================
grep 'a.c' file.txt
grep '[aeiou]' file.txt
grep '[^0-9]' file.txt
grep '[[:digit:]]' file.txt
grep '[[:upper:]]' file.txt
# ============================================
# PART 3: THE QUANTIFIERS
# ============================================
grep 'ab*c' file.txt
grep -E 'ab?c' file.txt
grep -E 'ab+c' file.txt
grep -E 'a{3}' file.txt
grep -E 'a{2,4}' file.txt
# ============================================
# PART 4: THE ALTERNATION AND THE GROUPING
# ============================================
grep 'cat\|dog' file.txt
grep -E 'cat|dog' file.txt
grep -E '(cat|dog)s' file.txt
grep -E '^(error|warning):' log.txt
# ============================================
# PART 5: THE BACKREFERENCES
# ============================================
grep '\([a-z]\)\1' file.txt
grep -E '(\w+)\s+\1' file.txt
# ============================================
# PART 6: THE PCRE
# ============================================
grep -P '\d{3}-\d{4}' file.txt
grep -P '\bword\b' file.txt
grep -P '(?<=foo)bar' file.txt
# ============================================
# PART 7: THE RECURSIVE SEARCH
# ============================================
grep -r 'pattern' /etc
grep -r --include='*.conf' 'Listen' /etc
grep -r --exclude='*.log' 'error' /var
grep -r --exclude-dir='.git' 'TODO' .
# ============================================
# PART 8: THE FILE SELECTION
# ============================================
grep -l 'pattern' *.conf
grep -L 'pattern' *.conf
grep -rl 'pattern' /etc
# ============================================
# PART 9: THE CONTEXT
# ============================================
grep -A 3 'error' log.txt
grep -B 3 'error' log.txt
grep -C 3 'error' log.txt
# ============================================
# PART 10: THE LFCS PIPELINE
# ============================================
# Find the error lines with the context
grep -C 2 'ERROR' /var/log/app.log
# Extract the email addresses
grep -oE '[[:alnum:]._%+-]+@[[:alnum:].-]+\.[[:alpha:]]{2,}' file.txt
# Find the configuration files with a setting
grep -rl --include='*.conf' 'Listen 80' /etc
# Count the errors per file
grep -rc 'ERROR' /var/log/*.log
# The silent check in the script
if grep -q 'pattern' file.txt; then
echo 'found'
fi
# The case-insensitive recursive search
grep -ri 'timeout' /etc/nginx
# The exclude the comments
grep -v '^#' /etc/ssh/sshd_config | grep -v '^$'
The ten parts show the basic regular expressions, the character classes, the quantifiers, the alternation and the grouping, the backreferences, the PCRE, the recursive search, the file selection, the context, and the LFCS pipeline.
Quick Reference
The Dialects
| Dialect | Flag | Special characters |
|---|---|---|
| BRE | (default) | ., *, [...], ^, $ |
| ERE | -E, egrep | +, ?, ` |
| PCRE | -P | \d, \w, \s, lookahead, lookbehind |
| Fixed | -F, fgrep | None; the literal strings |
The Anchors
| Pattern | Meaning |
|---|---|
^ | The start of the line |
$ | The end of the line |
^$ | The empty line |
\< / \> | The word boundary |
The Character Classes
| Pattern | Meaning |
|---|---|
. | Any character |
[abc] | One of the a, b, c |
[^abc] | Any except the a, b, c |
[a-z] | The range |
[[:digit:]] | Any digit |
[[:alpha:]] | Any letter |
[[:space:]] | Any whitespace |
The Quantifiers
| BRE | ERE | Meaning |
|---|---|---|
* | * | Zero or more |
\? | ? | Zero or one |
\+ | + | One or more |
\{n\} | {n} | Exactly n |
\{n,m\} | {n,m} | Between n and m |
The File Selection
| Option | Meaning |
|---|---|
-r | Recursive |
-R | Recursive, follow the symlinks |
--include='*.conf' | Only the conf files |
--exclude='*.log' | Skip the log files |
--exclude-dir='.git' | Skip the .git directory |
-l | The files with the matches |
-L | The files without the matches |
The Output Control
| Option | Meaning |
|---|---|
-n | The line numbers |
-c | The count |
-o | The only-matching |
-A N | The N lines after |
-B N | The N lines before |
-C N | The N lines on both sides |
-H / -h | The file name prefix / no prefix |
-q | The silent; the exit status |
--color=auto | The highlighted matches |
Best Practices
โ Do This:
# Use the -E for the extended regex
grep -E 'cat|dog' file.txt # โ
# Use the -o to extract the strings
grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' file.txt # โ
# Use the -C for the log context
grep -C 3 'ERROR' /var/log/app.log # โ
# Use the --include to filter the files
grep -r --include='*.conf' 'Listen' /etc # โ
# Use the -q for the script checks
if grep -q 'pattern' file.txt; then ... fi # โ
# Use the -P for the advanced patterns
grep -P '\bword\b' file.txt # โ
# Use the [[:class:]] for the portability
grep '[[:digit:]]' file.txt # โ
โ Don’t Do This:
# Don't forget the -E for the ERE metacharacters
grep 'cat|dog' file.txt # the literal | in the BRE // โ
# Don't use the -r without the filters
grep -r 'pattern' / # too slow and noisy // โ
# Don't forget the context for the logs
grep 'ERROR' /var/log/app.log # the missing context // โ
# Don't parse the grep output when the exit status suffices
grep 'pattern' file.txt | wc -l # use the -c or the -q // โ
# Don't use the -P when the portability matters
grep -P '\d' file.txt # the GNU only // โ
# Don't forget the quotes
grep *.log file.txt # the shell expands *.log // โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
The + is the literal | The BRE is the default | Use the -E |
| The ` | ` is the literal | The BRE is the default |
| The parentheses are the literal | The BRE is the default | Use the -E or the \( |
The . matches the unexpected | The dot is any character | Escape it: the \. |
The -r is too slow | No filters | Use the --include |
| The context is missing | The -A, the -B, the -C | Add the context |
| The color is not working | The --color=auto is the tty only | Use the --color=always |
| The PCRE is not available | The BSD or the busybox | Use the BRE or the ERE |
Real-World Examples
1. The Error Lines
grep '^ERROR' /var/log/app.log
2. The Case-Insensitive
grep -i 'error' /var/log/app.log
3. The Context
grep -C 3 'ERROR' /var/log/app.log
4. The Extended
grep -E '^(ERROR|WARN):' /var/log/app.log
5. The Extract
grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' file.txt
6. The Recursive
grep -r --include='*.conf' 'Listen' /etc
7. The File List
grep -rl 'pattern' /etc
8. The Count
grep -rc 'ERROR' /var/log/*.log
9. The Silent
if grep -q 'pattern' file.txt; then echo found; fi
10. The Backreference
grep -E '(\w+)\s+\1' file.txt
Visual
The Regular-Expression Matching
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE REGULAR-EXPRESSION MATCHING โ
โ โ
โ The pattern: ^error.*[0-9]+$ โ
โ โ
โ The input: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ error: the disk is full 100 โ โ
โ โ ^ ^ ^ ^ โ โ
โ โ โ โ โ โ โ โ
โ โ โ โ โ โโโ [0-9]+ the digits โ โ
โ โ โ โ โโโโโโ .* any characters โ โ
โ โ โ โโโโโโโโโโโโโโโโโโโโโโโ error the literal โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ ^ the start โ โ
โ โ โ โ
โ โ The line matches. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ warning: the disk is full โ โ
โ โ ^ โ โ
โ โ โ โ โ
โ โ โโโ ^error does not match the warning โ โ
โ โ โ โ
โ โ The line does not match. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The BRE vs the ERE
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE BRE vs THE ERE โ
โ โ
โ THE PATTERN: the cat or the dog โ
โ โ
โ BRE (the default): โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep 'cat\|dog' file.txt โ โ
โ โ The \| is the alternation. โ โ
โ โ The bare | is the literal. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ ERE (-E): โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep -E 'cat|dog' file.txt โ โ
โ โ The | is the alternation. โ โ
โ โ The \| is the literal. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ THE PATTERN: one or more a's โ
โ โ
โ BRE: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep 'a\+' file.txt โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ ERE: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep -E 'a+' file.txt โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ The metacharacters are the same, but the escaping is โ
โ the opposite. The -E is the modern choice. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Context Options
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE CONTEXT OPTIONS โ
โ โ
โ The log file: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ line 1: the request received โ โ
โ โ line 2: the user id 42 โ โ
โ โ line 3: ERROR the database timeout โ โ
โ โ line 4: the stack trace... โ โ
โ โ line 5: the request failed โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ grep -A 2 'ERROR': โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ line 3: ERROR the database timeout โ โ
โ โ line 4: the stack trace... โ โ
โ โ line 5: the request failed โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ grep -B 2 'ERROR': โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ line 1: the request received โ โ
โ โ line 2: the user id 42 โ โ
โ โ line 3: ERROR the database timeout โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ grep -C 2 'ERROR': โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ line 1: the request received โ โ
โ โ line 2: the user id 42 โ โ
โ โ line 3: ERROR the database timeout โ โ
โ โ line 4: the stack trace... โ โ
โ โ line 5: the request failed โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Recursive Search
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE RECURSIVE SEARCH โ
โ โ
โ WITHOUT the filters: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep -r 'Listen' /etc โ โ
โ โ โ โ
โ โ Searches every file under /etc. โ โ
โ โ The binary files, the logs, the caches. โ โ
โ โ Slow and noisy. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ WITH the --include: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep -r --include='*.conf' 'Listen' /etc โ โ
โ โ โ โ
โ โ Searches only the .conf files. โ โ
โ โ Fast and precise. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ WITH the --exclude: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep -r --exclude='*.log' 'error' /var โ โ
โ โ โ โ
โ โ Searches everything except the .log files. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ WITH the --exclude-dir: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ grep -r --exclude-dir='.git' 'TODO' . โ โ
โ โ โ โ
โ โ Skips the .git directory. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| The default dialect | The basic regular expression (BRE) |
| The extended | -E, egrep |
| The Perl | -P |
| The fixed | -F, fgrep |
| The anchors | ^, $ |
| The quantifiers | *, \?/?, \+/+, \{n\}/{n} |
| The character classes | [abc], [^abc], [[:digit:]] |
| The alternation | | (the BRE), ` |
| The grouping | \( \) (the BRE), ( ) (the ERE) |
| The backreferences | \1, \2 |
| The recursive | -r, -R |
| The file filters | --include, --exclude |
| The context | -A, -B, -C |
| The only-matching | -o |
Key takeaways:
- The default dialect is the basic regular expression (BRE). The
+, the?, the|, the(, and the)are the literal characters, and the escaped forms are the special. The-Eflag or theegrepcommand enables the extended regular expressions, where the same characters are the special . - The PCRE is enabled with the
-Pflag. The\d, the\w, the\s, the lookahead, and the lookbehind are the additions. The-Pis the GNU extension, and the portability is the trade-off . - The character classes are the
[abc], the[^abc], and the[[:class:]]. The POSIX classes โ the[[:digit:]], the[[:alpha:]], the[[:space:]]โ are the portable forms. The.matches any character, and it must be escaped to match the literal . - The quantifiers are the
*, the?, the+, and the{n}. The BRE escapes them, and the ERE does not. The*is the zero or more, the+is the one or more, and the{n,m}is the range . - The recursive search is the
-rand the-R. The-ris the common choice, and the-Rfollows the symbolic links. The--includeand the--excludefilter the files, and the--exclude-dirskips the directories . - The context options are the
-A, the-B, and the-C. The-A Nshows the N lines after, the-B Nshows the N lines before, and the-C Nshows the N lines on both sides. The context is the essential for the log analysis . - The
-oprints only the matched part. The-oEwith the ERE is the way to extract the strings like the email addresses and the IP addresses. The-ccounts the matching lines, and the-llists the files . - The
-qis the silent mode for the scripts. The exit status is 0 on the first match, and the output is suppressed. The-qis the standard for theifchecks in the shell scripts .
Remember: grep is the standard text-search tool, and the depth is the regular expressions and the file selection. The default dialect is the BRE, and the -E enables the ERE. The -P enables the PCRE. The character classes, the quantifiers, the alternation, the grouping, and the backreferences are the building blocks. The -r recurses, and the --include and the --exclude filter the files. The -A, the -B, and the -C show the context, and the -o extracts the matches. The result is a tool that can find the lines by any pattern and control the output for any task.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!