Previous | Table of Contents | Next |
SPECIAL REGULAR EXPRESSIONS
These regular expressions are used by the ex/vi editor.
Empty (Null) Regular Expression
// | The last RE is used if it has been defined. For example:
/John/ //The first line searches for the specified pattern John. The second line repeats the search for John. |
Special Substitute Characters
There are special characters that you can use in the replacement (new) string of a substitute command.
|
|
Special Character |
Description |
---|---|
|
|
& | Matches last search or substitution or old string. For example,
The born is doing fine. :s/born/new&/p The newborn is doing fine. |
or |
My name is Tom. :s/Tom/&my/p My name is Tommy.The first command substitutes born to be newborn. The second substitutes Tom to be Tommy. |
~ | Matches last replacement or new string. For example:
:s/finish/end/ :s/stop/~/The first line substitutes end in place of finish. The second command substitutes the last replacement string end in place of stop. |
\l | Converts the next character in the replacement string to lowercase. For
example,
:s/A big/\l&/p a bigThe substitute command replaces the string A big with the string a big. |
\L | Converts all following characters of the replacement string to lowercase
until a \e or \E is encountered. Example:
:s/THIS/\L&\E/p this |
\u | Converts the next character in the replacement string to uppercase. |
\U | Converts all following characters of the replacement string to uppercase until a \e or \E is encountered. |
\n | The nth grouped regular expression \(RE\). For example,
This is easy. :s/\(this\) is \(easy\)/\2 is \1/ easy is this. |
\E or \e | Turn off the action of the \U or \L strings. |
Multiple regular expressions may be used in a pattern. Some versions of vi do not support all the features of regular expressions shown here.
ESCAPE SEQUENCES
The following sequences of characters are referred to as escape sequences. Escape sequences specify a notation for characters that are not easily produced from your keyboard and/or cause problems when displayed to your screen. The nawk and echo commands use escape sequences. Although only nawk uses the escape sequences in regular expressions.
|
|
Sequence | Description |
---|---|
|
|
\b | Generates a backspace. |
\f | Generates a form feed. |
\n | Generates a carriage return. |
\r | Generates a new-line |
\t | Generates a tab |
\0ddd | Generates the ASCII character for the given octal ASCII code. |
\c | Generates the literal character c. |
CONCATENATION OF REGULAR EXPRESSIONS
Regular expressions can be placed beside each other to form more complex regular expressions. The following is a list of examples that describe some of the ways you can concatenate regular expressions.
.* | Matches the longest possible string of characters. |
t.*t | Matches the longest possible string between two ts. For example:
So this is the end of that.matches this is the end of that but not this is t. |
t[^t]*t | Matches the shortest possible string beginning and ending with a t. The [^t] means not to match a t. In the previous example this is t is matched. |
^\. | Matches any line beginning with a period. |
^[^Tab] | Matches the beginning of a line that does not begin with a tab. |
^$ | Matches a blank line containing no characters. Match beginning of line and end of line. |
^ *$ | Matches a blank line containing only blank characters. |
[0-9a-zA-Z] | Matches a single digit, lowercase character, or uppercase character. |
[0-9]{9} | Matches any nine consecutive digits. |
[01]{8} | Matches any eight consecutive ones or zeros. |
[Tt]he | Matches The or the. |
(bee)+ | Matches one or more occurrences of the string bee. It matches bee, beebee, and so on but does not match be or beee. |
(be?)+ | Matches one or more occurrences of the string be?. Thus possible matches are be, bee, bebe, beebee, or bebee. |
( I )|( Me ) | Matches either " I " or " Me ." |
APPLICATIONS
Regular expressions provide a shorthand for matching desired text patterns. Instead of having to type 20 different strings to search for in a file, you might be able to use regular expressions and only enter two or three regular expression strings.
You may also find regular expressions useful for defining strings you do not want to have selected. So they are helpful in selecting and rejecting various strings of text.
Regular expressions are used by the nawk, ed, egrep, ex, expr, grep, pg, sed, sh, and vi commands. By using regular expressions you can increase the power and effectiveness of these UNIX commands, thus increasing your productivity by better utilizing the features offered by each command.
TYPICAL OPERATIONS
In this activity you use the egrep and the ksh to utilize regular expressions. Begin at the shell prompt.
$ egrep "^Some" db/phone Someone: 916 Hawk Street::Austin:Texas:78749
Previous | Table of Contents | Next |