[转载]强大的grep用法详解:grep与正则表达式

首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!
正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串。vim、grep、awk 、sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大;在以前上班的公司里,由于公司是基于web的服务型网站(nginx),对正则的需求比 较大,所以也花了点时间研究正则,特与大家分享下:

1基础正则表达式

grep 工具,以前介绍过。
grep -[acinv] ‘搜索内容串‘ filename
-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号
-v 反向选择,即找 没有搜索字符串的行
其中搜索串可以是正则表达式!

1
搜索有the的行,并输出行号
$grep -n ‘the‘ regular_express.txt
搜 索没有the的行,并输出行号
$grep -nv ‘the‘ regular_express.txt

利 用[]搜索集合字符
[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e
[email protected]:~/tmp$ grep -n ‘t[ae]st‘ regular_express.txt 
8:I can‘t finish the test.
9:Oh! the soup taste good!

可以用^符号做[]内的前缀,表示除[]内的字符之外的字 符。
比如搜索oo前没有g的字符串所在的行. 使用 ‘[^g]oo‘ 作搜索字符串
[email protected]:~/tmp$ grep -n ‘[^g]oo‘ regular_express.txt 
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!

[] 内可以用范围表示,比如[a-z] 表示小写字母,[0-9] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-9]表示所有数字与英文字符。 当然也可以配合^来排除字符。
搜索包含数字的行
[email protected]:~/tmp$ grep -n ‘[0-9]‘ regular_express.txt 
5:However ,this dress is about $ 3183 dollars.
15:You are the best is menu you are the no.1.

行首与行尾字符 ^ $. ^ 表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$’ 就表示空行,因为只有
行首和行尾。

这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。
比如搜索the在开头的行
[email protected]:~/tmp$ grep -n ‘^the‘ regular_express.txt 
12:the symbol ‘*‘ is represented as star.

搜索以小写字母开头的行
[email protected]:~/tmp$ grep -n ‘^[a-z]‘ regular_express.txt 
2:apple is my favorite food.
4:this dress doesn‘t fit me.
10:motorcycle is cheap than car.
12:the symbol ‘*‘ is represented as star.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let‘s go.
[email protected]:~/tmp$

搜索开头不是英文字母的行
[email protected]:~/tmp$ grep -n ‘^[^a-zA-Z]‘ regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
21:#I am VBird
[email protected]:~/tmp$

$表示它前面的串是在行的结尾,比如 ‘/.‘ 表示 . 在一行的结尾
搜索末尾是.的行
[email protected]:~/tmp$ grep -n ‘/.$‘ regular_express.txt //. 是正则表达式的特殊符号,所以要用/转义
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn‘t fit me.
5:However ,this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
.....

注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows
下面的文本时要特别注意!
可以用cat dos_file | tr -d ‘/r‘ > unix_file 来删除^M符号。 ^M==/r

那么‘^$‘ 就表示只有行首行尾的空行拉!
搜索空行
[email protected]:~/tmp$ grep -n ‘^$‘ regular_express.txt 
22:
23:
[email protected]:~/tmp$

搜索非空行
[email protected]:~/tmp$ grep -vn ‘^$‘ regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn‘t fit me.
..........

任意一个字符. 与重复字符 *

在bash中*代表通配符,用来代表任意个 字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。
例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.

点. 代表一个任意字符,必须存在。 g??d 可以用 ‘g..d‘ 表示。 good ,gxxd ,gabd .....都符合。

[email protected]:~/tmp$ grep -n ‘g..d‘ regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! the soup taste good!
16:The world is the same with ‘glad‘.
[email protected]:~/tmp$

搜索两个o以上的字符串
[email protected]:~/tmp$ grep -n ‘ooo*‘ regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! the soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等
[email protected]:~/tmp$ grep -n ‘goo*g‘ regular_express.txt 
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g开头和结尾的字符串在的行
[email protected]:~/tmp$ grep -n ‘g.*g‘ regular_express.txt     // .*表示 0个或多个任意字符
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let‘s go.


限定连续重复字符的范围 { } 

. * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 2,5 表示2~5个,
2表示2个,2, 表示2到更多个
注意,由于{ }在SHELL中有特殊意义,因此作为正则表达式用的时候要用/转义一下。

搜索包含两个o的字符串的行。
[email protected]:~/tmp$ grep -n ‘o/{2/}‘ regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! the soup taste good!
18:google is the best tools for search keyword.
19:goooooogle yes!

搜索g后面跟2~5个o,后面再跟一个g的字符串的行。
[email protected]:~/tmp$ grep -n ‘go/{2,5/}g‘ regular_express.txt 
18:google is the best tools for search keyword.

搜索包含g后面跟2个以上o,后面再跟g的行。。
[email protected]:~/tmp$ grep -n ‘go/{2,/}g‘ regular_express.txt 
18:google is the best tools for search keyword.
19:goooooogle yes!

注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。
‘[^a-z/.!^ -]‘ 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。

另外shell 里面的反向选择为[!range], 正则里面是 [^range]

2扩展正则表达式

扩展正则表达式是对基础正则表达式添加了几个特殊构成的。
它令某些操作更加方便。
比如我们要去除 空白行和行首为 #的行, 会这样用:
[email protected]:~/tmp$ grep -v ‘^$‘ regular_express.txt | grep -v ‘^#‘
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn‘t fit me.
............

