Monday, July 21, 2008

Pattern Matching

Pattern Matching

Shell Scripts provide the regular expression to find and replace the patterns. Below are the few regular expressions used in the Linux bash.

  1. . (dot) Matches any one character Example:

"f." Matches ‘f’ followed by any single character "a.b" matches ‘a’ followed by any single character followed by ‘b’ Matching Meta character Use “\mata character” to match the meta character

Example: "a\.b" Matches a followed by dot ( and not any single character) followed by b

  1. [] ( squre bracket)

Example: [a b c] Matches single a, b or c [a - z] Matches single character from a to z

  1. * (asterisk) Matches 0 or more occurrence of the previous pattern Example:

“a*” Matches string having any number of ‘a’ character including zero. That means it also matches the string which do not contain the ‘a’.

  1. + Matches 1 or more occurrence of the following pattern Example: "a+" Matches string having any number of ‘a’ character

The only difference between the above one (*) and this is that this do not matches the strings that doesn’t contain the pattern preceding by ‘+’

  1. ^ Matches the pattern followed by it at the beginning of the line only Example: "^The" Matches the line start with “The” String Sample.txt The game is over Linux is the best operating system

egrep –i ‘\bThe’ Sample.txt will print the following line The game is over.

  1. $ Matches the pattern preceded by it at the end of the line only Example: "End$" Matches the line end with "end" String Sample.txt It’s End It’s End now

egrep –i ‘End$’ Sample.txt will print the following line It’s End

  1. \b ( word boundary ) Matches the pattern followed by it at the starting of the word. Example: "\bThe" Matches the word start with “The” String

Sample.txt The game is over Linux is the best operating system

egrep –i ‘\bT’ Sample.txt will print the following line

The game is over Linux is the best operating system

  1. { } curly bracket
    1. {x} Matches x or more occurrence of the preceding pattern

Example: “a{2}” matches string containing 2 or more occurrence of ‘a’.

2 comments: