2 Why Regular Expressions? Regular expressions are used to describe text patterns/filtersUnix commands/utilities that support regular expressions:grep(fgrep, egrep) - search a file for a string or regular expressionsed - stream editorawk (nawk) - pattern scanning and processing languageThere are some minor differences between the regular expressions supported by these programsWe will cover the general matching operators first.
3 Character Class  matches any of the enclosed chars [abc] matches a single a b or c[a-z] matches any of abcdef…xyz[^A-Za-z] matches a single character as long as it is not a letter.Example: [Dd][Aa][Vv][Ee]Matches "Dave" or "dave" or "dAVE",Does not match "ave" or "da"Exactly one character from the possibilities
4 Regular Expression Operators Any character (except a metacharacter!) matches itself.. Matches any single character except newline.* Matches 0 or more of the immediately preceding R.E.? Matches 0 or 1 instances of the immediately preceding R.E.+ Matches 1 or more instances of immediately preceding R.E.^ Matches the preceding R.E. at the beginning of the line$ Matches the preceding R.E. at the end of the line| Matches the R.E. specified before or after this symbol\ Turn off the special meaning
5 Examples of R.E. x[abc]?x matches "xax" or "xx“ [abc]* matches "aaaaa" or "acbca"0*10 matches "010" or " "or "10"^(dog)$ matches lines starting and ending with dog[\t ]*(A|a)+b*c?
6 Grouping with parensIf you put a subpattern inside parens you can use + * and ? to the entire subpattern.a(bc)*d matches "ad" and "abcbcd"does not match "abcxd" or "bcbcd"
7 Example Christian Scott lives here and will put on a Christmas party There are around 30 to 35 people invited.They are:TomDanRhonda SavageNicky and Kimberly.Steve, Suzanne, Ginger and Larry^[A-Z]..$ ^[A-Z][a-z]*3[0-5] [a-z]*\.^ *[A-Z][a-z][a-z]$ ^[A-Z][a-z]*[^,][A-Za-z]*$
8 Review: Metacharacters for filename abbreviation * Matches anything: ls Test*.doc? Matches any single characterls Test?.doc[abc…] Matches any of the enclosed characters: ls T[eE][sS][tT].doc[a-z] matches any character in a rangels [a-zA-Z]*[!abc…] matches any character except those listed: ls [!0-9]*
9 Difference !!Although there are similarities to the metacharacters used in filename expansion – we are talking about something different!Filename expansion is done by the shell.Regular expressions are used by commands (programs).However, be careful about specifying RE on the command line as a result of this overlapGood idea to always quote RE with special chars (‘’or “”)on the command lineExample:% grep ‘[a-z]*’ chap*Note: filename mask expanded by shell w/o ``
10 grep - search for a string grep [-bchilnsvw] PATTERN [filename...]Read files or standard /redirected inputSearch for specified pattern in each lineSend results to the standard outputExamples:%grep ‘^X11’ *- search all files for lines starting with the string “X11”%grep -v text file - print lines that do not match “text”
11 Regular expressions for grep c any non special character \c turn off any special meaning of character c ^ beginning of line $ end of line . any single character [...] any of characters in range .… [^....] any single character not in range .… r* zero or more occurrences of r
12 Regular Expressions for grep \< beginning of word anchor\<abc matches “abcd” but not “dabc”\> end of work anchorabc\> matches “dabc” but not “abcd”\(…\) stores the pattern …\(abc\)def matches “abcdef” and storesabc in \1. So \(abc\)def\1 matches “abcdefabc”. Can store up to 9 matches
13 grep - options Some useful options -c count number of lines -h do not display filename -l list only the files with matching lines -v display lines that do not match -n print line numbers
14 File db northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Graysouthwest SW Lewis Dalsasssouthern SO Suan Chinsoutheast SE Patricia Hemeeastern EA TB Savagenortheast NE AM Main Jrnorth NO Margot Webbercentral CT Ann Stephens
15 grep with pipes Remember, we can use pipes when a file is expected ls –l | grep ‘\<Feb.*3\>’
16 egrep Extended grep allows for more kinds of regular expressions unfortunately, egrep regular expressions are not a superset of grep regular expressionssome of grep’s regular expressions are not available in egrep
17 grep vs. egrep new to egrep only in grep f+ matches one or more occurrences of ff? matches zero or one occurrences of ff|g matches f or g(ab) groups characters a and b togetheronly in grep\( … \), \<, \>Final Note: Different versions of grep/egrep may support different expressions. Make sure to check the man pages.