< Day Day Up > |
3.2. Combining Regular Expression MetacharactersNow that basic regular expression metacharacters have been explained, they can be combined into more complex expressions. Each of the regular expression examples enclosed in forward slashes is the search string and is matched against each line in the text file. Example 3.9.
Note: The line numbers are NOT part of the text file. The vertical bars mark the left and
right margins.
---------------------------------------------------------------
1 |Christian Scott lives here and will put on a Christmas party.|
2 |There are around 30 to 35 people invited. |
3 |They are: |
4 | Tom|
5 |Dan |
6 | Rhonda Savage |
7 |Nicky and Kimberly. |
8 |Steve, Suzanne, Ginger and Larry. |
---------------------------------------------------------------
EXPLANATION
3.2.1 More Regular Expression MetacharactersThe following metacharacters are not necessarily portable across all utilities using regular expressions, but can be used in the vi editor and some versions of sed and grep. There is an extended set of metacharacters available with egrep and awk, which will be discussed in later sections. Example 3.10.(Beginning-of-word (\<) and end-of-word (\>) anchors) % vi textfile ------------------------------------------------------------- Unusual occurrences happened at the fair. --> Patty won fourth place in the 50 yard dash square and fair. Occurrences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. ~ ~ ~ /\<fourth\>/ ------------------------------------------------------------- EXPLANATION Will find the word fourth on each line. The \< is the beginning-of-word anchor and the \ > is the end-of-word anchor. A word can be separated by spaces, end in punctuation, start at the beginning of a line, end at the end of a line, and so forth. Example 3.11.% vi textfile ------------------------------------------------------------- Unusual occurrences happened at the fair. --> Patty won fourth place in the 50 yard dash square and fair. Occurrences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. --> Guy fell down while running around the south bend in his last event. ~ ~ ~ /\<f.*th\>/ ------------------------------------------------------------- EXPLANATION Will find any word (or group of words) beginning with an f, followed by zero or more of any character (.*), and a string ending with th. Example 3.12.(Remembered patterns \( and \)) % vi textfile (Before substitution) ------------------------------------------------------------- Unusual occurences happened at the fair. Patty won fourth place in the 50 yard dash square and fair. Occurences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. ~ ~ ~ 1 :1,$s/\([0o]ccur\)ence/\1rence/ ------------------------------------------------------------- % vi textfile (After substitution) ------------------------------------------------- --> Unusual occurrences happened at the fair. Patty won fourth place in the 50 yard dash square and fair. --> Occurrences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. ~ ~ ~ ------------------------------------------------------------- EXPLANATION
Example 3.13.% vi textfile (Before substitution) ------------------------------------------------------------- Unusual occurrences happened at the fair. Patty won fourth place in the 50 yard dash square and fair. Occurrences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. ~ ~ ~ Occurrences like this are rare. The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. ~ ~ ~ ------------------------------------------------------------- EXPLANATION
Example 3.14.(Repetition of patterns ( \{n\} )) % vi textfile ------------------------------------------- Unusual occurrences happened at the fair. Patty won fourth place in the 50 yard dash square and fair. Occurrences like this are rare. --> The winning ticket is 55222. The ticket I got is 54333 and Dee got 55544. Guy fell down while running around the south bend in his last event. ~ ~ ~ ~ 1 /5\{2\}2\{3\}\./ ------------------------------------------------------------- EXPLANATION |
< Day Day Up > |