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/)ing' |
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 except 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. Would 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. Would 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.