Grep: Difference between revisions
Content added Content deleted
m (Protected "Grep" ([Edit=Allow only logged in users] (indefinite) [Move=Allow only logged in users] (indefinite) [Delete=Allow only logged in users] (indefinite))) |
(ββOne-Liners: searching) |
||
Line 67: | Line 67: | ||
|- |
|- |
||
| Search for "virus" in all files in a dir || grep virus /etc/snort/rules/* |
| 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> |
|||
|} |
|} |
||
<br /> |
<br /> |
Revision as of 20:35, 2 July 2017
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
Task | Command |
---|---|
Search for the given string in a single file | grep "this" demo_file |
Checking for the given string in multiple files | grep "this" demo_* |
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 match | grep -A 3 -i "example" demo_text |
Display N lines before match | grep -B 2 "single WORD" demo_text |
Display N lines around match | grep -C 2 "Example" demo_text |
Highlighting the search using GREP_OPTIONS | export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8' |
Searching in all files recursively | grep -r "ramesh" * |
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 grep -c this demo_file |
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 |
{{#widget:DISQUS
|id=networkm
|uniqid=Grep
|url=https://aman.awiki.org/wiki/Grep
}}