Linux命令详解之grep命令

作为Linux中最为常用的三大文本(awk,sed,grep)处理工具之一,掌握好其用法是很好必要的。
grep家族总共有三个:grep egrep fgrep

常用格式

grep [选项] “模式” [文件]

常用选项

-E:开启扩展(Extend)的正则表达式。
-i:忽略大小写(ignore case)。
-v:反过来(invent),只打印没有匹配的,而匹配的反而不打印。
-n:显示行号。
-w:被匹配的文本只能是单词,而不能是单词中的某一部分,如文本中有liker,而我搜寻的只是like,就可以使用-w选项来避免匹配liker。
-c:显示总共有多少行被匹配到了,而不是显示被匹配到的内容,注意如果同时使用-cv选项显示有多少行没有被匹配到。
-o:只显示被模式匹配到的字符串。
–color:将匹配到的内容以颜色高亮显示。
-A n:显示匹配到的字符串所咋的行及其后n行,after。
-B n:显示匹配到的字符串所在的行及其前n行,before。
-C n:显示匹配到的字符串所在的行及其前后各n行,context。

示例
[root@localhost etc]# grep "root" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost etc]# grep -i "Root" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost etc]# grep -n "root" /etc/passwd
1:root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost etc]# grep -vc "root" /etc/passwd
27

[root@localhost etc]# grep -o "root" /etc/passwd
root
root
root
root

[root@localhost ~]# grep -A 2 "vcvxz" /root/bawei.txt
vcvxz
fdsafas
vzxvegf

[root@localhost ~]# grep -B 2 "vcvxz" /root/bawei.txt
dasdafa
fasfaf
vcvxz

[root@localhost ~]# grep -C 2 "vcvxz" /root/bawei.txt
dasdafa
fasfaf
vcvxz
fdsafas
vzxvegf

模式部分

1.直接输入要匹配的字符串,可以用fgrep代替来提高查找速度。
2.使用基本正则表达式,下面是基本正则表达式的用法。

匹配字符

.:任意一个字符。
[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:]]

[root@localhost ~]# cat exam.c
#include <stdio.h>
int main()
{
	String a1="hello";
	String a2="world";
	String a3="helloworld";
	printf("%d\n",a3);
	return 0;
}
[root@localhost ~]# grep "hello" exam.c
	String a1="hello";
	String a3="helloworld";
[root@localhost ~]# grep "hello[[:alpha:]]" exam.c
	String a3="helloworld";
[root@localhost ~]# grep "a[[:digit:]][^[:alpha:]]" exam.c
	String a1="hello";
	String a2="world";
	String a3="helloworld";
	printf("%d\n",a3);
匹配次数

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

[root@localhost etc]# grep "/.*sh" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
zs100:x:1000:1000::/homes1/zs100:/bin/bash
gentoo:x:4004:4004::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
lisi:x:4006:4006::/homes1/lisi:/bin/bash
hu:x:4007:4007::/homes1/hu:/bin/bash
zheng:x:4008:4008::/homes1/zheng:/bin/bash
zheg:x:4009:4009::/homes1/zheg:/bin/bash

[root@localhost etc]# grep "/.\{0,2\}sh" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
zs100:x:1000:1000::/homes1/zs100:/bin/bash
gentoo:x:4004:4004::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
lisi:x:4006:4006::/homes1/lisi:/bin/bash
hu:x:4007:4007::/homes1/hu:/bin/bash
zheng:x:4008:4008::/homes1/zheng:/bin/bash
zheg:x:4009:4009::/homes1/zheg:/bin/bash
位置锚定

^:锚定行首
$ :锚定行尾。技巧:"^$“用于匹配空白行。
\b或<:锚定单词的行首。如”\blike"不会匹配alike,但是会匹配liker。
\b或>:锚定单词的行尾。如"\blike\b"不会撇皮alike和liker,只会匹配like。
\B:与\b作用相反。

