grep
PLEASE NOTE:
I am in the process of making my notes public. This docs section is still work in progress. It currently is a summary of random tips for various things.
Found a mistake? Want to add something? Feel free to open an issue, send a pull request or an e-mail.
- General info:
- Homepage
- Wikipedia
- Source code
- Documentation
- Manual
- Blog posts
Usefull links:
Basics
- Basic syntax:
grep [option...] [patterns] [file...]
Search for special characters
- Use
\
or--
to search special chars: grep "\-c" /dir/file
grep -- -c /dir/file
Search multiple terms
- OR-method - show result if 1 term is found:
grep -E "pineapple|pizza" file.txt
grep -e pineapple -e pizza file.txt
grep 'pineapple\|pizza' file.txt
- AND-method - show result when all terms are in one line:
grep 'pizza' file.text | grep 'pineapple'
-
grep -E 'pizza.*pineapple' file.text
(In order) grep -E 'pizza.*pineapple|pineaple.*pizza' file.text
Show additional lines
- Show addtional lines above and below matching line:
-
-A 1
- Show x lines above -
-B 1
- Show x lines below -
-C 1
- Show x lines above and below
Random examples
- Look for open port on your system:
ss -lpn | grep :22
- Look for
foo
in directory (My default search for docs): grep -Iirsn --color foo /random/dir/
-
-I
- ignore binary files -
-i
- ignore case distinction -
-r
- recursive search -
-s
- suppress error messages about nonexistent or unreadable files -
-n
- line that includes the search term
E-Mail
hello
- TODO:
- search with regex