• RSS
  • SmsJobs
  • Sms
  • GoogleBuzz
  • Facebook
  • Twitter
  • Linkedin
  • Youtube
The Linux Juggernaut

The Linux Juggernaut

roles on……

Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.
grep_command

In our previous post we saw how to use grep to search for words and played across different options. In this post we will see how to use Basic regular expressions to increase the power of grep command.

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.

If we learn any new thing with example, it will be there for long time in our mind. Now we will see one example each regular expression what we given above.

Example1: Find all the lines which start with “Mr”

grep ‘^Mr’ filename

Example2: Find all the lines which ends with ‘sh’

grep ‘sh$’ filename

Example3: Display all the lines in a file expect empty lines.

grep –v ‘^$’ filename

Note:-v option is used to negate the search term, here ^$ indicates empty line, so our grep –v is filtering empty lines in the output.

Example4: Search for words which are bash, baash, bsh, baaash, baaaaash,

grep ‘ba*s’ filename

This will search for words which has a between b and s zero or more times.

Example5: Search for all words which starts with b and h

grep ‘b.*h’ filename

Example6: Search for a word which is having three letters in it and starts with x and ends with m.

grep ‘x[a-z]m’ filename

This search will return all the 3 letter words which start with x and ends with m.

Example7: Search words do not contain ‘ac’ in a file.

grep ‘[^ac]’ filename

Example8: Search for a ‘[‘ in a file

Note: The “[“is a special character, you cannot search with normal grep we have to use escape character (\) in order to negate it. So use ‘\[‘ to search for [. This is applicable for all the special characters mention above.

grep ‘\[’ filename

Please feel free to comment on this, if you have more thoughts. Stay tuned to our next grep post on how to use extended regular expressions.

Do you like www.linuxnix.com ? Please consider supporting us by becoming a subscriber and get a Linux basics e-book for free.
bySurendra Anne On Wednesday-August - 3 - 2011

Categorie

Uncategorized

Tags

If you liked this article, you might like these as well..

  • No related posts found

email newsletter

Redhat Flagship dist

Redhat announced that they are going to release RHEL7 by ...

12 examples to use f

Flower brackets examples This is a small post on how to ...

7 Linux read command

read command examples read command is useful in scripts when reading ...

7 linux sort command

Linux/Unix sort command examples This is a small tutorial on how ...

How to find command

How to find command location in Linux and Unix This is ...

  • About Linux Juggernaut

    How the blog name selected? Juggernaut: * a massive inexorable force that seems to crush everything in its way ...readmore>>

  • Contact Me

    Contact us Send an Email Phone number and vCard LinkedIn profile Advertise with us
  • About Me

    Surendra Kumar Chowdary

    My name is Surendra Kumar Anne. I am from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present i work at Persistent Systems as Module Lead.   read more »