深入理解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
--helpprint some usage messagegrep --help
-V / --versionprint version numbergrep -V
-e--regexp=patternsgrep -e aa bbs_list
-i--ignore-casegrep -i Aa bbs_list
-vprint non-matching linesgrep -v aa bbs_list
-wmatching wordgrep -w core bbs_list
-xmatching all linegrep -x ' core 555-2912 1200/300 C' bbs_list
-ccount matchgrep -c '1200' bbs_list
-l列出文件内容符合指定的样式的文件名称grep -l '1200' bbs_list
-L列出文件内容不符合指定的样式的文件名称grep -l '1200' bbs_list
-m num--max-count=numgrep -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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值