Examples of Searching
When used with grep or egrep, regular expressions should be surrounded by quotes. (If the pattern contains a $, you must use single quotes; e.g., '
pattern
'.) When used with ed, ex, sed, and awk, regular expressions are usually surrounded by /, although (except for awk) any delimiter works. Tables 6-6 through Table 6-9 show some example patterns.
|
Pattern |
What does it match? |
|
bag |
The string |
|
^bag |
|
|
bag$ |
|
|
^bag$ |
|
|
[Bb]ag |
|
|
b[aeiou]g |
|
|
b[^aeiou]g |
|
|
b.g |
|
|
^...$ |
Any line containing exactly three characters. |
|
^\. |
Any line that begins with a dot. |
|
^\.[a-z][a-z] |
Same, followed by two lowercase letters (e.g., troff requests). |
|
^\.[a-z]\{2\} |
Same as previous; ed, grep, and sed only. |
|
^\[^.] |
Any line that doesn’t begin with a dot. |
|
bugs* |
|
|
“word” |
A word in quotes. |
|
“*word"* |
A word, with or without quotes. |
|
[A-Z][A-Z]* |
One or more uppercase letters. |
|
[A-Z]+ |
Same; egrep or awk only. |
|
[[:upper:]]+ |
Same; POSIX egrep or awk. |
|
[A-Z].* |
An uppercase letter, followed by zero or more characters. |
|
[A-Z]* |
Zero or more uppercase letters. |
|
[a-zA-Z] |
Any letter. |
|
[^0-9A-Za-z] |
Any symbol or space (not a letter or a number). |
|
[^[:alnum:]] |
Same, ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access