Shell Programming/Regular expressions
Regular expressions are used generally to manipulate strings by external programs like find
and grep
.
Commonly used characters in regular expression
edit^ Anchor to beginning of line $ Anchor to end of line . Any single character [ ] Encloses pattern matching characters
Matching Characters
edit[:alnum:] Alphanumeric characters [:alpha:] Letters [:ascii:] ASCII characters [:blank:] Space or tab [:cntrl:] ASCII control characters [:digit:] Digits [:graph:] Noncontrol, nonspace characters [:lower:] Lowercase letters [:print:] Printable characters [:punct:] Punctuation characters [:space:] Whitespace characters, including vertical tab [:upper:] Uppercase letters [:xdigit:] Hexadecimal digits
Extended grep mode
editThe -E mode may be used in grep with the following characters preceded by '\'.
? Match is optional but may be matched at most once "*" Must be matched zero or more times (without "") + Must be matched one or more times {n} Must be matched n times {n, } Must be matched n or more times {n,m} Must be matched between n and m times
Examples of regular expression in use
edit- Look for text in file, example_text_file, ending with 'e':
grep e$ example_text_file
- Look for:
Crazy Monkey Crazy Donkey Cranky Money
in another_text_file:
grep -E Cra..\*[[:space:]][[:print:]]o... another_text_file
The above command tells grep to use extended mode, find "Cra", followed by any number of string, followed by space, followed by printable character, followed by "o", followed by three characters, and search this in another_text_file.