Linux笔记(28)正则表达式以及grep的一些用法

本文介绍了正则表达式的概念及应用场景,详细解释了元字符和通配符的作用,并通过grep命令演示了如何利用正则表达式进行文本搜索,包括基本和扩展正则的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是正则?

使用一些特殊符号+字母和数字按照某个规则组合成一个公式用来表示某个意思这就叫正则表达式
expression 表达式  、公式 --》 一套方法,里面有特殊字符,字母,数字组合---》
表达某个意思方法--》一个轮子,写正则表达式就是找规律和共性问题

正则用在哪里?

查找的场景特别适合使用正则
vim 、grep、sed、awk等

为什么需要使用它?

 查找的场景特别适合使用正则,大海捞针,海选

shell里称呼: 通配符
正则表达式: 元字符: 有特殊作用的字符,能描述其他字符的字符

  通配符:
        *        匹配0个或者多个字符
        ?       匹配1个字符
        [...]    匹配范围内任意1个字符 [az],[a-z],[az\]
        [^...]   匹配范围内任意1个字符,表示取反,[^0-9]
        {}       组合匹配,touch a{1,3,4}  touch{1...10}
        ^        可以代表以什么开头

grep命令支持扩展正则
1.基本正则grep:元字符: * . ^ $
2.扩展正则元字符egrep: | + ? {}
区别:
扩展正则支持更加多的元字符,能够表示更加复杂的意思,功能更加强大

 用途:使用正则表达式搜索文本,并把匹配的行打印出来
 格式:grep[选项]...  模式  目标文件
 -a: 以文本文件方式搜索
 -c:计算找到的符合行的次数
 -i:忽略大小写   --ignore-case
 -n:顺便输出行号    --line-number
 -o:只显示匹配的内容
 -v:反转查找,输出与模式不相符的行
 -An:同时显示符合条件下的n行
 -Bn:同时显示符合条件上的n行
 -Cn:同时显示符合条件的上面和下面n行
 -r:递归查找  grep -r "xiaomi /lianxi

相关代码:

root@localhost lianxi]# cat name.txt |grep -B2 "lizhuofu"
zhengyang   ZHENGYANG
LIZHUOFU   lizhuofu123  12345lizhuofu
--
liyili lili
zhengyang   ZHENGYANG
LIZHUOFU   lizhuofu123  12345lizhuofu
[root@localhost lianxi]# cat name.txt |grep -A2 "lizhuofu"
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
--
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
[root@localhost lianxi]# cat name.txt |grep -A 2 "lizhuofu"
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
--
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
[root@localhost lianxi]# cat name.txt |grep -C 2 "lizhuofu"
zhengyang   ZHENGYANG
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
--
liyili lili
zhengyang   ZHENGYANG
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
[root@localhost lianxi]#
[root@localhost lianxi]# grep -r  "xiaomi"  /lianxi
/lianxi/name.txt:xiaomi  huawei  oppo
/lianxi/name.txt:XIAOMI xiaomi12234+56  sc@163.com
/lianxi/name.txt:xiaomi  huawei  oppo
/lianxi/name.txt:XIAOMI xiaomi12234+56  sc@163.com
[root@localhost lianxi]# cp name.txt xisha
[root@localhost lianxi]# cp name.txt  daifron/
[root@localhost lianxi]# grep -r  "xiaomi"  /lianxi
/lianxi/daifurong/name.txt:xiaomi  huawei  oppo
/lianxi/daifurong/name.txt:XIAOMI xiaomi12234+56  sc@163.com
/lianxi/daifurong/name.txt:xiaomi  huawei  oppo
/lianxi/daifurong/name.txt:XIAOMI xiaomi12234+56  sc@163.com
/lianxi/xieshan/name.txt:xiaomi  huawei  oppo
/lianxi/xieshan/name.txt:XIAOMI xiaomi12234+56  sc@163.com
/lianxi/xieshan/name.txt:xiaomi  huawei  oppo
/lianxi/xieshan/name.txt:XIAOMI xiaomi12234+56  sc@163.com
/lianxi/name.txt:xiaomi  huawei  oppo
/lianxi/name.txt:XIAOMI xiaomi12234+56  sc@163.com
/lianxi/name.txt:xiaomi  huawei  oppo
/lianxi/name.txt:XIAOMI xiaomi12234+56  sc@163.com
[root@localhost lianxi]#

[root@localhost lianxi]# cat name.txt |grep -E -i  "wenkeke$|fangfang$"
wenke  wenkeke
wanglianfang fangfang
weke  wekeke
wanlianfang fangfang
[root@localhost lianxi]# cat name.txt |egrep -i  "wenkeke$|fangfang$"
wenke  wenkeke
wanglianfang fangfang
wenke  wenkeke
wanglianfang fangfang
[root@localhost lianxi]#

