Escolar Documentos
Profissional Documentos
Cultura Documentos
Examples – Part I
http://www.thegeekstuff.com/2011/01/regular-expressions-in-grep-command/ January 4, 2011
You can also use regular expressions with grep command when you want to search for a
text containing a particular pattern. Regular expressions search for the patterns on each
line of the file. It simplifies our search operation.
This part 1 article covers grep examples for simple regular expressions. The future part 2
article will cover advanced regular expression examples in grep.
Let us take the file /var/log/messages file which will be used in our examples.
In grep command, caret Symbol ^ matches the expression at the start of a line. In the
following example, it displays all the line which starts with the Nov 10. i.e All the messages
logged on November 10.
The ^ matches the expression in the beginning of a line, only if it is the first character in a
regular expression. ^N matches line beginning with N.
From the above output you can come to know when all the kernel log has got terminated.
Just like ^ matches the beginning of the line only if it is the first character, $ matches the
end of the line only if it is the last character in a regular expression.
Using ^ and $ character you can find out the empty lines available in a file. “^$” specifies
empty line.
The above commands displays the count of the empty lines available in the messages and
anaconda.log files.
The special meta-character “.” (dot) matches any character except the end of the line
character. Let us take the input file which has the content as follows.
$ cat input
1. first line
2. hi hello
3. hi zello how are you
4. cello
5. aello
6. eello
7. last line
Now let us search for a word which has any single character followed by ello. i.e hello, cello
etc.,
The special character “*” matches zero or more occurrence of the previous character. For
example, the pattern ’1*’ matches zero or more ’1′.
The following example searches for a pattern “kernel: *” i.e kernel: and zero or more
occurrence of space character.
In the above example it matches for kernel and colon symbol followed by any number of
spaces/no space and “.” matches any single character.
The special character “\+” matches one or more occurrence of the previous character. ”
\+” matches at least one or more space character.
If there is no space then it will not match. The character “+” comes under extended regular
expression. So you have to escape when you want to use it with the grep command.
$ cat input
hi hello
hi hello how are you
hihello
$ grep "hi \+hello" input
hi hello
hi hello how are you
In the above example, the grep pattern matches for the pattern ‘hi’, followed by one or
more space character, followed by “hello”.
If there is no space between hi and hello it wont match that. However, * character matches
zero or more occurrence.
The special character “?” matches zero or one occurrence of the previous character. “0?”
matches single zero or nothing.
“hi \?hello” matches hi and hello with single space (hi hello) and no space (hihello).
The line which has more than one space between hi and hello did not get matched in the
above command.
If you want to search for special characters (for example: * , dot) in the content you have to
escape the special character in the regular expression.
The character class is nothing but list of characters mentioned with in the square bracket
which is used to match only one out of several characters.
Repeated messages will be logged in messages logfile as “last message repeated n times”.
The above example searches for the line which has any number (0to9) followed by the
word “times”. If it matches it displays the line before the matched line and matched line
also.
With in the square bracket, using hyphen you can specify the range of characters. Like
[0123456789] can be represented by [0-9]. Alphabets range also can be specified such as
[a-z],[A-Z] etc. So the above command can also be written as
If you want to search for all the characters except those in the square bracket, then use ^
(Caret) symbol as the first character after open square bracket. The following example
searches for a line which does not start with the vowel letter from dictionary word file in
linux.
First caret symbol in regular expression represents beginning of the line. However, caret
symbol inside the square bracket represents “except” — i.e match except everything in the
square bracket.