Saturday, May 11, 2013

Regular Expression Pattern matching (grep,egrep)

?              any single char.
[abcd]      any single char from the specified list 
[!abcd]     any single char other than one from the specified list.
[a-z]         any char between “a” and “z” inclusive 
[!a-z]        any char other than between “a” and “z” inclusive 


?
      The question mark indicates there is zero or one of the preceding element. example, colou?r matches both "color" and "colour".

* 
      The asterisk indicates there is zero or more of the preceding element.

example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.

The plus sign indicates there is one or more of the preceding element. example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".

GREP with Regular Expression

REGULAR EXPRESSIONS

.
  Matches single character
*   Any character comes 0 or more times
[] any character contains within bracket
^ starting character
$ End of the character

Examples:

file name : sample.dat

applas11i13
sunday is holiday
monday
tuesday
wednesday
thursday
friday
saturday
Sunday SUCCESS
SUNDAY 1234
SUUNDAY
this

The file content sample.dat ends with s lines only displayed

$ grep s$ sample.dat
this

The file content sample.dat must start with t lines only displayed

$ grep ^t sample.dat
tuesday
thursday
this

. (dot) place any character will come with in the file sample.dat content

$ grep fri.ay sample.dat
friday

The file content sample.dat must start with a-f  lines only displayed

$ grep ^[a-f] sample.dat
applas11i13
friday

f may come 0 or more times but that line must occurs y

$ grep f*y sample.dat
sunday is holiday
monday
tuesday
wednesday
thursday
friday
saturday
Sunday SUCCESS

The word must start with f and ends with y in between any no of characters  are allowed.

$ grep f.*y sample.dat
friday

EGREP (Extended Global Regular Expression Parser)


 grep command is used to search the contents of files that matches a particular criteria.
 grep - basic regular expressions
 egrep - uses extended regular expressions

It prints sunday or monday line in file sample.dat

$ egrep '(sun|mon)day' sample.dat
sunday is holiday
monday

It display all line which have end with day in file sample.dat
? means previous word  comes 0 or 1 times

$ egrep '(sun|mon)?day' sample.dat
sunday is holiday
monday
tuesday
wednesday
thursday
friday
saturday
Sunday SUCCESS

It shows line starts with any one of character [s or m or t] mandatory and followed by any character or word will come but it must ends with day in file sample.dat

$ egrep '^(s|m|t)+.*day$' sample.dat
sunday is holiday
monday
tuesday
thursday
saturday

It shows line starts with s or ends with s in file sample.dat

$ egrep '^(s)|s$' sample.dat
sunday is holiday
saturday
this

It shows line starts with any character except s in file sample.dat

$ egrep '^[^s]' sample.dat
applas11i13
monday
tuesday
wednesday
thursday
friday
Sunday SUCCESS
SUNDAY 1234
SUUNDAY
this

It shows line which have the word sunday or monday irrespective of case in a file sample.dat

$ egrep -iw 'sunday|monday' sample.dat
sunday is holiday
monday
Sunday SUCCESS
SUNDAY 1234

It print all lines except sunday or monday occured lines in a file sample.dat, irrespective of case

$ egrep -iv 'sunday|monday' sample.dat
applas11i13
tuesday
wednesday
thursday
friday
saturday
SUUNDAY
this

It print all lines except the word sunday or monday occured lines in a file sample.dat, irrespective of case

$ egrep -iwv 'sunday|monday' sample.dat
applas11i13
tuesday
wednesday
thursday
friday
saturday
SUUNDAY
this





No comments:

Post a Comment

Price List Query for Item

 SELECT qph.list_header_id,        qph.name,        qph.description,        qphh.start_date_active,        qphh.currency_code,        q...