您的位置:首页 > 运维架构 > Linux

Linux 三大文本处理命令之一GREP

2017-04-10 16:57 441 查看
grep,全称global search regular expression(RE) and print out the line。是一种强大的文本搜索工具,根据正则表达式,把匹配的行输出。Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。linux使用GNU版本的grep。它功能更强,可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。

这里对GREP的常用用法做一个介绍。

一 定义


[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename
-aiinv通常有:
-a:将二进制文件以text文件的方式搜索
-c:计算匹配的次数
-i:忽略大小写的不同
-n:显示所在行号
-v:显示没有所要搜索字符串的行
-r:迭代查询,即在该目录及子目录下搜索
--color=auto:将找到的关键词显色
例如:

将/etc/passwd里有出现root的行取出:

yan@ysw:~/桌面$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
将/etc/passwd,将没有出现 root 的行取出来,并显示行号:



还有就是与dmesg配合使用,这里先简单介绍一下dmesg:

dmesg定义:

dmesg [ops]
主要有7种应用:

1 列出加载到内存的所有驱动

使用如‘more’。 ‘tail’, ‘less ’或者‘grep’文字处理工具来处理‘dmesg’命令的输出。由于dmesg日志的输出不适合在一页中完全显示,因此我们使用管道(pipe)将其输出送到more或者less命令单页显示:

用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行:

yan@ysw:~/桌面$ dmesg | grep -n 'eth'
858:[    2.697003] e1000e 0000:00:19.0 eth0: registered PHC clock
859:[    2.697006] e1000e 0000:00:19.0 eth0: (PCI Express:2.5GT/s:Width x1) f8:b1:56:ab:47:58
860:[    2.697007] e1000e 0000:00:19.0 eth0: Intel(R) PRO/1000 Network Connection
861:[    2.697041] e1000e 0000:00:19.0 eth0: MAC: 10, PHY: 11, PBA No: 1011FF-0FF
862:[    2.697580] e1000e 0000:00:19.0 eno1: renamed from eth0


2 列出所有被检测到的硬件:

yan@ysw:~/桌面$ dmesg | grep -n 'eth'
858:[    2.697003] e1000e 0000:00:19.0 eth0: registered PHC clock
859:[    2.697006] e1000e 0000:00:19.0 eth0: (PCI Express:2.5GT/s:Width x1) f8:b1:56:ab:47:58
860:[    2.697007] e1000e 0000:00:19.0 eth0: Intel(R) PRO/1000 Network Connection
861:[    2.697041] e1000e 0000:00:19.0 eth0: MAC: 10, PHY: 11, PBA No: 1011FF-0FF
862:[    2.697580] e1000e 0000:00:19.0 eno1: renamed from eth0yan@ysw:~/桌面$ dmesg | grep sda
[ 2.311489] sd 0:0:0:0: [sda] 976773168 512-byte logical blocks: (500 GB/466 GiB)
[ 2.311491] sd 0:0:0:0: [sda] 4096-byte physical blocks
[ 2.311743] sd 0:0:0:0: [sda] Write Protect is off
[ 2.311746] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
[ 2.311917] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[ 2.387520] sda: sda1 sda2 < sda5 sda6 sda7 sda8 sda9 sda10 >
[ 2.388439] sd 0:0:0:0: [sda] Attached SCSI disk
[ 3.709304] EXT4-fs (sda8): mounted filesystem with ordered data mode. Opts: (null)
[ 8.366431] EXT4-fs (sda8): re-mounted. Opts: errors=remount-ro
[ 11.281038] Adding 780284k swap on /dev/sda9. Priority:-1 extents:1 across:780284k FS
[ 12.333149] EXT4-fs (sda10): mounted filesystem with ordered data mode. Opts: (null)
[ 12.604467] EXT4-fs (sda7): mounted filesystem with ordered data mode. Opts: (null)
注解 ‘sda’表示第一块 SATA硬盘,‘sdb’表示第二块SATA硬盘。若想查看IDE硬盘搜索‘hda’或‘hdb’关键词。

3 只输出dmesg命令输出的前20行:

yan@ysw:~/桌面$ dmesg | head -20
4 只输出dmesg命令输出的最后20行:
yan@ysw:~/桌面$ dmesg | tail -20
5 搜索特定字符串的设备:

包含‘usb’ ‘dma’ ‘tty’ ‘memory’等字符串的日志行:

yan@ysw:~/桌面$ dmesg | grep -i memory
6 清空dmesg缓存:

yan@ysw:~/桌面$ dmesg -C
7 实时监控dmesg

yan@ysw:~/桌面$ watch "dmesg | tail -20"
二 与正则表达式结合的GREP

通过例子说明:

先在桌面建立一个a.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.

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.

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.
1 搜索出现“t?xt”字符串的行,?表示任意一个或多个字符:



2 字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g:



3 字符类的连续:再来,假设我 oo 前面不想要有小写字节:

yan@ysw:~/桌面$ grep -n '[^a-z]oo' a.txt
3:Football game is not use feet only.
当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字串是数字与英文将他全部写在一起,变成:[a-zA-Z0-9]。

例如要包含数字的行:

yan@ysw:~/桌面$ grep -n '[0-9]' a.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.


4 行首字符:如果我想要让 the 只在行首列出:



再例如只要开头是小写字母的行:

yan@ysw:~/桌面$ grep -n '^[a-z]' a.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 start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
如果不要开头是字母的行:

yan@ysw:~/桌面$ grep -n '^[^a-zA-Z]' a.txt
1:"Open Source" is a good mechanism to develop programs.


注意^在[]内部时表示取反,在[]外面表示行首。

5 行尾结束:行尾部为小数点 (.) 的那一行:



特别注意到,因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符(\)来加以解除其特殊意义

找出空白行:

yan@ysw:~/桌面$ grep -n '^$' a.txt
6:
7:
13:
21:
先介绍一下.和*

.:代表一个任意字节
*:代表重复前一个字符,0个或者多个,需要与一个字符组合才有意义。例如要出现2个及以上的o,则ooo*,o*代表出现0个或者多个o
6 需要找出 g??d 的字串:
yan@ysw:~/桌面$ grep -n 'g..d' a.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
列出最少两个o的行:

yan@ysw:~/桌面$ grep -n 'ooo*' a.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,但是两个 g 之间仅能存在至少一个 o:

yan@ysw:~/桌面$ grep -n 'goo*g' a.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
想要找出 g 开头与 g 结尾的行,当中的字符可有可无:



可以发现此时相当于返回一个范围的子字符串了。

最后介绍一下限制连续RE字符串的范围方法即{}

我们可以利用 . 与 RE 字符及 * 来配置 0 个到无限多个重复字节, 那如果我想要限制一个范围区间内的重复字节数就需要{},由于{ 与 } 的符号在 shell 是有特殊意义的,因此, 我们必须要使用字符   \ 来让他失去特殊意义才行。:

7 要找到两个 o 的字串:

yan@ysw:~/桌面$ grep -n 'ooo*\{2\}' a.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 的字串:

yan@ysw:~/桌面$ grep -n 'go*\{2,5\}g' a.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  GREP