Shell脚本中的正则表达式(2)

在Linux/UNIX系统中包含很多种文本处理器或文本编辑器,其中包括VIM编辑器与grep等。而grep,sed,awk更是shell编程中经常用到的文本处理工具,被称为shell编程三剑客。

1、sed工具

sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、
替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于shell脚本中,用以完成各种自动化处理任务。

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

读取:sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,patterm space)。

执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。

显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直到所有内容被处理完。

2、sed命令常见的用法 sed[选项] '操作' 参数 sed [选项] -f scriptfile 参数

常见的sed命令选项主要包含以下几种:

-e或--expression=:表示用指定命令或者脚本来处理输入的文本文件。

-f或--file=:表示用指定的脚本文件来处理输入的文本文件。

-h或--help:显示帮助。

-n、--quiet或silent:表示仅显示处理后的结果。

-i:直接编辑文本文件。
“操作”用于指定对文件操作的动作行为,也就是sed的命令。通常情况下是采用的“[n1[,n2]]”操作参数的格式。n1、n2是可选的,不一定会存在,代表选择进行操作的行数,如操作需要在5~20行之间进行,则表示为“5,20动作行为”。常见的操作包括以下几种:

a:增加,在当前行下面增加一行指定内容。

c:替换,将选定行替换为指定内容。

d:删除,删除选定的行。

i:插入,在选定行上面插入一行指定内容。

p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与“-n”选项一起使用。

s:替换,替换指定字符。

y:字符转换。

