Grep: Difference between revisions
m (→Basics: * rem) |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 28: | Line 28: | ||
=One-Liners= |
=One-Liners= |
||
Case insensitive search |
|||
{| class="wikitable" |
|||
grep -i "the" demo_file |
|||
|- |
|||
! Task !! Command |
|||
Checking for full words, not for sub-strings |
|||
|- |
|||
grep -iw "is" demo_file |
|||
| Search for the given string in a single file || grep "this" demo_file |
|||
|- |
|||
Display N lines after, before, around match |
|||
| Checking for the given string in multiple files || grep "this" demo_* |
|||
grep -A 3 -i "example" demo_text # After Match |
|||
|- |
|||
grep -B 2 "single WORD" demo_text # Before Match |
|||
| Case insensitive search || grep -i "the" demo_file |
|||
grep -C 2 "Example" demo_text # Around Match |
|||
|- |
|||
| Checking for full words, not for sub-strings || grep -iw "is" demo_file |
|||
Color Filtered Output |
|||
|- |
|||
grep "Gecko" /data --color=always |
|||
| Display N lines after match || grep -A 3 -i "example" demo_text |
|||
|- |
|||
Highlighting the search using GREP_OPTIONS |
|||
| Display N lines before match || grep -B 2 "single WORD" demo_text |
|||
export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' |
|||
|- |
|||
| Display N lines around match || grep -C 2 "Example" demo_text |
|||
Recursive Search |
|||
|- |
|||
grep -r "test" * |
|||
| Highlighting the search using GREP_OPTIONS || export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' |
|||
|- |
|||
Recursively but ignore binary files |
|||
| Searching in all files recursively || grep -r "ramesh" * |
|||
grep -rI "test" someDir/ |
|||
|- |
|||
| 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 the lines which does not matches all the given pattern |
|||
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 |
|||
| 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 |
|||
|} |
|||
Show line number while displaying the output |
|||
grep -n "go" demo_text |
|||
Filter comments from a config file |
|||
<nowiki>grep -vE '^#|^;|^$' server.conf</nowiki> |
|||
Search for "virus" in all files in a dir |
|||
grep virus /etc/snort/rules/* |
|||
Search this or that using Extended Regex |
|||
<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* |
|||
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 /> |
<br /> |
||
<br /> |
<br /> |
Latest revision as of 19:19, 8 May 2018
Basics
Match regular expression in files
grep "lines.*empty" demo_file
Character | Function |
---|---|
? | The preceding item is optional and matched at most once. |
* | The preceding item will be matched zero or more times. |
+ | The preceding item will be matched one or more times. |
{n} | The preceding item is matched exactly n times. |
{n,} | The preceding item is matched n or more times. |
{,m} | The preceding item is matched at most m times. |
{n,m} | The preceding item is matched at least n times, but not more than m times. |
One-Liners
Case insensitive search
grep -i "the" demo_file
Checking for full words, not for sub-strings
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
Color Filtered Output
grep "Gecko" /data --color=always
Highlighting the search using GREP_OPTIONS
export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
Recursive Search
grep -r "test" *
Recursively but ignore binary files
grep -rI "test" someDir/
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
How many lines matches the given pattern
grep -c "go" demo_text
How many lines that does not match the pattern
grep -v -c this demo_file
Display only the file names which matches the given pattern
grep -l this demo_*
Show only the matched string
grep -o "is.*line" demo_file
Show line number while displaying the output
grep -n "go" demo_text
Filter comments from a config file
grep -vE '^#|^;|^$' server.conf
Search for "virus" in all files in a dir
grep virus /etc/snort/rules/*
Search this or that using Extended Regex
grep -E '(then|there)' demo_text
Search this or that without Extended Regex
grep '\(then\|there\)' demo_text
Search this or that grouping not necessary
grep 'then\|there' demo_text
Search this or that grouping required
grep 'the\(n\|re\)' demo_text
Search Email addresses using regex
grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" filename.txt
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
{{#widget:DISQUS
|id=networkm
|uniqid=Grep
|url=https://aman.awiki.org/wiki/Grep
}}