Lect16 - Advanced String Search Flashcards

1
Q

Name the basic usage of grep and explain the following parameters:

  • -v
  • -i
  • -b
  • -o
  • –color
  • $
A

grep <option> <searchpattern> inputfile</searchpattern>
</option>

  • -v : reverse grep (show lines that do not match)
  • -i : case insensitive
  • -b : show a byte offset in the input file to the match
  • -o : show only the match
  • ^ : match the start of a line
  • $ : match the end of a line
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are extended regular expression? Explain basic usage?

A

Extended regular expressions include more metacharacters and include quantifiers that allow for more precise pattern matching.

# grep -E <options> <pattern> inputfile</pattern></options>

oder

# egrep <options> <pattern> inputfile</pattern></options>

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

Explain the following regex sets:

  1. match any of the chars a, b, or c
  2. match any chars NOT a, b, or c (note inside the brackets)
  3. match a range of chars (a through f)
  4. any letter
  5. any uppercase letter
  6. any lowercase letter
  7. any whitespace (horizontal)
  8. Any digit, 0-9
  9. Any alphanumeric character, A-Za-z0-9
A
  1. [abc]
  2. [^abc]
  3. [a-f]
  4. [[:alpha:]]
  5. [[:upper:]]
  6. [[:lower:]]
  7. [[:blank:]]
  8. [[:digit:]]
  9. [[:alnum:]]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the following regex quantifiers:

  1. 0 or more of the preceding chars
  2. 0 or 1 of the preceding chars
  3. 1 or more of the preceding chars
  4. Matches any character. e.g.: do? (hit for dog, door, dot, etc)
  5. Matches a range of characters: ?ar (matches car, bar or far)
  6. the preceding chars match exactly x number of times
  7. the preceding chars match at least x, but not more than y times
A
  1. *
  2. ?
  3. +
  4. .
  5. []
  6. {x}
  7. {x,y}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Regex examples:

U.S. Phone numbers 516-804-3222

A

’[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}’

or

’([[:digit:]]{3}-){2}-[[:digit:]]{4}’

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

Regex examples:

Matches any words that contains a, b or c

A

egrep ‘[abc]’ testgr.txt

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

Regex examples:
Matches any words that contains a, b or c once

A

egrep ‘[abc]{1}’ testgr.txt

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

Regex examples:
Match any line that contains only a, b or c

A

egrep ‘^[abc]{1}$’ testgr.txt

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

Regex examples:
Matches any line with only alphabetical characters

A

egrep ‘^[[:alpha:]]*$’ testgr.txt

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

Regex examples:
Matches either “color” or “colour”

A

egrep ‘colo[u]?r’ testgr.txt

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

Regex examples:
Matches any line with at least 3 numbers

A

egrep ‘[[:digit:]]{3}’ testgr.txt

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

Regex examples:
Matches either 202-224-1228 or (202)224-1228

A

egrep ‘(([[:digit:]]{3}))|([[:digit]]{3}-)[[:digit:]]{3}-[[:digit]]{4}’ testgr.txt

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

Regex examples:
Matches ip address

A

egrep ‘^([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}$’ testgr.txt

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