3、用法示例 1)输出符号条件的文本(p表示正常输出) [root@centos01 ~]# sed -n '3p' test.txt <!--输出第三行--> The home of Football on BBC Sport online. [root@centos01 ~]# sed -n '3,5p' test.txt <!--输出第三行到第五行--> The home of Football on BBC Sport online. the tongue is boneless but it breaks bones.12! google is the best tools for search keyword. [root@centos01 ~]# sed -n 'p;n' test.txt <!--输出所有奇数行--> he was short and fat. The home of Football on BBC Sport online. google is the best tools for search keyword. PI=3.141592653589793238462643383249901429 Actions speak louder than words #woood # #woooooood # I bet this place is really spooky late at night! I shouldn't have lett so tast. [root@centos01 ~]# sed -n 'p;n' test.txt <!--输出所有偶数行--> he was short and fat. The home of Football on BBC Sport online. google is the best tools for search keyword. PI=3.141592653589793238462643383249901429 Actions speak louder than words #woood # #woooooood # I bet this place is really spooky late at night! I shouldn't have lett so tast. [root@centos01 ~]# sed -n '1,5{p;n}' test.txt <!--输出第一行到第五行之间的奇数行 --> he was short and fat. The home of Football on BBC Sport online. google is the best tools for search keyword. [root@centos01 ~]# sed -n '10,${n;p}' test.txt <!--输出第10行至文件尾之间的偶数行--> #woood # #woooooood # I bet this place is really spooky late at night! I shouldn't have lett so tast. 2)Sed命令结合正则表达式 [root@centos01 ~]# sed -n '/the/p' test.txt <!--输出包含the的行--> the tongue is boneless but it breaks bones.12! google is the best tools for search keyword. The year ahead will test our political establishment to the limit. [root@centos01 ~]# sed -n '4,/the/p' test.txt<!--输出从第4行至第一个包含the的行--> the tongue is boneless but it breaks bones.12! google is the best tools for search keyword. [root@centos01 ~]# sed -n '/the/=' test.txt <!--输出包含the的行所在的行号, 等号(=)用来输出行号--> 4 5 6 [root@centos01 ~]# sed -n '/^PI/p' test.txt <!--输出以PI开头的行--> PI=3.141592653589793238462643383249901429 [root@centos01 ~]# sed -n '/\<wood\>/p' test.txt <!--输出包含单词wood的行, \<、\>代表单词边界--> a wood cross! 3)删除符合条件的文件(d) [root@centos01 ~]# nl test.txt | sed '3d' <!--删除第3行--> 1 he was short and fat. 2 He was wearing a blue polo shirt with black pants. 4 the tongue is boneless but it breaks bones.12! 5 google is the best tools for search keyword. 6 The year ahead will test our political establishment to the limit. 7 PI=3.141592653589793238462643383249901429 8 a wood cross! 9 Actions speak louder than words 10 11 #woood # 12 13 #woooooood # 14 15 16 AxyzxyzxyzxyzC 17 18 19 I bet this place is really spooky late at night! 20 Misfortunes never come alone/single. 21 I shouldn't have lett so tast. [root@centos01 ~]# nl test.txt | sed '3,5d' <!--删除第3~5行--> 1 he was short and fat. 2 He was wearing a blue polo shirt with black pants. 6 The year ahead will test our political establishment to the limit. 7 PI=3.141592653589793238462643383249901429 8 a wood cross! 9 Actions speak louder than words 10 11 #woood # 12 13 #woooooood # 14 15 16 AxyzxyzxyzxyzC 17 18 19 I bet this place is really spooky late at night! 20 Misfortunes never come alone/single. 21 I shouldn't have lett so tast. [root@centos01 ~]# sed '/^[a-z]/d' test.txt <!--删除以小写字母开头的行--> He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online. The year ahead will test our political establishment to the limit. PI=3.141592653589793238462643383249901429 Actions speak louder than words #woood # #woooooood # AxyzxyzxyzxyzC I bet this place is really spooky late at night! Misfortunes never come alone/single. I shouldn't have lett so tast. 4)替换符合条件的文本 [root@centos01 ~]# sed 's/the/THE/' test.txt <!--将每行中的第一个the替换为THE--> he was short and fat. He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online. THE tongue is boneless but it breaks bones.12! google is THE best tools for search keyword. The year ahead will test our political establishment to THE limit. PI=3.141592653589793238462643383249901429 a wood cross! Actions speak louder than words #woood # #woooooood # AxyzxyzxyzxyzC I bet this place is really spooky late at night! Misfortunes never come alone/single. I shouldn't have lett so tast. [root@centos01 ~]# sed 's/l/L/2' test.txt <!--将每行中的第三个l替换为L--> he was short and fat. He was wearing a blue poLo shirt with black pants. The home of FootbalL on BBC Sport online. the tongue is boneless but it breaks bones.12! google is the best tooLs for search keyword. The year ahead wilL test our political establishment to the limit. PI=3.141592653589793238462643383249901429 a wood cross! Actions speak louder than words #woood # #woooooood # AxyzxyzxyzxyzC I bet this place is reaLly spooky late at night! Misfortunes never come alone/singLe. I shouldn't have Lett so tast. [root@centos01 ~]# sed 's/^/#/' test.txt <!--在每行行首插入#号--> #he was short and fat. #He was wearing a blue polo shirt with black pants. #The home of Football on BBC Sport online. #the tongue is boneless but it breaks bones.12! #google is the best tools for search keyword. #The year ahead will test our political establishment to the limit. #PI=3.141592653589793238462643383249901429 #a wood cross! #Actions speak louder than words # ##woood # # ##woooooood # # # #AxyzxyzxyzxyzC # # #I bet this place is really spooky late at night! #Misfortunes never come alone/single. #I shouldn't have lett so tast. [root@centos01 ~]# sed '/the/s/o/0/g' test.txt <!--将包含the的所有行中的o都替换为0--> he was short and fat. He was wearing a blue polo shirt with black pants. The home of Football on BBC Sport online. the t0ngue is b0neless but it breaks b0nes.12! g00gle is the best t00ls f0r search keyw0rd. The year ahead will test 0ur p0litical establishment t0 the limit. PI=3.141592653589793238462643383249901429 a wood cross! Actions speak louder than words #woood # #woooooood # AxyzxyzxyzxyzC I bet this place is really spooky late at night! Misfortunes never come alone/single. I shouldn't have lett so tast. 3、awk工具

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11639.html