151 lines
3.5 KiB
Markdown
151 lines
3.5 KiB
Markdown
# string searching using grep in Linux
|
|
|
|
## 1. Basic Usage:
|
|
The fundamental syntax of grep is:
|
|
```
|
|
grep [options] pattern [file...]
|
|
```
|
|
|
|
Example:
|
|
```
|
|
grep "error" logfile.txt
|
|
```
|
|
This searches for the word "error" in logfile.txt.
|
|
|
|
## 2. Case Sensitivity:
|
|
- By default, grep is case-sensitive.
|
|
- Use -i for case-insensitive search:
|
|
```
|
|
grep -i "error" logfile.txt
|
|
```
|
|
|
|
## 3. Regular Expressions:
|
|
grep supports regular expressions for powerful pattern matching.
|
|
|
|
- . (dot): Matches any single character
|
|
- *: Matches zero or more of the preceding character
|
|
- ^: Matches the start of a line
|
|
- $: Matches the end of a line
|
|
- []: Matches any single character in brackets
|
|
|
|
Example:
|
|
```
|
|
grep "^Error" logfile.txt # Lines starting with "Error"
|
|
grep "failed$" logfile.txt # Lines ending with "failed"
|
|
grep "t[ae]st" logfile.txt # Matches "test" or "tast"
|
|
```
|
|
|
|
## 4. Extended Regular Expressions:
|
|
Use -E option or egrep command for extended regex:
|
|
```
|
|
grep -E "Error|Warning" logfile.txt
|
|
egrep "Error|Warning" logfile.txt
|
|
```
|
|
|
|
## 5. Inverting the Match:
|
|
-v option inverts the match, showing lines that don't match:
|
|
```
|
|
grep -v "success" logfile.txt
|
|
```
|
|
|
|
## 6. Displaying Line Numbers:
|
|
-n option shows line numbers:
|
|
```
|
|
grep -n "error" logfile.txt
|
|
```
|
|
|
|
## 7. Counting Matches:
|
|
-c option counts the number of matching lines:
|
|
```
|
|
grep -c "error" logfile.txt
|
|
```
|
|
|
|
## 8. Showing Context:
|
|
- -A n: Shows n lines after the match
|
|
- -B n: Shows n lines before the match
|
|
- -C n: Shows n lines before and after the match
|
|
|
|
Example:
|
|
```
|
|
grep -C 2 "critical error" logfile.txt
|
|
```
|
|
|
|
## 9. Recursive Search:
|
|
-r option searches recursively through directories:
|
|
```
|
|
grep -r "TODO" /path/to/project
|
|
```
|
|
|
|
## 10. Matching Whole Words:
|
|
-w option matches whole words only:
|
|
```
|
|
grep -w "log" logfile.txt
|
|
```
|
|
|
|
## 11. Displaying Filename:
|
|
- -H: Always print filename
|
|
- -h: Never print filename
|
|
```
|
|
grep -H "error" *.log
|
|
```
|
|
|
|
## 12. Quiet Mode:
|
|
-q option suppresses output, useful in scripts:
|
|
```
|
|
if grep -q "error" logfile.txt; then
|
|
echo "Errors found"
|
|
fi
|
|
```
|
|
|
|
## 13. Using grep with Pipes:
|
|
grep works well with pipes for filtering output:
|
|
```
|
|
ps aux | grep "nginx"
|
|
```
|
|
|
|
## 14. Multiple Patterns:
|
|
Use -e option for multiple patterns:
|
|
```
|
|
grep -e "error" -e "warning" -e "critical" logfile.txt
|
|
```
|
|
|
|
## 15. Reading Patterns from a File:
|
|
-f option reads patterns from a file:
|
|
```
|
|
grep -f patterns.txt logfile.txt
|
|
```
|
|
|
|
## 16. Binary Files:
|
|
- -a: Process binary files as text
|
|
- --binary-files=without-match: Assume binary files don't match
|
|
|
|
```
|
|
grep -a "string" binary_file
|
|
```
|
|
|
|
## 17. Colorizing Output:
|
|
--color option highlights matches:
|
|
```
|
|
grep --color "error" logfile.txt
|
|
```
|
|
|
|
## 18. Excluding Files:
|
|
--exclude option excludes files from the search:
|
|
```
|
|
grep "TODO" --exclude="*.o" -r .
|
|
```
|
|
|
|
## 19. Including Only Certain Files:
|
|
--include option includes only specified files:
|
|
```
|
|
grep "function" --include="*.c" -r .
|
|
```
|
|
|
|
## 20. Null-Separated Output:
|
|
-Z option outputs a zero byte after filename:
|
|
```
|
|
grep -lZ "error" *.log | xargs -0 rm
|
|
```
|
|
|
|
This guide covers many of grep's powerful features. Remember to consult the man pages (`man grep`) for more detailed information and additional options.
|