Grep "Global Regular Expression Print"
Basic regular expressions:
^ -- Caret symbol, Match beginning of the line.
$ -- Match End of the line
* -- Match 0 or more occurrence of previous character
. – Match Any single character
[ ] – Match Range of characters, just single occurrence.
[a-z] –Match small letters
[A-Z] –Match cap letters
[0-9] – Match numerical.
[^] – Match Negate a sequence
\ -- Match Escape character.
Find directories that have reached more then 50%
df -kl|grep "5[0-1]%"
/dev/sda5 195069136 94201000 91102556 51% /
grep -w 'developed' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
grep '\<developed\>' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
grep '\<de..loped\>' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
The following will match any row of at least two letters 'p' and 'l'
^ -- Caret symbol, Match beginning of the line.
$ -- Match End of the line
* -- Match 0 or more occurrence of previous character
. – Match Any single character
[ ] – Match Range of characters, just single occurrence.
[a-z] –Match small letters
[A-Z] –Match cap letters
[0-9] – Match numerical.
[^] – Match Negate a sequence
\ -- Match Escape character.
Extended regular expressions:
+ --Match one or more occurrences of previous character.
| -- Match Either character
? – Match 0 or 1 occurrence of previous character.
( ) –match a group of characters
{number} –Match number of occurrence of a character
{1, 3} –Match a character which is 1 to 3 times repetition
{5, } –Match a repeated character which is repeated 5 or more times.
\{x,y\} -Match x to y occurrences of the preceding.
\{x\} -Match exactly x occurrences of the preceding.
\{x,\} -Match x or more occurrences of the preceding.
grep '^From: ' /usr/mail/$USER -List your mail
grep '[a-zA-Z]' -Any line with at least one letter
grep '[^a-zA-Z0-9] -Anything not a letter or number
grep '[0-9]\{3\}-[0-9]\{4\}' -> 999-9999, like phone numbers
grep '^.$' -lines with exactly one character
grep '"ashish"' -'ashish' within double quotes
grep '"*ashish"*' -'ashish', with or without quotes
grep '^\.' -Any line that starts with a Period "."
grep '^\.[a-z][a-z]' -Line start with "." and 2 lc letters
# Count the number of ashish in all .txt files
grep -c ashish *.txt
# Print lines that DO NOT have "ashish"
grep -v ashish *.txt
# List processes that match "ashish"
Use [ ] around the first letter to omit the 'grep' process or use of grep -v grep.
ps -ef | grep [a]shish
#To search for a $ (dollar sign) in the file named test2, enter:
grep \\$ ashish.txtThe \\ (double backslash) characters are necessary in order to force the shell to pass a \$ (single backslash, dollar sign) to the grep command. The \ (single backslash) character tells the grep command to treat the following character (in this example the $) as a literal character rather than an expression character. Use the fgrepcommand to avoid the necessity of using escape characters such as the backslash.grep Search a Pa tern from current directory. egrep (grep -E in linux) is extended grep where additional regular expression metacharacters have been added like +, ?, | and (). fgrep (grep -F in linux) is fixed or fast grep and behaves as grep but does not recognise any regular expression metacharacters as being special.
Extended Regular Expression + ? \< \> ( ) |
Notation with egrep
An enhanced version of regular expressions allows for a few more useful features. Where these conflict with existing notation, they are only available through the egrep command.
- +
- is analogous to \{1,\}. It does the same as * but matches one or more characters instead of zero or more characters.
- ?
- is analogous to \{1\}. It matches zero or one character.
- \< \>
- can surround a string to match only whole words.
- ( )
- can surround several strings, separated by |. This notation will match any of these strings. ( egrep only.)
- \( \)
- can surround several strings, separated by \|. This notation will match any of these strings. ( grep only.)
The following examples should make the last two notations clearer.
- grep 'trot'
- Matches the words electrotherapist, betroth, and so on, but
- grep '\<trot\>'
- matches only the word trot.
- egrep -w '(this|that|c[aeiou]*t)'
- Matches the words this, that, cot, coat, cat, and cut.
cat telephone 123-4567 7890-123 grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" telephone 123-4567
Find directories that have reached more then 50%
df -kl|grep "5[0-1]%"
/dev/sda5 195069136 94201000 91102556 51% /
cat textfile2.txt
Linux is, in simplest terms, an operating system.
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
One of the most noted properties of Linux is where it can be used.
Linux is, in simplest terms, an operating system.
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
One of the most noted properties of Linux is where it can be used.
grep 'develop' textfile2.txt (Will grep developed and development)
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
grep '\<developed\>' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
grep '\<de..loped\>' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
Within a bracket expression, the name of a character class enclosed in "[:" and ":]" stands for the list of all characters belonging to that class. Standard character class names are:
- [:alnum:] - Alphanumeric characters.
- [:alpha:] - Alphabetic characters
- [:blank:] - Blank characters: space and tab.
- [:digit:] - Digits: '0 1 2 3 4 5 6 7 8 9'.
- [:lower:] - Lower-case letters: 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.
- [:space:] - Space characters: tab, newline, vertical tab, form feed, carriage return, and space.
- [:upper:] - Upper-case letters: 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.
In this example match all upper case letters:
grep '[:upper:]' filename The following will match any row of at least two letters 'p' and 'l'
egrep 'p{2,}' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
egrep 'l{2,}' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
egrep 'l{2,}' textfile2.txt
As an open operating system, Linux is developed collaboratively, meaning no one company is solely responsible for its development or ongoing support.
Comments
Post a Comment