温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Shell脚本正则表达式之——grep、egrep、sed(内含多个Demo)

发布时间:2020-07-28 06:14:14 来源:网络 阅读:753 作者:JarryZ 栏目:系统运维

Grep命令

基本正则表达式实例之查找特定字符:

这里我们就以存放本机所有用户的/etc/passwd文件做实例

Demo1:
[root@localhost ~]# grep -n "root" /etc/passwd      //-n表示显示行号
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

基本正则表达式实例之查找集合字符:

有重复的字符时,可使用“[ ]”来进行集合匹配,每次只匹配“[ ]”中的一个字符。

Demo2:
[root@localhost ~]# grep -n "[fn]tp" /etc/passwd
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
27:ntp:x:38:38::/etc/ntp:/sbin/nologin

基本正则表达式实例之反向选择:

在“[ ]”中括号中添加“^”表示进行反向选择(有一定的基础的朋友肯定知道“^[ ]”表示定位行首,这里“^”内外位置意思将完全不同。)

Demo3:
[root@localhost ~]# grep -n "^[^root]" /etc/passwd       //匹配除了以root开头的所有选项
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
......
42:named:x:25:25:Named:/var/named:/sbin/nologin

基本正则表达式实例之转义符:

在正则表达式中一个元字符,所以在这里需要用转义字符“\”将具有特殊意义的字符转化成普通字符。

Demo4:
[root@localhost ~]# grep -n '\.$' test.txt 1:he was short and fat.
2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.

基本正则表达式实例——查找任一字符&查找重复字符:

在正则表达式中小数点(.)也是一个元字符,代表任意一个字符。

Demo5:
[root@localhost ~]# grep -n "r..t" /etc/passwd        //(.)小数点这里代表任一字符
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

在上述结果中,“root”字符串“r..t”匹配规则。若想要查询 oo、ooo、ooooo 等资料,则需要使用星号(*)元字符。但需要注意的是,“*”代表的是重复零个或多个前面的单字符。“o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符

Demo5:
[root@localhost ~]# grep -n "oo*" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
......

基本正则表达式实例之查找连续字符范围:

例如,查找三到五个 o 的连续字符,这个时候就需要使用基础正则表达式中的限定范围的字符“{ }”。因为“{ }”在 Shell 中具有特殊 意义,所以在使用“{ }”字符时,需要利用转义字符“\”,将“{ }”字符转换成普通字符。

Demo6:
[root@localhost ~]# grep -n "0\{2,\}" /etc/passwd       //表示中间包含2以上o的字符串
11:games:x:12:100:games:/usr/games:/sbin/nologin
41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash

Egrep命令

此外,grep 命令仅支持基础正则表达式,如果使用扩展正则表达式,需要使用 egrep 或 awk 命令。awk 命令在后面的进行讲解,这里我们直接使用 egrep 命令。egrep 命令与 grep 命令的用法基本相似。(grep命令能用的egrep命令同样能够使用)

扩展正则表达式元字符 作用
+ 作用:重复一个或者一个以上的前一个字符
作用:零个或者一个的前一个字符
| 作用:使用或者(or)的方式找出多个字符
() 作用:查找“组”字符串
()+ 作用:辨别多个重复的组
Demo7:
[root@localhost ~]# egrep -n "10+" /etc/passwd             //使用“+”扩展元字符
11:games:x:12:100:games:/usr/games:/sbin/nologin
31:qemu:x:107:107:qemu user:/:/sbin/nologin
41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash
[root@localhost ~]# egrep -n "10?" /etc/passwd             //使用“?”扩展元字符
2:bin:x:1:1:bin:/bin:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
[root@localhost ~]# egrep -n 'root|zhy' /etc/passwd        //使用“|”扩展元字符
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
41:zhy:x:1000:1000:zhy:/home/zhy:/bin/bash
[root@localhost ~]# egrep -n '(f|n)tp' /etc/passwd        //使用“()”扩展元字符,可与“|”一起使用
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
27:ntp:x:38:38::/etc/ntp:/sbin/nologin

Sed命令:

sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作

sed 的工作流程主要包括读取、执行和显示三个过程:

1.读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)。
2.执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
3.显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

默认情况下,所有的sed命令都是在模式空间中进行,并不会进行保存。

Sed命令格式:

sed [选项] '操作' 参数
sed [选项] -f scriptfile 参数 // scriptfile 表示脚本文件

常用选项:

-e :表示用指定命令或者脚本来处理输入的文本文件。
-f :表示用指定的脚本文件来处理输入的文本文件。
-h :显示帮助。
-n:表示仅显示处理后的结果。
-i:直接编辑文本文件。

常用的“操作”参数:

a:增加,在当前行下面增加一行指定内容。
c:替换,将选定行替换为指定内容。
d:删除,删除选定的行
i:插入,在选定行上面插入一行指定内容。
p:打印,其通常与“-n”选项一起使用
s:替换,替换指定字符。
y:字符转换。

基本用法实例:

输出所有,效果等同cat命令:
[root@localhost ~]# sed -n 'p' /etc/passwd                //效果等同cat命令
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
......
输出某一特定行,或者某一段行:
[root@localhost ~]# sed -n '10p' /etc/passwd                   //输出第10行内容
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sed -n '2,4p' /etc/passwd                 //输出2~4行内容
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
输出奇数行:
[root@localhost ~]# sed -n 'n;p' /etc/passwd                //输出奇数行,偶数行为p;n
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
......

同样,除了基本的使用方法,sed命令也可以结合正则表达式进行使用

输出包含特定内容的行(和grep命令一样,可以使用^、$来定位行首、行尾):
[root@localhost ~]# sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
输出包含特定单词的行:
[root@localhost ~]# sed -n '/\<root\>/p' /etc/passwd           //\<  \>代表单词边界
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
替换符合条件的文本:

sed 's/the/THE/' test.txt //将每行中的第一个the 替换为 THE
sed 's/l/L/3' test.txt //将每行中的第 3 个l 替换为L
sed 's/the/THE/g' test.txt //将文件中的所有the 替换为THE
sed 's/o//g' test.txt //将文件中的所有o 删除(替换为空串)
sed 's/^/#/' test.txt //在每行行首插入#:号
sed '/the/s/^/#/' test.txt //在包含the 的每行行首插入#号
sed 's/$/EOF/' test.txt //在每行行尾插入字符串EOF
sed '3,5s/the/THE/g' test.txt //将第 3~5 行中的所有the 替换为 THE
sed '/the/s/o/O/g' test.txt //将包含the 的所有行中的o 都替换为 O

将文本进行迁移:

sed '/the/{H;d};$G' test.txt //将包含the 的行迁移至文件末尾,{;}用于多个操作
sed '1,5{H;d};17G' test.txt //将第 1~5 行内容转移至第 17 行后
sed '/the/w out.file' test.txt //将包含the 的行另存为文件out.file
sed '/the/r /etc/hostname' test.txt //将文件/etc/hostname 的内容添加到包含the 的每行以后
sed '3aNew' test.txt //在第 3 行后插入一个新行,内容为 New
sed '/the/aNew' test.txt //在包含the 的每行后插入一个新行,内容为 New
sed '3aNew1\nNew2' test.txt //在第 3 行后插入多行内容,中间的\n 表示换行

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI