Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grep is a powerful command-line utility used for searching text patterns in files. Here are some common use cases for grep:
- Finding Text in Files: Search for a specific word or pattern in one or multiple files. For example:
- perl
- Copy code
- grep "pattern" file.txt
- Recursive Search: Search for a pattern recursively in all files under a directory and its subdirectories. For example:
- perl
- Copy code
- grep -r "pattern" directory/
- Case Insensitive Search: Perform a case-insensitive search. For example:
- perl
- Copy code
- grep -i "pattern" file.txt
- Counting Matches: Count the number of lines that match a pattern. For example:
- perl
- Copy code
- grep -c "pattern" file.txt
- Display Matching Line Numbers: Display line numbers along with matching lines. For example:
- perl
- Copy code
- grep -n "pattern" file.txt
- Matching Whole Words: Search for the pattern as a whole word. For example:
- perl
- Copy code
- grep -w "pattern" file.txt
- Using Regular Expressions: Utilize regular expressions for more complex pattern matching. For example:
- perl
- Copy code
- grep "^[0-9]" file.txt
- Inverting Match: Display lines that do not match the pattern. For example:
- perl
- Copy code
- grep -v "pattern" file.txt
- Displaying Context: Show lines before and after the matched lines. For example:
- css
- Copy code
- grep -A 2 -B 1 "pattern" file.txt
- Using Multiple Patterns: Search for lines matching any one of multiple patterns. For example:
- perl
- Copy code
- grep -e "pattern1" -e "pattern2" file.txt
- Using Extended Regular Expressions: Enable extended regular expressions for more complex pattern matching. For example:
- mathematica
- Copy code
- grep -E "pattern1|pattern2" file.txt
Add Comment
Please, Sign In to add comment