Lect16 - Advanced String Search Flashcards
Name the basic usage of grep and explain the following parameters:
- -v
- -i
- -b
- -o
- –color
- $
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
What are extended regular expression? Explain basic usage?
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>
Explain the following regex sets:
- match any of the chars a, b, or c
- match any chars NOT a, b, or c (note inside the brackets)
- match a range of chars (a through f)
- any letter
- any uppercase letter
- any lowercase letter
- any whitespace (horizontal)
- Any digit, 0-9
- Any alphanumeric character, A-Za-z0-9
- [abc]
- [^abc]
- [a-f]
- [[:alpha:]]
- [[:upper:]]
- [[:lower:]]
- [[:blank:]]
- [[:digit:]]
- [[:alnum:]]
Explain the following regex quantifiers:
- 0 or more of the preceding chars
- 0 or 1 of the preceding chars
- 1 or more of the preceding chars
- Matches any character. e.g.: do? (hit for dog, door, dot, etc)
- Matches a range of characters: ?ar (matches car, bar or far)
- the preceding chars match exactly x number of times
- the preceding chars match at least x, but not more than y times
- *
- ?
- +
- .
- []
- {x}
- {x,y}
Regex examples:
U.S. Phone numbers 516-804-3222
’[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}’
or
’([[:digit:]]{3}-){2}-[[:digit:]]{4}’
Regex examples:
Matches any words that contains a, b or c
egrep ‘[abc]’ testgr.txt
Regex examples:
Matches any words that contains a, b or c once
egrep ‘[abc]{1}’ testgr.txt
Regex examples:
Match any line that contains only a, b or c
egrep ‘^[abc]{1}$’ testgr.txt
Regex examples:
Matches any line with only alphabetical characters
egrep ‘^[[:alpha:]]*$’ testgr.txt
Regex examples:
Matches either “color” or “colour”
egrep ‘colo[u]?r’ testgr.txt
Regex examples:
Matches any line with at least 3 numbers
egrep ‘[[:digit:]]{3}’ testgr.txt
Regex examples:
Matches either 202-224-1228 or (202)224-1228
egrep ‘(([[:digit:]]{3}))|([[:digit]]{3}-)[[:digit:]]{3}-[[:digit]]{4}’ testgr.txt
Regex examples:
Matches ip address
egrep ‘^([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}$’ testgr.txt