grep Flashcards
grep
displays lines in a file or stream that match a pattern expressed as a regular expression
grep command syntax
grep options ‘regular expression’ file(s)
-i
ignores case for matching
-v
Doesn’t display lines matching expression
-n
Displays line and n lines after matching lines (linux)
-c
Displays count of number of occurrences
-l
Displays list of filenames only
-e exp
Specifies given expression and can use multiple times
-x
Matches pattern with entire line (doesn’t match embedded patterns)
-f file
Takes pattern from file, one per line
-E or egrep
Treats pattern as an extended regular expression (ERE)
-F
Matches multiple fixed strings (in fgrep-style)
-A n
Display line and n lines after matching lines (linux)
-B n
Display line and n lines before matching line (linux)
matches a pattern at the beginning of a line
The caret has a triple role to play in regular expressions. When placed at the beginning of a character class (e.g., [^a-z]), it _____ every character of the class. When placed outside it, and at the beginning of the expression (e.g., ^2…), the pattern is matched at the ____ of the line. At any other location (e.g., a^b), it matches ____ literally.
The caret has a triple role to play in regular expressions. When placed at the beginning of a character class (e.g., [^a-z]), it negates every character of the class. When placed outside it, and at the beginning of the expression (e.g., ^2…), the pattern is matched at the beginning of the line. At any other location (e.g., a^b), it matches itself literally.
grep special character (for making Basic Regular Expressions)
The end of a line
$
*
zero or more occurrences of the previous character
grep special character
(for making Basic Regular Expressions)
a single character that is one of the ones listed in
[ ]
a single character that is one of the ones listed in
[ … ].
(similar to [ … ] for shell
file matching)
.
Matches single character
[abc]
a single character a, b, c
[c1-c2]
A single character with the ASCII range represented by c1 and c2
[^abc]
A single character that is not a, b, or
^pat
Pattern pat at the beginning of a line
pat$
Pattern pat at the end of line
-F
Matches multiple fixed strings (in fgrep-style)
A regular expression uses an elaborate metacharacter set that overshadows the shell’s ____ _____.
A regular expression uses an elaborate metacharacter set that overshadows the shell’s wild cards.
Define: A regular expression
A regular expression uses an elaborate metacharacter set that overshadows the shell’s wild cards. grep uses this expression to match multiple similar patterns
Unlike wild cards, however, a regular expression is a feature of the _____ that uses it and has nothing to do with the _____.
Quoting ensures that the _____ isn’t able to interfere and interpret the metacharacters in its own way.
Regular expressions are interpreted by the command and not by the shell.
Quoting ensures that the shell isn’t able to interfere and interpret the meta-characters in its own way.
How does the command interpret the the following BRE:
g*
Nothing or g, gg, ggg, etc
How does the command interpret the the following BRE:
gg*
g, gg, ggg, etc
How does the command interpret the the following BRE:
.*
Any number of characters, or none
How does the command interpret the the following BRE:
[1-3]
A digit between 1 and 3
How does the command interpret the the following BRE:
[^a-zA-Z]
A non alphanumeric character
How does the command interpret the the following BRE:
bash$
bash at the end of a line
How does the command interpret the the following BRE:
^bash$
bash as the only word in line
How does the command interpret the the following BRE:
^$
Lines containing nothing
Define: a character class
A regular expression lets you specify a group of characters enclosed within a pair of rectangular brackets, [ ]. The match is then performed for any single character in the group. This form resembles the one used by the shell’s wild cards.
match a string beginning with e
ee*
How do you match trueman and truman from emp.1st
grep “true*man” emp.lst
How do you match match wilcocks and wilcox from emp.1st
grep “wilco[cx]ks” emp.lst
The expression ks means that k and s may not occur at all (or as many times as possible); that’s why the expression used with grep also matches wilcox.
$
Matches a pattern at the end of a line
Extended regular expressions (ERE) make it possible to
match dissimilar patterns
with a single expression.
The ERE set includes two special characters, (?) and (?). They are often used in place of the * to restrict the matching scope
+ & -
(egrep) +
Matches one or more occurrences of the previous character.
(egrep) ?
Matches zero or one occurrence of the previous character.
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
ch+
Matches one or more occurrences of character ch
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
ch?
Matches zero or more occurrence of character ch
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
exp1|exp2
Matches exp1 or exp2
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
(x1|x2)x3
Matches x1x3 or x2x3
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
g+
Matches at least one g
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
g?
Matches at least one g
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
g?
Matches nothing or one g
The Extended Regular Expression (ERE) Set Used by grep, egrep and awk
GIF|JPEG
Matches GIF or JPEG
(lock|ver) wood
Matches lockwood or verwood