shell三剑客之grep

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。

给定一个正则表达式和另一个字符串,我们可以达到如下的目的:

1. 给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”);

2. 可以通过正则表达式,从字符串中获取我们想要的特定部分。

正则表达式的特点是:

1. 灵活性、逻辑性和功能性非常的强;

2. 可以迅速地用极简单的方式达到字符串的复杂控制。

3. 对于刚接触的人来说,比较晦涩难懂。

由于正则表达式主要应用对象是文本,因此它在各种文本编辑器场合都有应用,小到著名编辑器EditPlus,大到Microsoft Word、Visual Studio等大型编辑器,都可以使用正则表达式来处理文本内容。

这里将会讲到shell里边的sed、awk、find、grep四剑客中的grep

–color=auto: 对匹配到的文本着色显示;  

-v: 显示不能够被pattern匹配到的行; 

-i: 忽略字符大小写 

-n:显示匹配的行号  

-c: 统计匹配的行数  

-o: 仅显示匹配到的字符串;  

-q: 静默模式,不输出任何信息  

-A #: after, 后#行  

-B #: before, 前#行  

-C #:context, 前后各#行  

-e:实现多个选项间的逻辑or关系 grep –e ‘cat ’  -e ‘dog’  file  

-w:整行匹配整个单词  

-E:使用ERE

正则表达式元字符

字符匹配: 

.   : 匹配任意单个字符; 

[]  : 匹配指定范围内的任意单个字符 

[^] :匹配指定范围外的任意单个字符

[:alpha:] 所有字母,包括大、小写 

[:alnum:] 所有字母和数字 

[:upper:] 所有大写字母 

[:lower:] 所有小写字母 

[:digit:] 所有数字 

[:punct:] 所有标点符号 

[:space:] 空格和Tab

正则表达式

*:匹配前面的字符任意次,包括0次 贪婪模式:尽可能长的匹配 

.*:任意长度的任意字符 

\?:匹配其前面的字符0或1次 

\+:匹配其前面的字符至少1次 

\{n\}:匹配前面的字符n次 

\{m,n\}:匹配前面的字符至少m次,至多n次 

\{,n\}:匹配前面的字符至多n次 

\{n,\}:匹配前面的字符至少n次

位置锚定:定位出现的位置 

^:行首锚定,用于模式的最左侧 

$:行尾锚定,用于模式的最右侧 

^PATTERN$: 用于模式匹配整行 

^$: 空行 ^[[:space:]]*$ :空白行 

\< 或 \b:词首锚定,用于单词模式的左侧 

\> 或 \b:词尾锚定;用于单词模式的右侧 

\<PATTERN\>:匹配整个单词 

首先创建一个文本,内容如下;

[root@centous1 home]# cat 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.
 However, this dress is about $ 3183 dollars.
 GNU is free air not free beer.
 Her hair is very beauty.
 I can't finish the test.
 Oh! The soup taste good.
 motorcycle is cheap than car.
 This window is clear.


 the symbol '*' is represented as start.
 Oh!My god!
 The gd software is a library for drafting programs.
 You are the best is mean you are the no. 1.
 The world <Happy> is the same with "glad".
 I like dog.
 google is the best tools for search keyword.
 goooooogle yes!
 go! go! Let's go.
 # I am VBird

1.搜寻特定字符串"the"

 注: n为显示行号

grep -n 'the' regular_express.txt

 

2.反向搜寻特定字符串"the"

 grep -vn 'the' regular_express.txt

 

3.取得任意大小写"the"的这个字符串

grep -in 'the' regular_express.txt

 

4.利用括号 [] 来搜寻集合字符

搜索test或taste这两个单词时,发现他们有共同的't?st',所以可以这么搜寻

grep -n 't[ae]st' regular_express.txt

这样其实就是在找t[a]st和t[e]st这两个分开的字符

如果搜索有 oo 的字符时,则可以使用:

grep -n 'oo' regular_express.txt

如果搜索oo时不想搜到 oo 前面有 g 的话,我们可以利用反向选择[^]来达成:

grep -n '[^g]oo'  regular_express.txt

如果搜索oo前面不想有小写字符,则:

grep -n '[^a-z]oo' regular_express.txt

 注: 大写英文/小写英文/数字 可以使用 [a-z]/[A-Z]/[0-9]等方式来书写,也可以写在一起

 [a-zA-Z0-9]表示要求字符串是数字以及英文

 如果我们要取得有数字的那行,则:

 grep -n '[0-9]' regular_express.txt

注:但考虑到语系对编码顺序的影响,因此除了连续编码使用减号[-]外,也可以用[:lower:]代替a-z 以及 [:digit:] 代替0-9 使用

grep -n '[^[:lower:]]oo' regular_express.txt

grep -n '[[:digit:]]' regular_express.txt

 

5.显示行首为'the'的字符串

grep -n '^the' regular_express.txt

显示行首是小写字符

grep -n '^[a-z]' regular_express.txt

 

6.显示行尾为点 . 的那一行

grep -n '\.$' regular_express.txt

 

7.显示5-9行数据

cat -An regular_express.txt |head -n 10 |tail -n 6

 

8.显示空白行

grep -n '^$' regular_express.txt

 

9.找出g??d字符串,起头g结束d的四个字符串

 grep -n 'g..d' regular_express.txt

 

10. o*代表空字符(就是有没有字符都可以)或者一个到N个o字符,所以grep -n 'o*' regular_express.txt就会把所有行全部打印出来,

 11.oo*代表o+空字符或者一个到N个o字符,所以grep -n 'oo*' regular_express.txt就会把o,oo,ooo等的行全部打印出来

 12."goo*g"代表gog,goog,gooog…等

 grep -n 'goo*g' regular_express.txt

 

 

13.找出含g…g字符串的行

注: .代表任意字符, .*则就代表空字符或者一个到N个任意字符

grep -n 'g.*g' regular_express.txt

 

14.找出含有数字的行

grep -n '[0-9][0-9]*' regular_express.txt

或grep -n '[0-9]' regular_express.txt

 

15.找出含两个o的字符串

 注:{}因为在shell里有特殊意义,所以需要加跳脱符\来让其失去意义

grep -n 'o\{2\}'  regular_express.txt

找出g后含2到5个o然后以g结尾的字符串

grep -n 'go\{2,5\}g'  regular_express.txt

找出g后含2以上的o然后以g结尾的字符串

grep -n 'go\{2,\}g'  regular_express.txt

原创文章,作者:forest,如若转载,请注明出处:http://www.178linux.com/30509

(0)
forestforest
上一篇 2016-08-08 16:07
下一篇 2016-08-08 16:07

相关推荐

  • rpm软件包管理

    软件包管理 软件运行环境 API   应用程序开发接口 ABI   二进制接口 Rpm包命名方式 Name-version-release-arch-rpm 例如 zziplib-devel-0.13.162-5.el7.i686.rpm zziplib :主包名 dewel:次包名 0.13.162: 主版…

    Linux干货 2016-08-24
  • Linux系统grep正则表达式

    一、grep命令的使用 首先,我们来了解grep命令,grep (global search regular expression and printing),全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Linux中的grep命令包括、grep, egrep和fgrep 。egrep是grep…

    Linux干货 2017-04-23
  • 20160803作业-用户组和权限管理

    http://note.youdao.com/yws/public/redirect/share?id=46f06331b737c6d08a0e8c9c3d49ac9e&type=false

    Linux干货 2016-08-08
  • 26期全程班-第七周博客作业

    1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;    (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; # fdisk /dev/sda # mkf…

    Linux干货 2017-03-17
  • history命令详解

       有效地使用命令历史机制将会使效率获得极大提升。history:   保存你输入的命令历史。 可以用它来重复执行命令。   history [-c] [-d offset] [n]   history -anrw [filename]   history …

    2017-03-26
  • linux运维

    linux运维大纲,学习路线图

    Linux干货 2017-10-21