GREP Command Builder - Interactive Pattern Search Tool
What is the GREP Command?
The grep (Global Regular Expression Print) command is a powerful Unix utility used to search for specific patterns within files or output streams [^51^]. It supports basic and extended regular expressions, making it indispensable for text processing, log analysis, and data extraction tasks.
Common Use Cases:
- Search for errors or keywords in log files
- Filter output from other commands using pipes
- Recursively search codebases for patterns
- Extract specific data using regular expressions
- Count occurrences of patterns in files
- Display line numbers for debugging
Why Use This GREP Command Builder?
Building grep commands requires understanding complex regex syntax and option combinations. Our visual builder helps you:
- ✅ Generate commands without syntax errors
- ✅ Visualize regex patterns with live explanations
- ✅ Copy production-ready commands instantly
- ✅ Learn pattern matching through interactive examples
Core Features
1. Pattern Matching Options
Control how patterns are interpreted [^50^]:
- Basic regex (-G): Default BRE syntax with limited metacharacters
- Extended regex (-E): ERE syntax supporting +, ?, |, and () [^48^][^52^]
- Fixed strings (-F): Search literal strings without regex interpretation [^50^]
- Perl regex (-P): PCRE syntax with advanced features [^50^]
2. Search Modifiers
Refine your search behavior [^48^][^52^]:
- Case insensitive (-i): Match regardless of case [^51^]
- Whole words (-w): Match complete words only [^48^]
- Invert match (-v): Show lines NOT matching pattern [^48^]
- Line matching (-x): Match entire lines only [^50^]
3. Recursive Search
Search through directory hierarchies [^48^][^57^]:
- Recursive (-r): Search directories recursively
- Follow symlinks (-R): Include symlinked directories [^48^]
- Include patterns (--include): Search specific file types only
- Exclude patterns (--exclude): Skip specific files [^48^]
- Exclude directories (--exclude-dir): Skip directories like node_modules [^48^]
4. Output Control
Customize what grep displays [^50^]:
- Line numbers (-n): Show line numbers with matches [^51^]
- Count only (-c): Display match counts per file [^52^]
- Filenames only (-l): Show only matching filenames [^48^]
- Quiet mode (-q): Exit status only (for scripts) [^48^]
- Only matching (-o): Print only the matched part [^48^]
5. Context Display
Show surrounding lines for better understanding [^48^]:
- After context (-A): Show N lines after match
- Before context (-B): Show N lines before match
- Context (-C): Show N lines before and after
Common GREP Command Patterns
Basic Text Search
# Simple pattern search
grep "error" logfile.txt
# Case insensitive search
grep -i "error" logfile.txt
# Whole word only
grep -w "error" logfile.txt
# Count occurrences
grep -c "error" logfile.txt
# Show line numbers
grep -n "error" logfile.txtRecursive Code Search
# Search all files recursively
grep -r "TODO" .
# Search specific file types only
grep -r --include="*.js" "console.log" .
# Exclude directories
grep -r --exclude-dir={node_modules,.git} "function" .
# Exclude file types
grep -r --exclude="*.min.js" "pattern" .Pipeline Filtering
# Filter command output
ps aux | grep "nginx"
# Chain multiple filters
cat access.log | grep "404" | grep -v "favicon"
# Check if package is installed
dpkg -l | grep -i "openssh"
# Filter with context
ifconfig | grep -A 4 "eth0"Regular Expression Patterns
# Extended regex (OR pattern)
grep -E "error|warning|critical" app.log
# Lines starting with pattern
grep "^Error" logfile.txt
# Lines ending with pattern
grep "failed$" logfile.txt
# Match IP addresses
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" access.log
# Match email addresses
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" contacts.txtLog Analysis
# Find errors excluding warnings
grep "ERROR" app.log | grep -v "DEPRECATED"
# Recent errors with context
grep -B 2 -A 2 "Exception" /var/log/syslog
# Find all unique error types
grep -o "ERROR.*" app.log | sort | uniq -c | sort -rn
# Search compressed logs
zgrep "pattern" access.log.gzFile Selection
# Show only filenames containing match
grep -rl "API_KEY" .
# Show files NOT containing pattern
grep -rL "test" src/
# Limit matches per file
grep -m 5 "pattern" largefile.txtGREP Command Options Reference
Pattern Syntax
| Option | Description |
|---|---|
| -E, --extended-regexp | Use extended regular expressions [^50^] |
| -F, --fixed-strings | Treat pattern as literal strings [^50^] |
| -G, --basic-regexp | Use basic regular expressions (default) [^50^] |
| -P, --perl-regexp | Use Perl-compatible regular expressions [^50^] |
| -e pattern | Specify multiple patterns |
| -f file | Read patterns from file [^54^] |
Matching Control
| Option | Description |
|---|---|
| -i, --ignore-case | Case insensitive search [^51^] |
| -v, --invert-match | Select non-matching lines [^48^] |
| -w, --word-regexp | Match whole words only [^48^] |
| -x, --line-regexp | Match whole lines only [^50^] |
| -m num | Stop after num matches [^50^] |
Output Control
| Option | Description |
|---|---|
| -n, --line-number | Show line numbers [^51^] |
| -c, --count | Count matching lines [^52^] |
| -l, --files-with-matches | Show only filenames [^48^] |
| -L, --files-without-match | Show filenames without match [^50^] |
| -o, --only-matching | Print only matched parts [^48^] |
| -q, --quiet | No output, exit status only [^48^] |
| -s, --no-messages | Suppress error messages [^50^] |
Context Control
| Option | Description |
|---|---|
| -A num | Show num lines after match [^48^] |
| -B num | Show num lines before match [^48^] |
| -C num | Show num lines before and after [^50^] |
| --group-separator=SEP | Use SEP instead of -- |
| --no-group-separator | No separator between groups |
Recursive Options
| Option | Description |
|---|---|
| -r, --recursive | Read all files recursively [^57^] |
| -R, --dereference-recursive | Follow symbolic links [^48^] |
| --include=GLOB | Search only matching files [^48^] |
| --exclude=GLOB | Skip matching files [^48^] |
| --exclude-dir=GLOB | Skip matching directories [^48^] |
File Options
| Option | Description |
|---|---|
| -a, --text | Process binary files as text [^50^] |
| -I | Ignore binary files |
| --color[=WHEN] | Highlight matches [^50^] |
| -Z, --null | Output zero byte after filename [^50^] |
| -H | Print filename with match [^50^] |
| -h | Suppress filename output [^50^] |
Regular Expression Quick Reference
Character Classes
| Pattern | Matches |
|---|---|
| . | Any single character [^53^] |
| [abc] | Any character in range [^53^] |
| [^abc] | Any character NOT in range [^53^] |
| [a-z] | Any lowercase letter |
| [0-9] | Any digit |
| \w | Word character (alphanumeric + underscore) |
| \s | Whitespace character |
Anchors
| Pattern | Matches |
|---|---|
| ^ | Start of line [^51^] |
| $ | End of line [^51^] |
| < | Start of word |
| > | End of word |
| \b | Word boundary [^53^] |
Quantifiers (ERE)
| Pattern | Matches |
|---|---|
| ? | Zero or one [^53^] |
| * | Zero or more [^53^] |
| + | One or more [^53^] |
| {n} | Exactly n times [^55^] |
| {n,} | n or more times [^55^] |
| {n,m} | Between n and m times [^55^] |
Grouping and Alternation
| Pattern | Matches |
|---|---|
| (abc) | Group and remember [^53^] |
| abc|def | Either abc OR def [^53^] |
| \1, \2 | Backreference to group [^53^] |
FAQ
What is the difference between grep and egrep?
egrep is the same as grep -E - both use extended regular expressions [^52^]. Modern grep supports -E flag, so egrep is deprecated but still available for backward compatibility. Use grep -E for better portability.
How do I search recursively in all subdirectories?
Use grep -r "pattern" . to search all files in current directory and subdirectories [^57^]. Add --exclude-dir=dir_name to skip directories like node_modules or .git [^48^].
How do I make grep case insensitive?
Add the -i flag: grep -i "pattern" file.txt [^51^]. This matches "Pattern", "PATTERN", "pattern", etc.
What is the difference between -r and -R?
The -r flag searches recursively but skips symbolic links [^48^]. The -R flag follows symbolic links and searches their targets as well [^48^]. Use -r to avoid infinite loops with circular symlinks.
How do I show lines before and after a match?
Use -C N to show N lines of context before and after: grep -C 3 "error" log.txt [^50^]. Use -B N for lines before only, -A N for lines after only [^48^].
How do I exclude binary files from search?
Use the -I flag (capital i) to ignore binary files entirely, or -a to treat binary files as text [^50^]. Without these, grep may print "Binary file matches" instead of showing content.
Can I search for multiple patterns at once?
Yes, use grep -e "pattern1" -e "pattern2" for multiple patterns [^54^], or grep -E "pattern1|pattern2" for extended regex OR operation [^52^].
How do I count the number of matches?
Use grep -c "pattern" file.txt to count lines containing matches [^52^]. Note this counts lines, not occurrences. For total occurrences across multiple lines, use grep -o "pattern" file.txt | wc -l.
What does the -w flag do?
The -w flag matches whole words only [^48^]. grep -w "the" file.txt matches "the" but not "then" or "other". Word boundaries are determined by non-word characters (spaces, punctuation).
How do I use grep in a shell script to check if a pattern exists?
Use the -q (quiet) flag and check the exit status: if grep -q "pattern" file.txt; then echo "Found"; fi [^48^]. -q produces no output, just returns exit code 0 if found, 1 if not found.
Technical Specifications
- Zero dependencies: Runs entirely in browser
- No backend required: Static hosting compatible
- Offline capable: Works without internet after initial load
- Privacy focused: No data leaves your browser
- Keyboard shortcuts: Full keyboard navigation support
Related Tools
- awk Command Builder
- sed Stream Editor
- find File Search
- Regular Expression Tester
- diff File Comparator
- sort Line Sorter
Resources
- GNU Grep Manual
- Linux man pages - grep(1) [^50^]
- GeeksforGeeks Grep Guide [^54^]
- DigitalOcean Grep Tutorial [^51^]