GREP Cheatsheet

GREP stands for Global search using Regular Expressions and Print. It is a find command which returns all the line that has respective search patterns.

Syntax : grep searchPattern searchLocation


Parameter Output
-n outputs line number
-v outputs lines that doesn't contain the pattern
-i ignores the case of searchPattern
"searchPattern" search term (case sensitive in case -i not provided)
^ matches search pattern at the beginning of the line. e.g. ^"search".
$ matches search pattern at the end of the line.,e.g. "search"$.
If present at beginning then it ignores it completely.
^ matches search pattern at the beginning of the line. e.g. ^"search".
\< matches search pattern at the beginning of the word.
\> matches search pattern at the end of the word. 
\b matches search pattern at the beginning or end of the word. 
-c count number of lines
-e takes next param as pattern . If you want to search "-i", by default it will take -i as param, so use -e "-i"
-w Search word
. match any character with "." (dot)
-r searches recursively for files under specified directory
[ ] specify a range. ie any one of these characters
[^123] lines that doesn't contain 1,2 or 3. Not in range
[123] lines that contain 1,2 or 3
[0-9] lines that contain a number
[a-zA-Z] lines that contain a alphabet
\{2,4\} between 2 or 4 digits
\<word\> searches for specific seperate word
[0-9][0-9] matches 2 digits
\([0-9]\) matches and remembers the digit
\1 recalls the first group remembered
\([0-9]\)\1 2 same digits

Examples on the way :D

Comments

Popular posts from this blog

"Don't Ask, Tell" Principle

Dependency Inversion Principle (DIP) Explained