Name the basic usage of grep and explain the following parameters:
grep <option> <searchpattern> inputfile</searchpattern>
</option>
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:
Explain the following regex quantifiers:
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