每天学一点儿shell:Linux三剑客——grep命令

前言

Linux的“三剑客”指的是:grepsedawk
之所以被称为三剑客是通过上述工具可以更好的处理linux的查询结果。

正则表达式

上述命令之所以被称为三剑客是因为他们能很好的结合正则表达式来处理查询内容,并且只有上述“三剑客”能结合正则表达式使用。下面介绍以下正则表达式组成字符的含义:

元字符功能说明
^匹配首行表示以某个字符开头
$匹配行尾表示以某个字符结尾
^$空行的意思表示空行的意思
.匹配任意单个字符表示匹配任意单个字符
*匹配0个或多个此字符表示重复的任意多个字符
\转义字符表示转义字符
[]匹配中括号内的字符表示过滤括号内的字符
.*代表任意多个字符表示匹配任意多个字符

特殊的几个如下:

正则表达式解释
.任意一个字符。
[abc]表示匹配一个字符,这个字符必须是abc中的一个。
[a-zA-Z]表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。
[^123]匹配一个字符,这个字符是除了1、2、3以外的所有字符。

对于一些常用的字符集,系统做了定义:

字符集等价系统定义
[A-Za-z]等价于 [[:alpha:]]
[0-9]等价于 [[:digit:]]
[A-Za-z0-9]等价于 [[:alnum:]
tab,space等空白字符 [[:space:]]
[A-Z]等价于 [[:upper:]]
[a-z]等价于 [[:lower:]]
标点符号[[:punct:]]

匹配次数:

\{m\}匹配其前面出现的字符m次
\{m,\}匹配其前面出现的字符至少m次
\{m,n\}匹配其前面出现的字符至少m次,至多n次
\?匹配其前面出现的内容0次或1次,等价于{0,1}
*匹配其前面出现的内容任意次,等价于{0,},所以 “.*” 表述任意字符任意次,即无论什么内容全部匹配。

三剑客的功能非常强大,但我们只需要掌握他们分别擅长的领域即可:grep擅长查找功能sed擅长取行和替换awk擅长取列

grep命令用法

用法:

grep [选项]... PATTERN [FILE]...

列出一些常见的选项命令

选选项解释
–color对匹配到的文本着色显示
-v反过来(invert),显示不配patern匹配到的行
-i忽略大小写(ignore case)
-n显示匹配的行号
-c统计匹配的行数
-o只显示被模式匹配到的字符串
-q静默模式,不输出任何信息
-A–after-context=NUM 打印以文本结尾的NUM 行
-B–before-context=NUM 打印以文本起始的NUM 行
-C–context=NUM 打印输出文本NUM 行
-e实现多个选项之间的逻辑or关系,例如:grep -e ‘cat’ -e ‘dog’ file
-w被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker
-E开启扩展(Extend)的正则表达式,使用正则相当于egrep
-F不支持正则,相当与fgrep

grep命令实例

用法一:查找root字符串的行数以及内容

[root@hadoop-master test-grep]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@hadoop-master test-grep]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@hadoop-master test-grep]# grep -vc "root" /etc/passwd
43
[root@hadoop-master test-grep]# grep -o "root" /etc/passwd
root
root
root
root

用法二:查找"core id"字符串以及前后2行内容

[root@hadoop-master test-grep]# grep -A 2 "core id" /proc/cpuinfo
core id		: 0
cpu cores	: 1
apicid		: 0
[root@hadoop-master test-grep]# grep -B 2 "core id" /proc/cpuinfo
physical id	: 0
siblings	: 1
core id		: 0
[root@hadoop-master test-grep]# grep -C 2 "core id" /proc/cpuinfo
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0

用法三:匹配包含"sh"字符串的内容

[root@hadoop-master test-grep]# grep "/.*sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
setroubleshoot:x:990:986::/var/lib/setroubleshoot:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法四:匹配以"sh"结尾前面有0到2个字符的字符串

[root@hadoop-master test-grep]# grep "/.\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法五:匹配以"sh"结尾前面有0到2个字符的英文单词

[root@hadoop-master test-grep]# grep -w ".\{0,2\}sh" /etc/passwd
root:x:0:0:root:/root:/bin/bash
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法六:查询以"h"结尾的字符串

[root@hadoop-master test-grep]# grep "h$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
leo:x:1000:1000:leo:/home/leo:/bin/bash
mysql:x:987:1001::/home/mysql:/bin/bash

用法七:查询当前目录下的所有文件中包含关键”hello2“ 的文件,以及行数

[root@hadoop-master shell-test]# grep -nr "hello2"  ./
./test-grep/file.txt:leo2 hello2
./test-grep/file2.text:leo2 hello2 txt2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leo825...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值