然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。
注意grep只支持基础表达式, 而egrep 支持扩展的, 其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。
那么:
[email protected]:~/tmp$ egrep -v ‘^$|^#‘ regular_express.txt 
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn‘t fit me.
....................
这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。

这里列出几个扩展特殊符号:
+, 于 . * 作用类似,表示 一个或多个重复字符。
?, 于 . * 作用类似,表示0个或一个字符。
|,表示或关系,比如 ‘gd|good|dog‘ 表示有gd,good或dog的串
(),将部分内容合成一个单元组。 比如 要搜索 glad 或 good 可以这样 ‘g(la|oo)d‘
()的好处是可以对小组使用 + ? * 等。
比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 : ‘A(xyz)+C‘

时间: 2024-08-06 12:43:02

[转载]强大的grep用法详解:grep与正则表达式的相关文章

grep用法详解 grep与正则表达式

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. 主要参数    [options]主要参数:    -c:只输出匹配行的计数.    -I:不区分大 小写(只适用于单字符).    -h:查询多文件时不显示文件名.    -l:查询多文件时只输出包含匹配字符的文件名.    -n:显示匹配行及 行号.    -s

grep用法详解:grep与正则表达式 [转]

正则表达式与通配符不一样,它们表示的含义并不相同. grep命令的选项用于对搜索过程进行补充说明.grep命令的模式十分灵活,可以是字符串.变量,还可以是正则表达式. 无论模式是何种形式,只要模式中包含了空格,就需要使用双引号或单引号将模式引起来. '搜寻字符串’是正则表达式,注意为了避免shell的元字符对正则表达式的影响,请用单引号(’’)括起来,千万不要用双引号括起来("”)或者不括起来.正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.g

sed及grep用法详解

在linux系统有被称为文本处理三剑客grep,sed,awk. 今天与大家分享的就是gerp和sed处理工具,至于awk,我将会在晚些的时间与大家分享,在分享grep和sed之前我先要和大家说说正则表达,正则表达是什么呢,正则表达式就像是三剑客手里的剑,没有剑哪还能称的上什么剑客,所以我们要先了解以下正则表达式的简单用法. 什么是正则表达式: "正则表达式是描述一组字符窜特征的模式,用来匹配特定的字符串."--Ken Thompson 基本正则表达式的元字符有以下这些: . :匹配任

grep正则表达式与grep用法详解

功能:输入文件的每一行中查找字符串. 基本用法: grep [-acinv] [--color=auto] [-A n] [-B n] '搜寻字符串' 文件名 参数说明: -a:将二进制文档以文本方式处理-c:显示匹配次数-i:忽略大小写差异-n:在行首显示行号-A:After的意思,显示匹配字符串后n行的数据-B:before的意思,显示匹配字符串前n行的数据-v:显示没有匹配行-A:After的意思,显示匹配部分之后n行-B:before的意思,显示匹配部分之前n行 --color:以特定颜

linux系统 I/O重定向、管道及grep用法详解

输入/输出设备(INPUT OUTPUT) 系统设定 默认输出设备:标准输出 STDOUT 1 默认输入设备:标准输入 STDIN 0 标准错误输出:STDERR 2 默认标准输入:键盘 默认标准输出和错误输出:显示器 linux I/O重定向 >:覆盖输出 >>:追加输出 set -C:禁止对已经存在的文件使用覆盖重定向 强制覆盖输出:则使用>| set +C:关闭上述功能 2>:重定向错误输出 2>>:追加方式错误输出 &>:重定向标准输出和错误

linux中grep用法详解

查找特定字符串并颜色显示 [[email protected] test]# grep -n 'the' regular_express.txt --color=auto 8:I can't finish the test. 12:the symbol '*' is represented as start. 15:You are the best is mean you are the no. 1. 16:The world <Happy> is the same with "gl

grep用法详解

格式: ** grep [-acinv] [--color=auto] '搜寻字串' filename** 选项与参数: -a :将 binary 文件以 text 文件的方式搜寻数据 -c :计算找到 '搜寻字串' 的次数 -i :忽略大小写的不同,所以大小写视为相同 -n :顺便输出行号 -v :反向选择,亦即显示出没有 '搜寻字串' 内容的那一行! --color=auto :可以将找到的关键字部分加上颜色的显示喔! 通过grep能匹配出我们需要的一些信息. [[email protect

【转载】wget 命令用法详解

wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能和特点:(1)支持断点下传功能:这一点,也是网络蚂蚁和FlashGet当年最大的卖点,现在,Wget也可以使用此功能,那些网络不是太好的用户可以放心了:(2)同时支持FTP和HTTP下载方式:尽管现在大部分软件可以使用HTTP方式下载,但是,有些时候,仍然需要使用FTP方式下载软件:(3)支持代理服务器:对安全强度很高的系统而言,一般不会将自己的系统直接暴

awk.sed.grep三剑客详解

事前准备1.主机node1:172.16.133.112.作为实验的文件/etc/passwd /etc/fstab qinqin cp /etc/passwd . cp /etc/fstab . 一.grep用法详解1.grep是干什么的grep的全名是Galobal research Regular Expression and Pringtiong,即搜索正则表达式,也就是说grep简单来讲就是用来搜索匹配字符的2.grep分类grep有基本正则表达式和扩展正则表达式之分,不过她们的作用域