Grep: Difference between revisions

274 bytes added ,  6 years ago
 
(One intermediate revision by the same user not shown)
Line 27:
 
=One-Liners=
 
 
Case insensitive search
Line 35 ⟶ 34:
grep -iw "is" demo_file
 
Display N lines after, before, around match
grep -A 3 -i "example" demo_text # After Match
grep -B 2 "single WORD" demo_text # Before Match
 
grep -C 2 "Example" demo_text # Around Match
Display N lines before match
grep -B 2 "single WORD" demo_text
 
Display N lines around match
grep -C 2 "Example" demo_text
 
Color Filtered Output
Line 50 ⟶ 45:
export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
 
Recursive Search
Searching in all files recursively
grep -r "rameshtest" *
 
Recursively but ignore binary files
grep -rI "test" someDir/
 
Invert match
Line 60 ⟶ 58:
 
How many lines matches the given pattern
grep -c "go" demo_text<br />grep -c this demo_file
 
How many lines that does not match the pattern
Line 97 ⟶ 95:
Grep from compressed files
zgrep -I "free space" ./messages*
 
Print lines which begin with "foo" and end in "bar"
grep "^foo.*bar$" file.txt
 
Filter the Filtered output:
grep "test" file.txt | grep -v "pass"
 
Search for the string and not the regex:
fgrep "foobar" file.txt
grep -F "foobar" file.txt
 
<br />