-
BASH > grep
grep <options> pattern <file...>
Matching options
-e, --regexp=PATTERN -f, --file=FILE -i # ignore-case -v, --invert-match -w, --word-regexp -x, --line-regexp
Pattern options
-F # list of fixed strings -G # basic regular expression (default) -E # extended regular expression
REGEX
caracters have a special meaning unless they are escaped with a backslash:
^ $ . * [ ] \caracters do not have any special meaning unless they are escaped with a backslash:
? + { } | ( )-Ecaracters a special meaning unless they are escaped with a backslash: ^ $ . * + ? [ ] ( ) | { }Output Options
-c # print the count of matching lines. suppresses normal output -m, --max-count=NUM # stop reading after max count is reached -o # only print the matched part of a line -q # quiet, silent -s # suppress error messages about nonexistent or unreadable files
Context Options
-B NUM # print NUM lines before a match -A NUM # print NUM lines after a match -C NUM # print NUM lines before and after a match
Examples
grep -E "foo|oof" bar.txt # match any line in bar.txt that contains either "foo" or "oof" # match anything that resembles a URL in foo.txt and only print out the match grep -oE "https?:\/\/((\w+[_-]?)+\.?)+" foo.txt # follow the tail of server.log, pipe to grep and print out any line that contains "error" # and include 5 lines of context tail -f server.log | grep -iC 5 error