[root@localhost etc]# grep "h" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
zs100:x:1000:1000::/homes1/zs100:/bin/bash
mariadb:x:4003:5002::/homes1/mariadb:/sbin/nologin
gentoo:x:4004:4004::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
lisi:x:4006:4006::/homes1/lisi:/bin/bash
hu:x:4007:4007::/homes1/hu:/bin/bash
zheng:x:4008:4008::/homes1/zheng:/bin/bash
zheg:x:4009:4009::/homes1/zheg:/bin/bash
[root@localhost etc]# grep "h$" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
zs100:x:1000:1000::/homes1/zs100:/bin/bash
gentoo:x:4004:4004::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
lisi:x:4006:4006::/homes1/lisi:/bin/bash
hu:x:4007:4007::/homes1/hu:/bin/bash
zheng:x:4008:4008::/homes1/zheng:/bin/bash
zheg:x:4009:4009::/homes1/zheg:/bin/bash
[root@localhost etc]# grep "^h" /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
hu:x:4007:4007::/homes1/hu:/bin/bash

[root@localhost etc]# grep "sh" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
zs100:x:1000:1000::/homes1/zs100:/bin/bash
gentoo:x:4004:4004::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
lisi:x:4006:4006::/homes1/lisi:/bin/bash
hu:x:4007:4007::/homes1/hu:/bin/bash
zheng:x:4008:4008::/homes1/zheng:/bin/bash
zheg:x:4009:4009::/homes1/zheg:/bin/bash
[root@localhost etc]# grep "\<sh" /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost etc]# grep "\Bsh\b" /etc/passwd
root:x:0:0:root,xisanqi,110,112:/root:/bin/bash
zs100:x:1000:1000::/homes1/zs100:/bin/bash
gentoo:x:4004:4004::/users/gentoo:/bin/bash
fedora:x:4005:4005::/users/fedora:/bin/bash
lisi:x:4006:4006::/homes1/lisi:/bin/bash
hu:x:4007:4007::/homes1/hu:/bin/bash
zheng:x:4008:4008::/homes1/zheng:/bin/bash
zheg:x:4009:4009::/homes1/zheg:/bin/bash
分组及引用

(string):将string作为一个整体方便后面引用
\1:引用第一个左括号及其对应的右括号所匹配的内容。
\2:引用第二个左括号及其对应的右括号所匹配的内容。
\n:引用第n个左括号及其对应的右括号所匹配的内容。

[root@localhost etc]# grep "^\([[:alpha:]]\).*\1$" /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
hu:x:4007:4007::/homes1/hu:/bin/bash

扩展的正则表达式

注意:要使用扩展的正则表达式要加-E选项,或直接使用egrep。

匹配字符

和基本正则表达式一样

匹配次数

*:和基本正则表达式一样
?:基本正则表达式式?,这里没有\。
{m,n}:相比基本正则表达式没有了\。
+:匹配其前面的字符至少一次,相当于{1,}。

位置锚定

和基本正则表达式一样

分组及引用

(string):相比基本正则表达式没有了\。
\1:引用部分和基本正则表达式一样。
\2:引用部分和基本正则表达式一样。

或者

a|b:匹配a或b,注意是a指 | 的左边的整体,b同理。比如 C|cat 表示的是 C或cat,而不是Cat或cat,如果要表示Cat或cat,则应该写为 (C|c)at 。记住(string)除了用于引用还用于分组。

注1:默认情况下,正则表达式的匹配工作在贪婪模式下,也就是说它会尽可能长地去匹配,比如某一行有字符串 abacb,如果搜索内容为 “a.*b” 那么会直接匹配 abacb这个串,而不会只匹配ab或acb。

注2:所有的正则字符,如 [ 、* 、( 等,若要搜索 * ,而不是想把 * 解释为重复先前字符任意次,可以使用 * 来转义。

练习

检索出0-255的范围

[root@localhost network-scripts]# egrep "[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]" /etc/sysconfig/network-scripts/ifcfg-eno16777736
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="f29bf5b2-0bb2-40c1-a9c0-acbe48e0dd36"
DEVICE="eno16777736"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值