grep 命令细讲

    本文参考自GUN grep 2.6.3 。

    现在linux下的grep命令都是集grep, egrep, fgrep于一身的。当然egrep和fgrep你也可以单独使用。egrep相当于 grep -E,fgrep相当于grep -F。,首先讲讲其选项吧。文中会多处谈到一个名词:模式。模式就是pattern,用来匹配一段内容的规则。

    grep的选项主要有如下七类,红色字体是我比较常用的选项:

1、Generic Program Information 通用程序信息:

    --help ,-V(-version)就不多说了。

2、Matcher Selection 匹配选项:

-E, --extended-regexp 

      解释扩展正则表达式(ERE)的模式
-F, --fixed-strings
      解释固定字符串列表的模式,由换行符分隔,任何一种的要匹配
-G, --basic-regexp
       解释基础正则表达式(BRE)的模式,这是默认的grep
-P, --perl-regexp
       解释perl正则表达式的模式。

 3、Matching Control 匹配控制:

-e PATTERN, --regexp=PATTERN。只有一个检索模式的时候可以省略,多个的时候,该选项就有用了。看例子

<span style="font-size:14px;"><span style="font-size:12px;">[jyapp@vm04-ap01 ~]$ cat a.txt
4124
aa
bb
[jyapp@vm04-ap01 ~]$ grep -e a a.txt
aa
[jyapp@vm04-ap01 ~]$ grep -e a -e b a.txt
aa
bb</span></span>

 -f FILE, --file=FILE。指定检索模式所在的文件,读取该文件的每一行的内容作为检索的模式。看例子

<span style="font-size:14px;"><span style="font-size:12px;">[jyapp@vm04-ap01 ~]$ cat reg.txt
a
b
[jyapp@vm04-ap01 ~]$ grep -f reg.txt a.txt 
aa
bb</span></span>

-i, --ignore-case。忽略大小写。

-v, --invert-match。显示没有匹配上的行。

-w, --word-regexp。整个word被匹配上的才显示。

-x, --line-regexp。整行都能匹配上的才显示。

-y,同-i

4、General Output Control 通用输出控制

-c, --count。输出匹配到模式的行数

--color[=WHEN], --colour[=WHEN]

    环绕的匹配的 (非空) 字符串,匹配线、 上下文线、 文件名、 行号、 字节偏移量和分离器 (为域和组的上下文行) 的转义序列可以使用颜色在终端上显示它们。 颜色定义使用环境变量 GREP_COLORS。 不推荐使用的环境变量 GREP_COLOR 仍受支持,但其设置并不有优先权。 WHEN可以是never, always, or auto。

color有三个值供选择:never、always、auto。该选项可以用于设定显示颜色。常用语做高亮显示。

always和auto的区别就是,always会在任何情况下都给匹配字段加上颜色标记,当通过管道或重定向时就会多出一些控制字符,结果会变成

export ^[[1;32m^[[KGREP^[[m^[[K_OPTIONS='-color=always'
export ^[[1;32m^[[KGREP^[[m^[[K_COLOR='1;32′
而auto则只在输出到终端时才加上颜色。

方法1:编辑  vim  ~/.bashrc
添加  alias grep = 'grep --color=auto'
source ~/.bashrc 

方法2:vim  ~/.bashrc 
export GREP_OPTIONS='--color=auto' GREP_COLOR='10;32'

GREP_COLOR参数现在仍支持,但是更推荐使用GREP_COLORS参数。

export GREP_OPTIONS='-color=auto'  #来实现高亮匹配,具体用什么颜色,可以通过
export GREP_COLOR='a;b'   #默认是1;31,即高亮的红色
来设置,其中:
a可以选择:【0,1,4,5,7,8】
0 关闭所有属性
1 设置高亮度
4 下划线
5 闪烁
7 反显
8 消隐

b可以选择:【30-37或40-47】
30 black
31 red
32 green
33 yellow
34 blue
35 purple
36 cyan
37 white
30 — 37 设置前景色
40 — 47 设置背景色


-L, --files-without-match。用模式来检索文件内容,如果检索不到,则显示该文件名

-l, --files-with-matches。与L相反。用模式来检索文件内容,如果检索到,则显示该文件名

-m NUM, --max-count=NUM。设置能显示最大的匹配行数。看例子

<span style="font-size:14px;"><span style="font-size:12px;">[jyapp@vm04-ap01 ~]$ cat a.txt
4124
aa
bb
aab
aac
aad
dd
ba
[jyapp@vm04-ap01 ~]$ grep -m 2 a a.txt
aa
aab</span></span>


-o, --only-matching。仅仅显示匹配到模式的内容。

-q, --quiet, --silent。安静模式,不输出标准输出信息。

-s, --no-messages。不输出标准错误信息。

5、Output Line Prefix Control 输出行的前缀控制

-b, --byte-offset。偏移的字节数,由于换行符也占用一个byte。看例子

<span style="font-size:14px;"><span style="font-size:12px;">[jyapp@vm04-ap01 ~]$ grep a a.txt -b
5:aa
11:aab
15:aac
19:aad
26:ba
[jyapp@vm04-ap01 ~]$ cat a.txt
4124
aa
bb
aab
aac
aad
dd
ba</span></span>


-H, --with-filename。在匹配到的内容前加上文件名。当搜索多个文件时,这是一个默认选项。

-h, --no-filename。与上相反。

-n, --line-number。输出匹配的内容所在文件行数。

6、Context Line Control 上下文行数控制

-A NUM, --after-context=NUM。输出匹配到的行数以及往后几行。

-B NUM, --before-context=NUM。输出匹配到的行数以及往前几行。
-C NUM, -NUM, --context=NUM。输出匹配到的行数以及前后几行。看例子吧

<span style="font-size:14px;"><span style="font-size:12px;">[jyapp@vm04-ap01 ~]$ cat a.txt
44
3ll
222
1111
kka
1111
2222
3333
4444
[jyapp@vm04-ap01 ~]$ grep -A 2 kk a.txt
kka
1111
2222
[jyapp@vm04-ap01 ~]$ grep -B 2 kk a.txt
222
1111
kka
[jyapp@vm04-ap01 ~]$ grep -C 2 kk a.txt
222
1111
kka
1111
2222</span></span>

7、File and Directory Selection 文件和目录选项

-R, -r, --recursive。递归查找目录。等同于 -d recurse。

其下选项用的不多,英文怕翻译的不恰当,就不翻译了,-D是针对设备文件的,-d是针对目录的,--exclude是排除选项。再额外还有几个选项,由于我没完全弄懂,这里就不说了。

       -D ACTION, --devices=ACTION
              If an input file is a device, FIFO or socket, use ACTION to process it.  By default, ACTION is read, which means that devices are read just as if they  were  ordinary  files.   If  ACTION  is  skip,
              devices are silently skipped.
       -d ACTION, --directories=ACTION
              If an input file is a directory, use ACTION to process it.  By default, ACTION is read, which means that directories are read just as if they were ordinary files.  If ACTION is skip, directories are
              silently skipped.  If ACTION is recurse, grep reads all files under each directory, recursively; this is equivalent to the -r option.
       --exclude=GLOB
              Skip files whose base name matches GLOB (using wildcard matching).  A file-name glob can use *, ?, and [...]  as wildcards, and \ to quote a wildcard or backslash character literally.
       --exclude-from=FILE
              Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under --exclude).
       --exclude-dir=DIR
              Exclude directories matching the pattern DIR from recursive searches.

嗯,那就先讲到这里。后续会继续讲到其正则表达式的使用,敬请期待


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值