grep
Global Regular Expression Patterns
Options
- c : conut number of matchig lines
- i : ignore case
- l : names of files containing matches
- v : lines NOT matching regular expression
- n : prepend line number to each line
Basic Regular Expressions
- ^ : begining of line
- $ : end of line
- . : any single character
- * : zero to any number of previuos character
- .* : zero to any number of any character
- [] : any single character in list
- [-] : any single character
- [^] : any single character not in the list
Example
Create a file with the following contents:
Xcv
X
Ab5
6
Abc
Abcde5
7
aAdc
A5
6789
A567
xX
66
Run grep with the folowing pattern:
- '^.c' : 2nd char must be a c
- 'A.c' : must have an A, single charcter and a c
- '^.$' : only one single character
- '^A5$' : just has A5
- '^A.5$' : A, single character, 5
- '^A..*5$' : Starts with A, ends with 5 and has at least 3 chars
- '[67]' : either has a 6 or 7
- '[67][89]' : either has a 6 or 7 followed by a 8 or 9
- '[^0-5][6-9]' : character that isn't 0-5 followed by a 6,7,8 or 9
- '[a-zA-Z] : any alphabaetic character
- '[A-Za-z] : any alphabaetic character
- '[A-z] : any alphabaetic character
- '[a-Z] : ??? any alphabaetic character
- '[A-z]b' : alphabetic character followed by a b
- '[z-A]b' : ??? alphabetic character followed by a b
- '[A-z]b[0-9]' : alphabetic character followed by a b then followed by a number
- '[A-z]b[9-0]' : ??? alphabetic character followed by a b then followed by a number