码迷,mamicode.com
首页 > 其他好文 > 详细

正则介绍grep

时间:2017-11-26 23:00:24      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:linux

正则表达式,又称规则表达式,英文名为Regular Expression,在代码中常简写为regex、regexp或RE,是计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。


正则表达式是对字符串(包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”))操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。正则表达式是一种文本模式,模式描述在搜索文本时要匹配的一个或多个字符串。



grep工具

该命令的格式为:grep [-cinvABC]‘word’ filename,其常用的选项如下所示。

-c 表示打印符合要求的行数。

-i 表示忽略大小写。

-n 表示输出符合要求的行及其行号。

-v 表示打印不符合要求的行。

-A 后面跟一个数字(有无空格都可以),例如-A2表示打印符合要求的行以及下面两行。

-B后面跟一个数字,例如-B2表示打印符合要求的行以及上面两行。

-C 后面跟一个数字,例如-C2表示打印符合要求的行以及上下各两行。



过滤出带有某个关键词的行,并输出行号

示例命令如下:

#grep –n ‘root’ /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

10:operator:x:11:0:operator:/root:/sbin/nologin



过滤出不带有某个关键词的行,并输出行号

示例命令如下:

#grep –nv ‘root’ /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

6:sync:x:5:0:sync:/sbin:/bin/sync



过滤出所有包含数字的行

示例命令如下:

#grep ‘[0-9]’ /etc/inittab

#multi-user.target:analogous to runlevel 3

#graphical.target:analogous to runlevel 5



过滤出所有不包含数字的行

示例命令如下:

#grep –v ‘[0-9]’ /etc/inittab



过滤掉所有以#开头的行

示例命令如下:

#cat /etc/sos.conf

[plugins]


[tunables]


#rpm.rpmva = off

#general.syslogsize = 15


#grep –v ‘^#’ /etc/sos.conf

[plugins]


[tunables]



过滤掉所有空行和以#开头的行

示例命令如下:

#grep –v ‘^#’ /etc/sos.conf |grep –v ‘^#’

[plugins]

[tunables]



过滤出任意一个字符和重复字符

示例命令如下:

#grep ‘r.o’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

.表示任意一个字符。上例中,r.o表示把r与o之间有一个任意字符的行过滤出来。


#grep ‘ooo*’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

*表示零个或多个*前面的字符。上例中,ooo*表示oo、ooo、oooo……或更多的o。



指定要过滤出的字符出现次数

示例命令如下:

#grep ‘o\{2\}’ /etc/passwd

符号{ },其内部为数字,表示前面的字符要重复的次数。需要强调的是,{}作用都需要加上转义字符\。另外使用“{ }”还可以表示一个范围,具体格式为{n1,n2},其中n1<n2,表示重复n1到n2次前面的字符,n2还可以为空,这时表示大于等于n1次。



egrep工具

egrep 是grep的扩展版本,可以完成grep不能完成的工作。下面介绍egrep不同于grep的几个用法。为了试验方便,先编辑一个test.txt,其内容如下:

rot:x:0:0:rot: /bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

1111111111111111111111111111

aaaaaaaaaaaaaaaaaaaaaaaaa



过滤出一个或多个指定的字符

示例命令如下:

#egrep ‘o+’ test.txt

rot:x:0:0:rot: /bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

#egrep ‘oo+’ test.txt

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

#egrep ‘ooo+’ test.txt

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

和grep不同,这里egrep使用的是符号+,它表示匹配1个或多个+前面的字符,这个“+”是不支持被grep直接使用的。包括上面的{},而egrep可以,而不用加\转义。示例如下:

#egrep ‘o{2}’ /etc/passwd

root:x:0:0:root:/root:/bin/bash



过滤出零个或一个指定的字符

示例命令如下:

#egrep ‘o?’ test.txt

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

1111111111111111111111111111

aaaaaaaaaaaaaaaaaaaaaaaaa

#egrep ‘ooo?’ test.txt

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash


#egrep ‘oooo?’ test.txt

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash



过滤出字符串1或字串符2

示例命令如下:

#egrep ‘aaa|111|ooo’ test.txt

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

1111111111111111111111111111

aaaaaaaaaaaaaaaaaaaaaaaaa



egrep中()的应用

示例命令如下:

#egrep ‘r(oo|at)o’ test.txt

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash

这里用()表示一个整体,上例中会把包含rooot或者rato的行过滤出来,另外也可以把()和其他符号组合在一起,例如(oo)+就表示1个或者多个oo。如下所示:

#egrep ‘r(oo)+’ test.txt

operator:x:11:0:operator:/root:/sbin/nologin

operator:x:11:0:operator:/rooot:/sbin/nologin

roooot:x:0:0:rooooot: /bin/bash


正则介绍grep

标签:linux

原文地址:http://blog.51cto.com/ccj168/2044513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!