Linux Grep 命令指南 (grep command cheatsheet)
By Long Luo
Grep 是什么?
grep, g/re/p , (Global / Regular Expression(RE) search / and Print Out the line) ,全面搜索正则表达式并把行打印出来 ,是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。用于过滤/搜索的特定字符。可使用正则表达式能配合多种命令使用,使用上十分灵活。
grep searches each FILE for PATTERNS. PATTERNS is a string that contains one or more patterns separated by newline characters, and grep prints each line that matches a pattern. When using grep in a shell command, PATTERNS should usually be quoted.
A FILE of “-” denotes standard input. If no FILE is specified, recursive searches examine the working directory, while nonrecursive searches read standard input.
Furthermore, the variant programs egrep, fgrep, and rgrep are equivalent to grep -E, grep -F, and grep -r, respectively. These variants are obsolete, but remain available for backward compatibility.
Grep 用法
grep [OPTION…] PATTERNS [FILE…]
grep [OPTION…] -e PATTERNS … [FILE…]
grep [OPTION…] -f PATTERN_FILE … [FILE…]
WILDCARDS
. | Match any Character Except New Line |
? | Match 0 or One characters |
* | Match 0 or More characters |
+ | Match 1 or More characters |
QUANTIFIERS
{n} | Matches exactly n times |
{n,} | Matches n or More |
{,m} | Matches up to m (maximum) |
{n,m} | Matches between n and m (Minimum, Maximum) |
CHARACTER CLASSES
[A-Za-z] Any lower and upper case letters [0-9] Any Number [0-9A-Za-z] Any lower and upper case letter or digits
POSITIONS
^ | Beginning of line |
$ | End of line |
^$ | Empty line |
\< | Start of word |
\> | End of word |
OPTIONS EXAMPLES
grep ‘alias’ filname find all occurrences of a string (basic usage).
-c grep -c ‘error’ /var/log/syslog count the number of matches. -e grep -e ‘linux$’ filename use regex (lines ending with ‘Linux’). -f grep -f patternjile .bashrc take PATTERNS from FILE. -i grep -i ‘Alias’ .bashrc ignore case search. -I grep -I ‘alias’ /home/traw/* returns files with matches. -L grep -L ‘alias’ /home/traw/* returns files without matches. -r grep -r ‘error’ /var/log/log.txt recursive search (within subdirs). -v grep -v ‘warning’ /home/traw/log.txt select non-matching lines.
-m grep -m 3 ‘alias’ .bashrc limit grep Output upto 3 lines. -n grep -n ‘sudo’ .bashrc print line numbers of the matches. -o grep -o ‘search string’ .bashrc only show the matching part of the string. -x grep -x ‘linux is love’ filename match only whole lines -w grep -w “alias” .bashrc match only whole words -A grep -A 6 ‘error” error.log print 6 lines after a match -B grep -B 2 ’warning’ error.log print 2 lines before a match -C grep -C 7 ‘info’ error.log print 7 lines before and after a match -E grep -E ‘b(a|e)g’ filename Extended regex (lines containing bag or beg)
Grep 例子
grep "example" my.txt | search for “example” in “my.txt” |
grep "example" *.txt | search for “example” in all”.txt”files |
grep -i "example" my.txt | search for “example” while ignoring cases |
grep -c "example" my.txt | count # of lines that contain “example” |
grep -n "example" my.txt | show line numbers along with matched lines |
grep -r "example" . | search for “example” in all files recursively |
grep -v "example" my.txt | display lines that do not contain “example” |
grep -w "example" | search for lines containing “example” as a full word |
grep -e "key1" -e "key2" my.txt | show lines containing either pattern |
grep -v -e "key1" -e "key2" my.txt | show lines containing neither pattern |
grep "key1*key2" my.txt | display lines contain both “key1” and “key2” |
grep -E "error|warning" app.log | use extended regex for matching |
grep -E "^[a-zA-Z]" my.txt | another extended regex example |
grep -m3 "keyword" my.txt | limit grep output to a fixed number of lines |
grep -A2 -B2 "example" my.txt | show 2 lines before and after match |
grep -C3 "error" server.log | show 3 lines before and after match |
grep -x "spoofing" my.txt | show lines that exactly match a string |
grep -1 "example" * | display file names that match the pattern |
grep "^hello" my.txt | show all lines that start with “hello” |
grep "done$" my.txt | show all lines that end with “done” |
grep -o "begin.*end" my.txt | show only the matched string |
grep -color "example" my.txt | display matches with color |
grep "[0-9]" my.txt | show all lines that contain any digits |
grep -a "string" my.bin | search for a string in a binary file |
POSIX
[:alpha:] | match any alphabetical character, either lower or upper case |
[:alnum:] | matches any alphanumeric character 0-9, A-Z, or a-z |
[:digit:] | matches a numberical digit from 0 through 9 |
[[:blank:]] | matches a space or Tab character |
[[:lower:]] | matches any lowercase alphabetical character a-z |
[[:upper:]] | matches any uppercase alphabetical character A-Z |
[[:punct:]] | matches a punctuation character |
[[:print:]] | matches any printable character |
BRE, ERE & PCRE
Basic Regular Expressions (BRE) Unless they are preceded by a backslash, these characters hava a special meaning in BRE:
^ $ . * [] \
However unless they are preceded by a backslash, these characters hava no special meanings:? + { } | ( )
Extended Regular Expressions (ERE) Unless they are escaped with a backslash, ERE gives each of these characters a unique meaning:
^ $ . * + ? [ ] ( ) | { }
Perl Compatible Regular Expressions (PCRE) Additional anchors and character classes, lookahead/lookbehind, conditional expressions, comments, and other features are available in PCRE.
参考文献
https://wangchujiang.com/linux-command/c/grep.html