Complete bash-script toolkit with generation and validation capabilities
97
97%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Risky
Do not use without reviewing
grep (Global Regular Expression Print) searches for patterns in text files. It's one of the most commonly used Unix tools.
Official Manual: https://www.gnu.org/software/grep/manual/
Man Page: man grep
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] -e PATTERN ... [FILE...]
grep [OPTIONS] -f PATTERN_FILE ... [FILE...]-i, --ignore-case # Case-insensitive search
-v, --invert-match # Invert match (select non-matching lines)
-w, --word-regexp # Match whole words only
-x, --line-regexp # Match whole lines only
-c, --count # Count matching lines
-n, --line-number # Show line numbers
-H, --with-filename # Print filename with matches
-h, --no-filename # Suppress filename output
-l, --files-with-matches # Print only filenames with matches
-L, --files-without-match # Print only filenames without matches-A NUM, --after-context=NUM # Print NUM lines after match
-B NUM, --before-context=NUM # Print NUM lines before match
-C NUM, --context=NUM # Print NUM lines before and after-E, --extended-regexp # Use Extended Regular Expressions (ERE)
-F, --fixed-strings # Treat PATTERN as fixed strings, not regex
-G, --basic-regexp # Use Basic Regular Expressions (BRE) - default
-P, --perl-regexp # Use Perl-compatible regex (PCRE)-o, --only-matching # Print only matched parts
-q, --quiet, --silent # Suppress output, just return exit code
--color[=WHEN] # Colorize output (auto, always, never)
-s, --no-messages # Suppress error messages-r, --recursive # Recursive search
-R, --dereference-recursive # Recursive, following symlinks
--include=PATTERN # Search only files matching PATTERN
--exclude=PATTERN # Skip files matching PATTERN
--exclude-dir=PATTERN # Skip directories matching PATTERN# Simple string search
grep "error" logfile.txt
# Case-insensitive search
grep -i "error" logfile.txt
# Search for whole word
grep -w "error" logfile.txt
# Count matches
grep -c "error" logfile.txt
# Show line numbers
grep -n "error" logfile.txt# Search in multiple files
grep "pattern" file1.txt file2.txt
# Search in all txt files
grep "pattern" *.txt
# Recursive search
grep -r "pattern" /path/to/dir
# Recursive with file pattern
grep -r --include="*.log" "error" /var/log# Show 3 lines after match
grep -A 3 "error" logfile.txt
# Show 3 lines before match
grep -B 3 "error" logfile.txt
# Show 3 lines before and after
grep -C 3 "error" logfile.txt# Show lines that DON'T contain pattern
grep -v "debug" logfile.txt
# Exclude multiple patterns
grep -v "debug\|info" logfile.txt# Match any pattern (OR)
grep -e "error" -e "warning" file.txt
grep "error\|warning" file.txt
# Match all patterns (AND) - requires pipeline
grep "error" file.txt | grep "critical"# List files containing match
grep -l "pattern" *.txt
# List files NOT containing match
grep -L "pattern" *.txt
# Recursive with excludes
grep -r --exclude-dir=".git" "pattern" .
grep -r --exclude="*.min.js" "pattern" .# BRE Metacharacters (no escaping needed)
. # Any single character
^ # Start of line
$ # End of line
[...] # Character class
[^...] # Negated character class
* # Zero or more of previous
# BRE Metacharacters (MUST be escaped)
\+ # One or more (requires \)
\? # Zero or one (requires \)
\{m,n\} # Between m and n occurrences (requires \)
\(...\) # Group (requires \)
\| # Alternation (requires \)# Match lines starting with "Error"
grep "^Error" file.txt
# Match lines ending with "failed"
grep "failed$" file.txt
# Match any character
grep "a.c" file.txt # Matches abc, aXc, a5c
# Character class
grep "[0-9]" file.txt # Match any digit
grep "[A-Za-z]" file.txt # Match any letter
# One or more (escaped)
grep "a\+b" file.txt # Matches ab, aab, aaab
# Groups and alternation (escaped)
grep "\(error\|warning\)" file.txt# ERE - No escaping needed for +, ?, |, (), {}
+ # One or more
? # Zero or one
{m,n} # Between m and n occurrences
(...) # Group
| # Alternation# One or more (no escape)
grep -E "a+b" file.txt
# Zero or one
grep -E "colou?r" file.txt # Matches color or colour
# Alternation
grep -E "(error|warning)" file.txt
# Quantifiers
grep -E "[0-9]{3}-[0-9]{4}" file.txt # Phone: 123-4567
grep -E "[0-9]{1,3}" file.txt # 1 to 3 digits
# Groups
grep -E "(http|https)://[^ ]+" file.txt # URLs[:alnum:] # Alphanumeric [A-Za-z0-9]
[:alpha:] # Alphabetic [A-Za-z]
[:digit:] # Digits [0-9]
[:lower:] # Lowercase [a-z]
[:upper:] # Uppercase [A-Z]
[:space:] # Whitespace [ \t\n\r\f\v]
[:blank:] # Space and tab [ \t]
[:punct:] # Punctuation
[:xdigit:] # Hex digits [0-9A-Fa-f]
[:word:] # Word characters [A-Za-z0-9_]# Match any digit
grep "[[:digit:]]" file.txt
# Match any whitespace
grep "[[:space:]]" file.txt
# Match uppercase letters
grep "[[:upper:]]" file.txt# Find errors in last hour
grep "$(date -d '1 hour ago' '+%Y-%m-%d %H')" /var/log/app.log | grep -i error
# Count error types
grep -i "error" logfile.log | cut -d: -f2 | sort | uniq -c | sort -rn
# Extract IP addresses
grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log# Find uncommented lines in config
grep -v "^#" config.file | grep -v "^$"
# Check for specific settings
if grep -q "debug = true" config.ini; then
echo "Debug mode is enabled"
fi# Find function definitions
grep -n "^function " script.sh
# Find TODO comments
grep -rn "TODO" --include="*.sh" .
# Find unquoted variables (simple check)
grep -n '\$[A-Za-z_][A-Za-z0-9_]*\s' script.sh# Faster when not using regex
grep -F "literal string" large_file.txt# Don't need output, just exit code
if grep -q "pattern" file.txt; then
echo "Found"
fi# Don't recurse too deep
grep -r --max-depth=2 "pattern" /pathgrep -r --exclude-dir={.git,.svn,node_modules} "pattern" .# Wrong
grep $pattern file.txt
# Right
grep "$pattern" file.txt# Inefficient
if [ "$(grep pattern file)" ]; then
# Better
if grep -q pattern file; then# Wrong (UUOC)
cat file | grep pattern
# Right
grep pattern file
# or
< file grep pattern# grep returns 1 if no match, can cause set -e to exit
grep "pattern" file || true
# Or check explicitly
if grep -q "pattern" file; then
echo "Found"
else
echo "Not found"
fi# Wrong - . matches any character
grep "192.168.1.1" file
# Right - escape the dots
grep "192\.168\.1\.1" file
# Or use -F for literal match
grep -F "192.168.1.1" file# Case-insensitive recursive search with line numbers
grep -rni "pattern" /path
# Count total matches across files
grep -r "pattern" . | wc -l
# Find and highlight matches
grep --color=always "pattern" file.txt | less -R
# Search compressed files
zgrep "pattern" file.gz
# Search with extended regex and only show matches
grep -Eo "pattern" file.txtgenerator
validator