1. grep's regular expression metacharacters (which I always dismissed ~)
| [^] |
Matches one character not in the Set |
'[^ A-K] ove' |
Matches lines not containing a character in the range A through K, followed by Ove. |
| /< |
Beginning-of-word anchor |
'/<Love' |
Matches lines containing a word that begins with love. |
| /> |
End-of-word anchor |
'Love/>' |
Matches lines containing a word that ends with love. |
| /(../) |
Tags matched characters |
'/(Love/) in' |
Tags marked portion in a register to be remembered later as number 1. to reference later, use/1 to repeat the pattern. may use up to nine tags, starting with the first tag at the left-most part of the pattern. for example, the pattern love is saved in register 1 to be referenced later as/1. |
| X/{M/} X/{M,/} X/{M, N /}[A] |
Repetition of character X: m times, at least m times, or between M and N times |
'O/{5/} 'o/{5,/}' o/{5, 10 /}' |
Matches if line has 5 occurences of O, at least 5 occurences of O, or between 5 and 10 occurrences of O. |
Explanation
Prints the line if it contains the word North. The/<is the beginning-of-word anchor, and the/> is the end-of-word anchor.
4. grep's options
| Option |
What it does |
| -B |
Precedes each line by the block number on which it was found. This is sometimes useful in locating disk block numbers by context. |
| -C |
Displays a count of Matching lines rather than displaying the lines that match. |
| -H |
Does not display filenames. |
| -I |
Ignores the case of letters in making comparisons (I. e., upper -- and lowercase are considered identical ). |
| -L |
Lists only the names of files with matching lines (once), separated by newline characters. |
| -N |
Precedes each line by its relative line number in the file. |
| -S |
Works silently, that is, displays nothing cannot error messages. This is useful for checking the exit status. |
| -V |
Inverts the search to display only lines that do not match. |
| -W |
Searches for the expression as a word, as if surrounded by/<and/>. this applies to grep only. (Not all versions of grep support this feature; e.g ., sco unix does not .) |
Egrep (Extended grep)
The main advantage of using egrep is that additional regular expression metacharacters (see Table 3.4) have been added to the set provided by grep. the/(/) and/{/}, however, are not allowed. (See GNU grep-e if using Linux .)
Table 3.4. egrep's regular expression metacharacters
| Metacharacter |
Function |
Example |
What it matches |
| ^ |
Beginning-of-line anchor |
'^ Love' |
Matches all Lines beginning with love. |
| $ |
End-of-line anchor |
'Love $' |
Matches all lines ending with love. |
| . |
Matches one character |
'L. e' |
Matches lines containing an L, followed by two characters, followed by an E. |
| * |
Matches zero or more characters |
'* Love' |
Matches lines with zero or more spaces of the preceding characters followed by the pattern love. |
| [] |
Matches one character in the Set |
'[Ll] ove' |
Matches lines containing love or love. |
| [^] |
Matches one character not in the Set |
'[^ A-KM-Z] ove' |
Matches lines not containing a through K or m through Z, followed by Ove. |
| New with egrep: |
| + |
Matches one or more of the preceding characters |
'[A-Z] + ove' |
Matches one or more lowercase letters, followed by Ove. wocould find move, approve, love, behoove, etc. |
| ? |
Matches zero or one of the preceding characters |
'Lo? Ve' |
Matches for an l followed by either one or not any occurrences of the letter O. wocould find love or lve. |
| A | B |
Matches either A or B |
'Love | hate' |
Matches for either expression, love or hate. |
| () |
Groups characters |
'Love (able | ly) (OV) +' |
Matches for lovable or lovely. matches for one or more occurrences of OV. |