Tuesday, December 11, 2012

Regular Expression




Some Reg Expresessions:-

.  -> Matches any one character.

eg:-

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html

[oracle@rhel ~]$ grep "Atest." manzoor.txt
Atestexample.html


[...] ->  Matches any of the character listed in the bracket.

eg:-

[oracle@rhel ~]$ grep "[C]" manzoor.txt
Ctestexample.html

[oracle@rhel ~]$ grep "[A]" manzoor.txt
Atestexample.html

[^...] -> Matchesh any character except those listed in the bracket.


eg:-

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames


[oracle@rhel ~]$ grep "hostname[^0-9]" manzoor.txt
hostnames

--Here on the above we want to find out all the word which starts with hostname and not end with the number.



-  -> Range operator

eg:-

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0


[oracle@rhel ~]$ grep "[0-9]" manzoor.txt
hostname1
hostname2
hostname3
hostname9
hostname0

[oracle@rhel ~]$ grep "[A-Z]" manzoor.txt
Atestexample.html
Ctestexample.html


[oracle@rhel ~]$ grep "[a-z]" manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0

[oracle@rhel ~]$ grep "[0-9A-Za-z]" manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0

+ - Matches any character zero or one time.

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1
eleeeephant2

[oracle@rhel ~]$ egrep "el[e]+phant[0-9]" manzoor.txt
eleephant0
elephant1
eleeeephant2

-- Here we doensn't know the no. of occurenance of e, in that case we can use + which will check for the e for any no. of instance.



* - Matches the series of character.

eg:-

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1


[oracle@rhel ~]$ grep "el.*[0-9]" manzoor.txt
eleephant0
elephant1


{num} - Matchesh the preceding element num times.

eg:-

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1
eleeeephant2


[oracle@rhel ~]$ egrep "hostname[0-9]{3}" manzoor.txt

hostname1
hostname2
hostname3


{minnum,maxnum} - Matches the preceding for the minimum and the max.


[oracle@rhel ~]$ egrep "hostname[0-9]{3,4}" manzoor.txt

hostname1
hostname2
hostname3
hostname9

--It will matches the preceding minimum for the 3 time but not more than the 4th time.


^ - Matches at the start of the line.

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1
eleeeephant2


[oracle@rhel ~]$ egrep "^[b]" manzoor.txt
btestexpample.html


[oracle@rhel ~]$ cat manzoor.txt | sed 's/^/host/'
hostAtestexample.html
hostbtestexpample.html
hostCtestexample.html
hosthostname1
hosthostname2
hosthostname3
hosthostname9
hosthostname0
hosthostnames
hosteleephant0
hostelephant1
hosteleeeephant2

[oracle@rhel ~]$ cat manzoor.txt | egrep "btest*" |sed 's/^b/B/'
Btestexpample.html


$ - Matches at the end of the line.

[oracle@rhel ~]$ grep "l$" manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html

[oracle@rhel ~]$ grep "[0-9]$" manzoor.txt
hostname1
hostname2
hostname3
hostname9
hostname0
eleephant0
elephant1
eleeeephant2

[oracle@rhel ~]$ cat manzoor.txt | tr [a-z] [A-Z]
ATESTEXAMPLE.HTML
BTESTEXPAMPLE.HTML
CTESTEXAMPLE.HTML
HOSTNAME1
HOSTNAME2
HOSTNAME3
HOSTNAME9
HOSTNAME0
HOSTNAMES
ELEEPHANT0
ELEPHANT1
ELEEEEPHANT2

[oracle@rhel ~]$ cat manzoor.txt | egrep "[0-9]$" | tr [a-z] [A-Z]
HOSTNAME1
HOSTNAME2
HOSTNAME3
HOSTNAME9
HOSTNAME0
ELEEPHANT0
ELEPHANT1
ELEEEEPHANT2

-- Here we are filtering anywords which ends with number and then we translating to upper character.

[oracle@rhel ~]$ cat manzoor.txt | egrep "[0-9]$" | egrep ^h | tr [a-z] [A-Z]
HOSTNAME1
HOSTNAME2
HOSTNAME3
HOSTNAME9
HOSTNAME0

-- Here we are filter any words which end with number and then starts with h and we are converting that to uppercharacter.

\< - Matches any word at the begining.


eg:-

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1
eleeeephant2
this is a testing for regexp.
this is also a testing for regexp.
yes this is a good test.

[oracle@rhel ~]$ cat manzoor.txt | grep "\this is a testing for regexp.
this is also a testing for regexp.
yes this is a good test.

[oracle@rhel ~]$ cat manzoor.txt | grep "\yes this is a good test.


\> - Matches at the end of the word

eg:-

[oracle@rhel ~]$ cat manzoor.txt | grep "exp\>"
this is a testing for regexp.
this is also a testing for regexp.

[oracle@rhel ~]$ cat manzoor.txt | grep "st\>"
yes this is a good test.


\b - Matches any character at the begining or the end of a word.

[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1
eleeeephant2
this is a testing for regexp.
this is also a testing for regexp.
yes this is a good test.
hi what it thethis.


[oracle@rhel ~]$ cat manzoor.txt | egrep "\bthis"
this is a testing for regexp.
this is also a testing for regexp.
yes this is a good test.

[oracle@rhel ~]$ cat manzoor.txt | egrep "\Bthis"
hi what it thethis.



\ - Escape character.


[oracle@rhel ~]$ cat manzoor.txt
Atestexample.html
btestexpample.html
Ctestexample.html
hostname1
hostname2
hostname3
hostname9
hostname0
hostnames
eleephant0
elephant1
eleeeephant2
this is a testing for regexp.
this is also a testing for regexp.
yes this is a good test.
hi what it thethis.
this is the five***** rating given.
what is the five..... mean.


[oracle@rhel ~]$ grep "\*" manzoor.txt
this is the five***** rating given.


Alternation:-

| -> Symbol indicates logical OR.

eg:-

[oracle@rhel ~]$ egrep "five*****|five....." manzoor.txt
this is the five***** rating given.
what is the five..... mean.


[oracle@rhel ~]$ egrep "ele(e|p)*" manzoor.txt
eleephant0
elephant1
eleeeephant2

No comments:

Post a Comment