I know there is a lot of documentation on the topic of regular expression (like the Regexp bible, Mastering Regular Expressions, by
Jeffrey Friedl ) so just some short examples I have used:
- ls -l |egrep '.*\.[ch]p{0,2}$'
Matches strings with a character repeated any number of times (0+), followed by the '.' character which is escaped(to not be interpreted as the '.' which represents any character), then 'c' or 'h' followed by 0, 1, or 2 'p's. '$' means 'end of line' (the caret ('^') could be used similarly to mark the begging of line).
But this would also match files like "main.cp" which is not what I want so:
- ls -l |egrep '.*\.(c|cpp|h|hpp)$'
Matches just specific extensions (enclosed in "(...)").
- ls -l |egrep -v '.*\.[ch]p{0,2}$'
The '-v' parameter excludes the lines matching this pattern
- ls -l |egrep -w "file123"
Matches word "file123", but not "file1234" or "file12345". Full description from man grep :
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.
- grep -A n -B m 'pattern'
Shows the lines matching the pattern, and also n lines after, and m lines before the match.
Good references:
Enjoy! :)
No comments:
Post a Comment