LWM-Linux/06 - Linux File Operations/String Matching (Inside files using grep).md

111 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

# string searching using grep in Linux
## 1. Basic Usage:
The fundamental syntax of grep is:
2024-09-07 09:29:54 -06:00
- `grep [options] pattern [file...]`
Example:
2024-09-07 09:29:54 -06:00
- `grep "error" logfile.txt` # This searches for the word "error" in logfile.txt.
## 2. Case Sensitivity:
2024-09-07 09:29:54 -06:00
- By default, grep IS case-sensitive.
- Use `-i` for case-insensitive search:
- `grep -i "eRRor" logfile.txt` # This will return all occurances of "error" in any case sensitivity. Error, erRor, ERROR, etc...
## 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
2024-09-07 09:29:54 -06:00
- [ ]: Matches any single character in brackets
Example:
2024-09-07 09:29:54 -06:00
- `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:
2024-09-07 09:29:54 -06:00
- `grep -E "Error|Warning" logfile.txt` # Searches for "Error" or "Warning" in "logfile.txt"
- `egrep "Error|Warning" logfile.txt` # Same as above
## 5. Inverting the Match:
-v option inverts the match, showing lines that don't match:
2024-09-07 09:29:54 -06:00
- `grep -v "success" logfile.txt` # Match any line that does not contain the work "success" in logfile.txt
## 6. Displaying Line Numbers:
2024-09-07 09:29:54 -06:00
`-n` option shows line numbers:
- `grep -n "error" logfile.txt`
## 7. Counting Matches:
2024-09-07 09:29:54 -06:00
`-c` option counts the number of matching lines:
- `grep -c "error" logfile.txt`
## 8. Showing Context:
2024-09-07 09:29:54 -06:00
- `-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:
2024-09-07 09:29:54 -06:00
- `grep -C 2 "critical error" logfile.txt` # Match "critical error" inside "logfile.txt" and show 2 lines above/below each occurance.
## 9. Recursive Search:
2024-09-07 09:29:54 -06:00
`-r` option searches recursively through directories:
- `grep -r "TODO" /path/to/project`
## 10. Matching Whole Words:
2024-09-07 09:29:54 -06:00
`-w` option matches whole words only:
- `grep -w "log" logfile.txt`
## 11. Displaying Filename:
2024-09-07 09:29:54 -06:00
- `-H`: Always print filename
- `-h`: Never print filename
- `grep -H "error" *.log`
## 12. Quiet Mode:
2024-09-07 09:29:54 -06:00
`-q` option suppresses output, useful in scripts:
```
if grep -q "error" logfile.txt; then
2024-09-07 09:29:54 -06:00
echo "Errors found"
fi
2024-09-07 09:29:54 -06:00
```
## 13. Using grep with Pipes:
grep works well with pipes for filtering output:
2024-09-07 09:29:54 -06:00
- `ps aux | grep "nginx"`
## 14. Multiple Patterns:
2024-09-07 09:29:54 -06:00
Use `-e` option for multiple patterns:
- `grep -e "error" -e "warning" -e "critical" logfile.txt`
## 15. Reading Patterns from a File:
2024-09-07 09:29:54 -06:00
`-f` option reads patterns from a file:
`grep -f patterns.txt logfile.txt`
## 16. Binary Files:
2024-09-07 09:29:54 -06:00
`-a` Process binary files as text
- \-\-binary-files=without-match: Assume binary files don't match
2024-09-07 09:29:54 -06:00
`grep -a "string" binary_file`
## 17. Colorizing Output:
2024-09-07 09:29:54 -06:00
`--color` option highlights matches:
- `grep --color "error" logfile.txt`
## 18. Excluding Files:
2024-09-07 09:29:54 -06:00
`--exclude` option excludes files from the search:
- `grep "TODO" --exclude="*.o" -r .`
## 19. Including Only Certain Files:
2024-09-07 09:29:54 -06:00
`--include` option includes only specified files:
- `grep "function" --include="*.c" -r .`
## 20. Null-Separated Output:
2024-09-07 09:29:54 -06:00
`-Z` option outputs a zero byte after filename:
- `grep -lZ "error" *.log | xargs -0 rm`