深入理解grep
相关文章
简介
GNU Grep : Print Lines that match patterns
GREP(1) BSD General Commands Manual GREP(1)
NAME
grep, egrep, fgrep, zgrep, zegrep, zfgrep -- file pattern searcher
SYNOPSIS
grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file]
[--binary-files=value] [--color[=when]] [--colour[=when]] [--context[=num]] [--label]
[--line-buffered] [--null] [pattern] [file ...]
DESCRIPTION
The grep utility searches any given input files, selecting lines that match one or more
patterns. By default, a pattern matches an input line if the regular expression (RE) in
the pattern matches the input line without its trailing newline. An empty expression
matches every line. Each input line that matches at least one of the patterns is written
to the standard output.
Benchmarks
bbs_list
each row is
a computer bulletin board
phone number
baud rate
a code
.
aardvark 555-5553 1200/300 B
alpo-net 555-3412 2400/1200/300 A
barfly 555-7685 1200/300 A
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
macfoo 555-6480 1200/300 A
sdace 555-3430 2400/1200/300 A
sabafoo 555-2127 1200/300 C
Program
grep [option…] [patterns] [file…]
Options | 备注 | Demo |
---|---|---|
--help | print some usage message | grep --help |
-V / --version | print version number | grep -V |
-e | --regexp=patterns | grep -e aa bbs_list |
-i | --ignore-case | grep -i Aa bbs_list |
-v | print non-matching lines | grep -v aa bbs_list |
-w | matching word | grep -w core bbs_list |
-x | matching all line | grep -x ' core 555-2912 1200/300 C' bbs_list |
-c | count match | grep -c '1200' bbs_list |
-l | 列出文件内容符合指定的样式的文件名称 | grep -l '1200' bbs_list |
-L | 列出文件内容不符合指定的样式的文件名称 | grep -l '1200' bbs_list |
-m num | --max-count=num | grep -m 2 '1200' bbs_list |
-A num | 附带输出匹配后几行 | grep -A 2 'core' bbs_list |
-B num | 附带输出匹配前几行 | grep -B 2 'core' bbs_list |
-C num | 附带输出匹配前后几行 | grep -C 2 'core' bbs_list |
--color | 高亮匹配 | grep --color '555' bbs_list |
GREP_COLOR=32 | 改变高亮颜色 | GREP_COLOR=32 grep --color '555' bbs_list |
eg1. --help
a123456@lucky ~ % grep --help
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
eg2. --version
a123456@lucky ~ % grep --version
grep (BSD grep) 2.5.1-FreeBSD
eg3. -e
a123456@lucky linux % grep -e aa bbs_list
aardvark 555-5553 1200/300 B
a123456@lucky linux %
eg4. -i
a123456@lucky linux % grep -i Aa bbs_list
aardvark 555-5553 1200/300 B
a123456@lucky linux % grep Aa bbs_list
a123456@lucky linux %
eg5. -v
a123456@lucky linux % grep -v aa bbs_list
alpo-net 555-3412 2400/1200/300 A
barfly 555-7685 1200/300 A
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
macfoo 555-6480 1200/300 A
sdace 555-3430 2400/1200/300 A
sabafoo 555-2127 1200/300 C
a123456@lucky linux %
eg6. -w
a123456@lucky linux % grep -w core bbs_list
core 555-2912 1200/300 C
a123456@lucky linux % grep -w co bbs_list
a123456@lucky linux %
eg7. -x
a123456@lucky linux % grep -x ' core 555-2912 1200/300 C' bbs_list
core 555-2912 1200/300 C
a123456@lucky linux % grep -x co bbs_list
a123456@lucky linux %
eg8. -c
a123456@lucky linux % grep -c '1200' bbs_list
10
a123456@lucky linux %
eg9. -l
a123456@lucky linux % grep -l '1200' bbs_list
bbs_list
a123456@lucky linux %
eg10. -L
localhost:benchmarks sunquan$ grep -L '1200' bbs_list
localhost:benchmarks sunquan$ grep -L '1122' bbs_list
bbs_list
localhost:benchmarks sunquan$
eg11. -m num
localhost:benchmarks sunquan$ grep -m 2 '1200' bbs_list
aardvark 555-5553 1200/300 B
alpo-net 555-3412 2400/1200/300 A
localhost:benchmarks sunquan$
eg12. -A num
localhost:benchmarks sunquan$ grep -A 2 'core' bbs_list
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
localhost:benchmarks sunquan$
eg13. -B num
localhost:benchmarks sunquan$ grep -B 2 'core' bbs_list
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
localhost:benchmarks sunquan$
eg14. -C num
localhost:benchmarks sunquan$ grep -C 2 'core' bbs_list
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
localhost:benchmarks sunquan$
Color
eg15 高亮grep匹配
eg16. 更改匹配高亮颜色
30 black
31 red
32 green
33 yellow
34 blue
35 purple
36 cyan
37 white