Grep: Difference between revisions

332 bytes removed ,  6 years ago
Line 28:
=One-Liners=
 
 
{| class="wikitable"
Case insensitive search
|-
grep -i "the" demo_file
! Task !! Command
 
|-
Checking for full words, not for sub-strings
| Search for the given string in a single file || grep "this" demo_file
grep -iw "is" demo_file
|-
 
| Checking for the given string in multiple files || grep "this" demo_*
Display N lines after match
|-
grep -A 3 -i "example" demo_text
| Case insensitive search || grep -i "the" demo_file
 
|-
Display N lines before match
| Checking for full words, not for sub-strings || grep -iw "is" demo_file
grep -B 2 "single WORD" demo_text
|-
 
| Display N lines after match || grep -A 3 -i "example" demo_text
Display N lines around match
|-
| Display N lines before match || grep -BC 2 "single WORDExample" demo_text
 
|-
Color Filtered Output
| Display N lines around match || grep -C 2 "Example" demo_text
grep "Gecko" /data --color=always
|-
 
| Color Filtered Output || grep "Gecko" /data --color=always
Highlighting the search using GREP_OPTIONS
|-
| Highlighting the search using GREP_OPTIONS || export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
 
|-
| Searching in all files recursively || grep -r "ramesh" *
grep -r "ramesh" *
|-
 
| Invert match || grep -v "go" demo_text
Invert match
|-
grep -v "go" demo_text
| display the lines which does not matches all the given pattern || grep -v -e "a" -e "b" -e "c" test-file.txt
 
|-
|Display How manythe lines which does not matches all the given pattern || grep -c "go" demo_text<br />grep -c this demo_file
grep -v -e "a" -e "b" -e "c" test-file.txt
|-
 
| How many lines that does not match the pattern || grep -v -c this demo_file
How many lines matches the given pattern
|-
grep -c "go" demo_text<br />grep -c this demo_file
| Display only the file names which matches the given pattern || grep -l this demo_*
 
|-
How many lines that does not match the pattern
| Show only the matched string || grep -o "is.*line" demo_file
grep -v -c this demo_file
|-
 
| Show line number while displaying the output || grep -n "go" demo_text
Display only the file names which matches the given pattern
|-
grep -l this demo_*
| Filter comments from a config file || <nowiki>grep -vE '^#|^;|^$' server.conf</nowiki>
 
|-
Show only the matched string
| Search for "virus" in all files in a dir || grep virus /etc/snort/rules/*
grep -o "is.*line" demo_file
|-
 
| Search this or that using Extended Regex || <nowiki>grep -E '(then|there)' demo_text</nowiki>
Show line number while displaying the output
|-
grep -n "go" demo_text
| Search this or that without Extended Regex || <nowiki>grep '\(then\|there\)' demo_text</nowiki>
 
|-
Filter comments from a config file
| Search this or that grouping not necessary || <nowiki>grep 'then\|there' demo_text</nowiki>
<nowiki>grep -vE '^#|^;|^$' server.conf</nowiki>
|-
 
| Search this or that grouping required || <nowiki>grep 'the\(n\|re\)' demo_text</nowiki>
Search for "virus" in all files in a dir
|-
grep virus /etc/snort/rules/*
| Search Email addresses using regex || <nowiki>grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" filename.txt</nowiki>
 
|-
Search this or that using Extended Regex
| Grep from compressed files || zgrep -I "free space" ./messages*
<nowiki>grep -E '(then|there)' demo_text</nowiki>
|}
 
Search this or that without Extended Regex
<nowiki>grep '\(then\|there\)' demo_text</nowiki>
 
Search this or that grouping not necessary
<nowiki>grep 'then\|there' demo_text</nowiki>
 
Search this or that grouping required
<nowiki>grep 'the\(n\|re\)' demo_text</nowiki>
 
Search Email addresses using regex
<nowiki>grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" filename.txt</nowiki>
 
Grep from compressed files
zgrep -I "free space" ./messages*
 
<br />
<br />