1/15: Search for the word "hello" in the file "example.txt":
grep hello example.txt
grep hello example.txt
2/15: Perform a case-insensitive search for the word "world" in multiple files:
grep -i world file1.txt file2.txt
grep -i world file1.txt file2.txt
3/15: Count the number of occurrences of the word "error" in a file:
grep -c error logfile.txt
grep -c error logfile.txt
4/15: Search for lines containing either "apple" or "banana" in a file:
grep 'apple\|banana' fruits.txt
grep 'apple\|banana' fruits.txt
5/15: Exclude the directory "docs" from the search for the word "important":
grep -v important --exclude-dir=docs *
grep -v important --exclude-dir=docs *
6/15: Search for the whole word "cat" in a file, ignoring partial matches:
grep -w cat animals.txt
grep -w cat animals.txt
7/15: Display lines that don't contain the word "success" in a file:
grep -v success report.txt
grep -v success report.txt
8/15: Search for a pattern recursively in all subdirectories:
grep -r "pattern" directory/
grep -r "pattern" directory/
9/15: Search for a pattern using extended regular expressions:
grep -E 'regex(pattern)?' file.txt
grep -E 'regex(pattern)?' file.txt
10/15: Show 3 lines before and after a match of the word "error":
grep -B 3 -A 3 error log.txt
grep -B 3 -A 3 error log.txt
11/15: Perform a case-insensitive search for the word "Linux":
grep -i Linux document.txt
grep -i Linux document.txt
12/15: Search for a pattern in compressed files:
grep -z pattern compressed.log.gz
grep -z pattern compressed.log.gz
13/15: Display line numbers along with matching lines:
grep -n "keyword" file.txt
grep -n "keyword" file.txt
14/15: Search for the word "example" in all text files within a directory:
grep "example" *.txt
grep "example" *.txt
15/15: Search for lines starting with "TODO" in a file and output the results to a new file:
grep '^TODO' tasks.txt > todo.txt
grep '^TODO' tasks.txt > todo.txt
Ever needed more advanced pattern matching with #grep?
Meet egrep!
It's similar to grep but supports extended regular expressions for powerful searches. #Linux #CommandLine
Meet egrep!
It's similar to grep but supports extended regular expressions for powerful searches. #Linux #CommandLine
1) egrep is just an alias for grep -E, where the -E option enables extended regular expressions.
You can use metacharacters like +, ?, and {} to specify occurrences and use parentheses for grouping and alternation with |. #egrep #Linux #CommandlineMagic
You can use metacharacters like +, ?, and {} to specify occurrences and use parentheses for grouping and alternation with |. #egrep #Linux #CommandlineMagic
2) With egrep, u can perform complex searches effortlessly.
For eg, search for lines containing either "apple" or "banana" in a file:
egrep 'apple|banana' fruits.txt
The extended regular expression pattern 'apple|banana' matches lines with either "apple" or "banana". #egrep
For eg, search for lines containing either "apple" or "banana" in a file:
egrep 'apple|banana' fruits.txt
The extended regular expression pattern 'apple|banana' matches lines with either "apple" or "banana". #egrep
3) So, when you need more advanced pattern matching capabilities with #grep, turn to egrep!
It's a powerful tool that supports extended regular expressions, enabling you to perform complex searches with ease. #egrep #Linux #CommandLineMagic
It's a powerful tool that supports extended regular expressions, enabling you to perform complex searches with ease. #egrep #Linux #CommandLineMagic
Retweet the thread if you find it useful. Thanks!
Loading suggestions...