egrep foo file returns all the lines that contain a string matching the expression "foo" in the file "file".
STDIN instead of having it search a file. For example,
ls | egrep blah lists all files in the current
directory containing the string "blah"
>cat file
big
bad bug
bag
bigger
boogy
>egrep 'b.g' file
big
bad bug
bag
bigger
the expression consisting of a character followed by a star matches any number (possibly zero) of repetitions of that character. In particular, .* matches any string, and hence acts as a "wildcard".
The File for These Examples
|
Wildcards #1
|
Wildcards #2
|
Wildcards #3
|
Frederic Smith or Fred Smith. In other
words, the letters eric are "optional".
An escaped character is a character preceded by a backslash. The preceding backslash removes an implied special meaning from a character
To search for a line containing text hello.gif, the
correct command is
egrep 'hello\.gif' file
since egrep 'hello.gif' file will match lines containing
hello-gif , hello1gif , helloagif , etc.
Fred or Frederic
an expression consisting of a character followed by a question mark matches one or zero instances of that character.
bugg?y
matches all of the following:
bugy, buggy
but not bugggy
An expression surrounded by parentheses is treated by a single character.
'Fred(eric)? Smith' matches Fred Smith or Frederic Smith
egrep 'Fred(eric)? Smith' file
'[Hh]ello'matches lines containinghelloorHello
[0-3]is the same as[0123][a-k]is the same as[abcdefghijk][A-C]is the same as[ABC][A-Ca-k]is the same as[ABCabcdefghijk]
egrep '[^aeiou]' file returns any line containing a
consonant---not very useful.
>cat file hello hello world hhello >egrep 'hello' file hello hello world hhello
The $ character matches the end of the line. The ^ character matches the beginning of the line.
egrep '^[ ]*hello[ ]*$' file
egrep '^[^aeiou]*$' file Returns all lines that contain
no vowels.
The expression consisting of two expressions separated by the or operator |
matches lines containing either of those two expressions.
egrep 'cat|dog' file
matches lines containing the word "cat" or the word "dog"
egrep 'I am a (cat|dog)' matches lines containing the string
"I am a cat" or the string "I am a dog".
? \ . [ ] ^ $ * ( )
[]12] matches ] , 1, or 2. egrep "!" file
will often produce an error (since the shell thinks that "!" is referring
to the shell command history) while egrep '!' file
will not.
egrep "$HOME" file
searches file for the name of your home directory, while
egrep '$HOME' file
searches for the string $HOME
For another source of information on using egrep see An introduction to UNIX by Dean Brock