grep Flashcards

1
Q

grep

A

displays lines in a file or stream that match a pattern expressed as a regular expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

grep command syntax

A

grep options ‘regular expression’ file(s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

-i

A

ignores case for matching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

-v

A

Doesn’t display lines matching expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

-n

A

Displays line and n lines after matching lines (linux)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

-c

A

Displays count of number of occurrences

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

-l

A

Displays list of filenames only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

-e exp

A

Specifies given expression and can use multiple times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

-x

A

Matches pattern with entire line (doesn’t match embedded patterns)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

-f file

A

Takes pattern from file, one per line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

-E or egrep

A

Treats pattern as an extended regular expression (ERE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

-F

A

Matches multiple fixed strings (in fgrep-style)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

-A n

A

Display line and n lines after matching lines (linux)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

-B n

A

Display line and n lines before matching line (linux)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A

matches a pattern at the beginning of a line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

grep special character (for making Basic Regular Expressions)

The end of a line

A

$

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

*

A

zero or more occurrences of the previous character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

grep special character

(for making Basic Regular Expressions)

a single character that is one of the ones listed in

A

[ ]

a single character that is one of the ones listed in
[ … ].

(similar to [ … ] for shell
file matching)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

.

A

Matches single character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

[abc]

A

a single character a, b, c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

[c1-c2]

A

A single character with the ASCII range represented by c1 and c2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

[^abc]

A

A single character that is not a, b, or

24
Q

^pat

A

Pattern pat at the beginning of a line

25
Q

pat$

A

Pattern pat at the end of line

26
Q

-F

A

Matches multiple fixed strings (in fgrep-style)

27
Q

A regular expression uses an elaborate metacharacter set that overshadows the shell’s ____ _____.

A

A regular expression uses an elaborate metacharacter set that overshadows the shell’s wild cards.

28
Q

Define: A regular expression

A

A regular expression uses an elaborate metacharacter set that overshadows the shell’s wild cards. grep uses this expression to match multiple similar patterns

29
Q

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.

A

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.

30
Q

How does the command interpret the the following BRE:

g*

A

Nothing or g, gg, ggg, etc

31
Q

How does the command interpret the the following BRE:

gg*

A

g, gg, ggg, etc

32
Q

How does the command interpret the the following BRE:

.*

A

Any number of characters, or none

33
Q

How does the command interpret the the following BRE:

[1-3]

A

A digit between 1 and 3

34
Q

How does the command interpret the the following BRE:

[^a-zA-Z]

A

A non alphanumeric character

35
Q

How does the command interpret the the following BRE:

bash$

A

bash at the end of a line

36
Q

How does the command interpret the the following BRE:

^bash$

A

bash as the only word in line

37
Q

How does the command interpret the the following BRE:

^$

A

Lines containing nothing

38
Q

Define: a character class

A

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.

39
Q

match a string beginning with e

A

ee*

40
Q

How do you match trueman and truman from emp.1st

A

grep “true*man” emp.lst

41
Q

How do you match match wilcocks and wilcox from emp.1st

A

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.

42
Q

$

A

Matches a pattern at the end of a line

43
Q

Extended regular expressions (ERE) make it possible to

A

match dissimilar patterns
with a single expression.

44
Q

The ERE set includes two special characters, (?) and (?). They are often used in place of the * to restrict the matching scope

A

+ & -

45
Q

(egrep) +

A

Matches one or more occurrences of the previous character.

46
Q

(egrep) ?

A

Matches zero or one occurrence of the previous character.

47
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

ch+

A

Matches one or more occurrences of character ch

48
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

ch?

A

Matches zero or more occurrence of character ch

49
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

exp1|exp2

A

Matches exp1 or exp2

50
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

(x1|x2)x3

A

Matches x1x3 or x2x3

51
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

g+

A

Matches at least one g

52
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

g?

A

Matches at least one g

53
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

g?

A

Matches nothing or one g

54
Q

The Extended Regular Expression (ERE) Set Used by grep, egrep and awk

GIF|JPEG

A

Matches GIF or JPEG

55
Q

(lock|ver) wood

A

Matches lockwood or verwood