Basic Search Examples
1. Simple Text Search
grep "error" logfile.txt
2023-10-15 10:23:45 ERROR: Connection failed
2023-10-15 11:45:12 ERROR: Database timeout
2. Case-Insensitive Search
grep -i "warning" system.log
WARNING: Disk space low
Warning: Memory usage high
3. Count Matching Lines
grep -c "success" transactions.log
42
4. Show Line Numbers
grep -n "login" auth.log
15: User admin logged in
89: Failed login attempt
5. Inverse Search
grep -v "active" status.log
service-nginx: inactive
service-mysql: disabled
Common Options
6. Whole Word Search
grep -w "run" script.sh
function run_backup() {
echo "Running backup..."
7. Multiple Patterns
grep -e "error" -e "warning" logfile.txt
ERROR: File not found
WARNING: High CPU usage
8. Show Only Matching Part
grep -o "user_[0-9]*" access.log
user_123
user_456
user_789
9. Colorized Output
grep --color=auto "important" notes.txt
This is an important message
Another important note
10. Quiet Mode (Exit Status Only)
grep -q "success" output.log
echo $?
0
Regular Expressions
11. Extended Regular Expressions
grep -E "(error|warning|critical)" system.log
CRITICAL: Disk failure
WARNING: Memory leak detected
12. Search for IP Addresses
grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" logfile.txt
192.168.1.1
10.0.0.45
13. Search for Email Addresses
grep -Eo "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" contacts.txt
john@example.com
admin@company.org
14. Search with Word Boundaries
grep "\bstart\b" commands.txt
service start
systemctl start nginx
15. Search for Lines Starting With Pattern
grep "^DEBUG" app.log
DEBUG: Starting initialization
DEBUG: Loading modules
Context Display
16. Show Lines Around Match
grep -C 2 "exception" app.log
Processing request ID: 12345
EXCEPTION: Null pointer
Stack trace follows...
17. Show Lines Before Match
grep -B 2 "error" app.log
Processing data...
Validating input...
ERROR: Invalid format
18. Show Lines After Match
grep -A 3 "starting" app.log
Starting application...
Loading configuration...
Initializing modules...
Ready to accept requests
File Operations
19. Search Multiple Files
grep "config" *.conf
nginx.conf: worker_processes 4;
apache.conf: Timeout 300
20. Recursive Search
grep -r "database" /etc/
/etc/mysql/my.cnf: database = production
/etc/app/config: db_host = localhost
21. Show Filenames Only
grep -l "config" *.txt
settings.txt
app-config.txt
22. Search in Compressed Files
zgrep "error" logfile.txt.gz
2023-10-15 ERROR: Connection refused
2023-10-16 ERROR: Timeout occurred
23. Search with Exclude Pattern
grep -r --exclude-dir={.git,node_modules} "function" .
./src/main.js: function calculate() {
./lib/utils.js: function helper() {
Advanced Usage
24. Perl-Compatible Regex
grep -P "\d{3}-\d{2}-\d{4}" data.txt
SSN: 123-45-6789
Tax ID: 987-65-4321
25. Search with Fixed Strings
grep -F "$special*chars" file.txt
Line containing $special*chars
26. Search for Non-ASCII Characters
grep -P "[^\x00-\x7F]" document.txt
Café
naïve
résumé
27. Search with Backreferences
grep -E "([0-9]{3})-\\1" data.txt
123-123
456-456
28. Show Byte Offset
grep -b "target" binary.data
1234:target data
5678:another target
Practical Examples
29. Filter Command Output
ps aux | grep "nginx"
root 1234 0.0 0.5 12345 6789 ? Ss 10:00 0:00 nginx: master process
www-data 1235 0.0 0.8 23456 7890 ? S 10:00 0:05 nginx: worker process
30. Find High Memory Processes
ps aux | grep -E "[0-9]{3,}.[0-9]{1,}$" | head -5
user 1234 2.5 12.3 1234567 89012 ? Sl 10:00 15:30 memory_hungry_app
31. Find System Users
grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
john:x:1000:1000:John Doe:/home/john:/bin/bash
32. Search Kernel Messages
dmesg | grep -i "error\|warning"
[ 123.456789] ACPI Warning: Something might be wrong
[ 234.567890] USB device error: -110
33. Find URLs in Files
grep -Eo "https?://[^[:space:]]+" webpage.html
https://example.com
http://sub.domain.org/path
Note: The outputs shown are examples of what you might see when running these commands. Actual output will vary based on your system and files.