[root@localhost lianxi]# cat name.txt |egrep  "^xiaomi"
xiaomi  huawei  oppo
xiaomi  huawei  oppo
[root@localhost lianxi]# cat name.txt |egrep  "^xiaomi|lili$"
xiaomi  huawei  oppo
liyili lili
xiaomi  huawei  oppo
liyili lili
[root@localhost lianxi]#
[root@localhost lianxi]# cat name.txt |egrep -n  "^$"  空行
17:
20:
22:
[root@localhost lianxi]# cat name.txt |egrep -n  "shejemi"
16:shenjiemi
[root@localhost lianxi]# vim name.txt
[root@localhost lianxi]# cat name.txt |egrep -n  "sheiemi"
16:shenjemi
17:shenjemi lijlin
[root@localhost lianxi]# cat name.txt |egrep -n  "^shenjiemi$"  shejemi单独是一行
16:shejemi
[root@localhost lianxi]# echo "shenjiedami shenxiaomi" >>name.txt
[root@localhost lianxi]# cat name.txt
zhngyang   ZHEGYANG
LZHUOFU   lzhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
weke  wekeke
wanlianfang fagfang
liyili lili
zhengyang   ZHENGYANG
LIZHUOFU   lizhuofu123  12345lizhuofu
xiaomi  huawei  oppo
XIAOMI xiaomi12234+56  sc@163.com
wenke  wenkeke
wanglianfang fangfang
liyili lili
zhaojunjie
shenjiemi
shenjiemi lijunlin

1
2

3

4
shenjiedami shenxiaomi
[root@localhost lianxi]# echo "shenjiedami shenxiaomi" >>name.txt ^C
[root@localhost lianxi]# cat name.txt | egrep "shen...."
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
[root@localhost lianxi]# cat name.txt | egrep "shen.*"  
#表示shen字符串后面出现任意个任意字符
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
[root@localhost lianxi]# cat name.txt | egrep "shen.{4}"
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
[root@localhost lianxi]# cat name.txt | egrep "shen.{4,6}"  
#表示shen字符串后面出现4到6个任意字符
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi

[root@localhost lianxi]# cat name.txt | egrep "^$" -n -v
1:zhengyang   ZHENGYANG
2:LIZHUOFU   lizhuofu123  12345lizhuofu
3:xiaomi  huawei  oppo
4:XIAOMI xiaomi12234+56  sc@163.com

[abc]  取中括号里的一个字符,可以是a也可以是b,也可以是c
"a|b|c"

[root@localhost lianxi]# echo "lijunlin hekunhong zhengyang    yishiying"|egrep "lijunlin\shekunhong"
lijunlin hekunhong zhengyang    yishiying
[root@localhost lianxi]# echo "lijunlin hekunhong zhengyang    yishiying"|egrep "li.*\she.*"
lijunlin hekunhong zhengyang    yishiying
[root@localhost lianxi]# echo "lijunlin hekunhong zhengyang    yishiying"|egrep "li.*\she.{7}"
lijunlin hekunhong zhengyang    yishiying
[root@localhost lianxi]# echo "lijunlin hekunhong zhengyang    yishiying"|egrep "li.{6}\she.{7}"
lijunlin hekunhong zhengyang    yishiying
[root@localhost lianxi]#




[root@localhost lianxi]# echo sannnnnnchuang
sannnnnnchuang
[root@localhost lianxi]# echo sannnnnnchuang|egrep  "san{4}"
sannnnnnchuang
[root@localhost lianxi]# echo sannnnnnchuang|egrep  "san{4,6}"
sannnnnnchuang
[root@localhost lianxi]# echo sannnnnnchuang|egrep  "san+"
sannnnnnchuang
[root@localhost lianxi]#
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san?"
sannnnnnchuang sayong
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san*"
sannnnnnchuang sayong
[root@localhost lianxi]#
[root@localhost lianxi]# echo sannnnnnchuang|egrep  "san?"
sannnnnnchuang
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san?"
sannnnnnchuang sayong
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san*"
sannnnnnchuang sayong
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san{1,}"
sannnnnnchuang sayong
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san{1,5}"
sannnnnnchuang sayong
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san{5}"
sannnnnnchuang sayong
[root@localhost lianxi]# echo sannnnnnchuang sayong|egrep  "san{,5}"
sannnnnnchuang sayong
[root@localhost lianxi]#
[root@localhost lianxi]# cat /etc/ssh/sshd_config |egrep -v "^#|^$"  
[root@localhost lianxi]# cat name.txt |egrep "^[^0-9]"  查找不是以数字开头的行
[^0-9]  非数字
^[^0-9]  不是以数字开头的行
里面的^表示取反
外面的^表示以什么开头

[root@localhost lianxi]# cat /etc/passwd|egrep  "bash"
root:x:0:0:root:/root:/bin/bash
[root@localhost lianxi]# cat /etc/passwd|egrep  "bash$"
root:x:0:0:root:/root:/bin/bash
[root@localhost lianxi]# useradd  bashzhang
[root@localhost lianxi]# cat /etc/passwd|egrep  "bash"
root:x:0:0:root:/root:/bin/bash
bashzhang:x:1004:1004::/home/bashzhang:/bin/bash
[root@localhost lianxi]# useradd  bashzhang1 -s /bin/sh
[root@localhost lianxi]# cat /etc/passwd|egrep  "bash"
root:x:0:0:root:/root:/bin/bash
bashzhang:x:1004:1004::/home/bashzhang:/bin/bash
bashzhang1:x:1005:1005::/home/bashzhang1:/bin/sh
[root@localhost lianxi]# cat /etc/passwd|egrep  "bash$"
root:x:0:0:root:/root:/bin/bash
bashzhang:x:1004:1004::/home/bashzhang:/bin/bash
[root@localhost lianxi]#


单词的定界符号

\<  和  \b   表示单词以什么开头 
\<san  单词以san开头
sanchuang  sanyi  sanhuan  sansiwu
\<sanchuang\> 等同于 \bsanchuang\b
\<sanchuang  等同于\bsanchuang
sanchuang\>  等同于 sanchuang\b

例:查找文本里单词的长度是13个字符的字符串
	grep  "\b[a-Z]{13}"
	hekunhonghong
[root@localhost lianxi]# echo sanchuang  sanyi  sanhuan  sansiwu abcd|egrep "\<sanchuang\>"
sanchuang sanyi sanhuan sansiwu abcd
[root@localhost lianxi]# echo sanchuang  sanchuangtongle sanyi  sanhuan  sansiwu abcd|egrep "\<sanchuang\>"
sanchuang sanchuangtongle sanyi sanhuan sansiwu abcd
[root@localhost lianxi]# echo sanchuang  sanchuangtongle sanyi  sanhuan  sansiwu abcd|egrep "sanchuang"
sanchuang sanchuangtongle sanyi sanhuan sansiwu abcd
[root@localhost lianxi]#
[root@localhost lianxi]# echo sanchuang  sanchuangtongle sanyi  sanhuan  sansiwu abcd|egrep "sanchuang"
sanchuang sanchuangtongle sanyi sanhuan sansiwu abcd
[root@localhost lianxi]#

[root@localhost lianxi]# echo sanchuang  sanchuangtongle sanyi  sanhuan  sansiwu abcd|egrep "\bsanchuang"
sanchuang sanchuangtongle sanyi sanhuan sansiwu abcd
[root@localhost lianxi]# echo sanchuang  sanchuangtongle sanyi  sanhuan  sansiwu abcd|egrep "sanchuang\b"
sanchuang sanchuangtongle sanyi sanhuan sansiwu abcd
[root@localhost lianxi]# echo sanchuang  sanchuangtongle sanyi  sanhuan  sansiwu abcd|egrep "\bsanchuang\b"
sanchuang sanchuangtongle sanyi sanhuan sansiwu abcd
[root@localhost lianxi]#

[root@localhost lianxi]# echo  hekunhonghong  zhengyangyang123 wangshenghu feng|egrep  "\b[a-Z]{11,13}\b"
hekunhonghong zhengyangyang123 wangshenghu feng
[root@localhost lianxi]# man grep
[root@localhost lianxi]#


[root@localhost lianxi]# echo  "foooo eeeeabc oo ee"|egrep "(oo){2}"
foooo eeeeabc oo ee
[root@localhost lianxi]# echo  "foooooo eeeeabc oo ee"|egrep "(oo){2}"
foooooo eeeeabc oo ee
[root@localhost lianxi]# echo  "foooooo eeeeabc oo ee"|egrep "(oo|ee){2}"
foooooo eeeeabc oo ee
[root@localhost lianxi]# echo  "foooooo eeeeabc oo ee"|egrep "(abc|ee){2}"
foooooo eeeeabc oo ee
[root@localhost lianxi]# echo  "foooooo eeeeabc oo ee abcabc"|egrep "(abc|ee){2}"
foooooo eeeeabc oo ee abcabc
[root@localhost lianxi]#
[root@localhost lianxi]# echo 'abce$#!'|egrep "$"
abce$#!
[root@localhost lianxi]# echo 'abce$#!'|egrep "\$"
abce$#!
[root@localhost lianxi]# echo 'abce$#!'|egrep "\\$"
abce$#!
[root@localhost lianxi]# echo 'abce$#!'|egrep "\\$"
abce$#!

从文本里过滤出所有邮箱地址

邮箱: 特点: 共性问题
格式:字符串1@字符串2.字符串3
695811769@qq.com
fengdeyong123@163.com
fengdeyong_123@164.cn

字符串1:  a-Z _ 0-9
字符串2:  a-Z 0-9
字符串3: a-Z
[0-z_]+@[0-z]+\.[a-z]+
[root@localhost lianxi]# cat mail.txt |egrep -o  "[0-z_.]+@[0-z]+\.[a-z]+"|sort|uniq
10001@qq.com
1234feng@163.com
123_ui@12306.cn
8898989@qq.com
fengdeyong@sina.com
feng@qq.com
liudehua@sina.com
meng.xianhui@yahoo.cn
qilu@qilu.edu
yishiying_1213@163.com
zhao@163.com
[root@localhost lianxi]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值