正则表达式三剑客之二sed工具

网友投稿 235 2022-10-13

正则表达式三剑客之二sed工具

三剑客之二sed工具

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

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

读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓

冲区中(又称模式空间,pattern space)。执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

注意:默认情况下,所有的 sed 命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。

sed命令常见用法

调用 sed 命令有两种格式。其中,“参数”是指操作的目标文件,当存在多个操作对象时用,文件之间用逗号“,”分隔;而 scriptfile 表示脚本文件,需要用“-f”选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件。

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

(1)e 或–expression=:表示用指定命令或者脚本来处理输入的文本文件。(2)-f 或–file=:表示用指定的脚本文件来处理输入的文本文件。(3)-h 或–help:显示帮助。(4)-n、–quiet 或 silent:表示仅显示处理后的结果。(5)-i:直接编辑文本文件。“操作”用于指定对文件操作的动作行为,也就是 sed 的命令。通常情况下是采用的“[n1[,n2]]”操作参数的格式。n1、n2 是可选的,不一定会存在,代表选择进行操作的行数,如操作需要在 5~20 行之间进行,则表示为“5,20 动作行为”。常见的操作包括以下几种。(1)a:增加,在当前行下面增加一行指定内容。(2)c:替换,将选定行替换为指定内容。(3)d:删除,删除选定的行。(4)i:插入,在选定行上面插入一行指定内容。(5)p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。(6)s:替换,替换指定字符。(7)y:字符转换。

一.sed工具用法

首先我们准备一个测试文件

[root@localhost ~]# vim chen.txt Use CDROM installation media cdrom. thethethe. THE THEASDHAS Use graphical install. graphical. best test ASSDJFXYxyzC AxyzxyzxyzC keyboard --vckeymap=cn --xlayouts='cn' System language lang zh_CN.UTF-8 Network information network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate network --hostname=localhost.localdomain Root password rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ. System services services --enabled="chronyd" #System timezone tast

(1)输出符合条件的文本(p表示正常输出)

#version=DEVEL #System authorization information auth --enableshadow --passalgo=sha512 Use CDROM installation media cdrom. thethethe. THE THEASDHAS Use graphical install. graphical. best test ASSDJFXYxyzC AxyzxyzxyzC keyboard --vckeymap=cn --xlayouts='cn' System language lang zh_CN.UTF-8 Network information network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate network --hostname=localhost.localdomain Root password rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ. System services services --enabled="chronyd" System timezone tast bet Run the Setup Agent on first boot. firstboot --enable ignoredisk --only-use=sda wood wd wod woooooooood dfsjdjoooooof Foofddd 124153 3234 342222222 faasd11 2 ZASASDNA short shirt

(2)输出第3行

[root@localhost ~]# sed -n '3p' chen.txt auth --enableshadow --passalgo=sha512

(3)输出3^5行

[root@localhost ~]# sed -n '3,5p' chen.txt auth --enableshadow --passalgo=sha512 #Use CDROM installation media cdrom.

(4)输出所有奇数行,n表示读入下一行资料,p在前面就是第一行显示

[root@localhost ~]# sed -n 'p;n' chen.txt #version=DEVEL auth --enableshadow --passalgo=sha512 cdrom. THE Use graphical install. best ASSDJFXYxyzC keyboard --vckeymap=cn --xlayouts='cn' lang zh_CN.UTF-8 Network information network --hostname=localhost.localdomain Root password System services System timezone bet firstboot --enable wood wod dfsjdjoooooof 124153 342222222 2 short

(5)输出所有偶数行,n在前面就是第一行不显示,直接从第二行开始

[root@localhost ~]# sed -n 'n;p' chen.txt System authorization information Use CDROM installation media thethethe. THEASDHAS graphical. test AxyzxyzxyzC #System language network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ. services --enabled="chronyd" tast Run the Setup Agent on first boot. ignoredisk --only-use=sda wd woooooooood Foofddd 3234 faasd11 ZASASDNA shirt

(6)输出第 10 行至文件尾之间的偶数行

[root@localhost ~]# sed -n '10,${n;p}' chen.txt best ASSDJFXYxyzC keyboard --vckeymap=cn --xlayouts='cn' lang zh_CN.UTF-8 Network information network --hostname=localhost.localdomain Root password# System services #System timezone bet firstboot --enable wood wod dfsjdjoooooof 124153 342222222 2 short

在执行“sed –n‘10,${n;p}’test.txt”命令时,读取的第 1 行是文件的第 10 行,读取的第 2 行是文件的第 11 行,依此类推,所以输出的偶数行是文件的第 11 行、13 行直至文件结尾,其中包括空行。以上是 sed 命令的基本用法,sed 命令结合正则表达式时,格式略有不同,正则表达式以“/”包围。例如,以下操作是 sed 命令与正则表达式结合使用的示例。

(7)输出包含the 的行

[root@localhost ~]# sed -n '/the/p' chen.txt thethethe. Run the Setup Agent on first boot.

(8)输出从第 4 行至第一个包含 the 的行

[root@localhost ~]# sed -n '4,/the/p' chen.txt Use CDROM installation media cdrom. thethethe.

(9)输出包含the 的行所在的行号,等号(=)用来输出行号

[root@localhost ~]# sed -n '/the/=' chen.txt 6 30

(10)输出以fi开头的行

[root@localhost ~]# sed -n '/^fi/p' chen.txt firstboot --enable

(11) 输出以数字结尾的行

[root@localhost ~]# sed -n '/[0-9]$/p' chen.txt auth --enableshadow --passalgo=sha512 lang zh_CN.UTF-8 124153 3234 342222222 faasd11 2

(12)输出包含单词wood 的行,<、>代表单词边界

[root@localhost ~]# sed -n '/\/P' chen.txt wood

(二)删除符合条件的文本(d)

因为后面的示例还需要使用测试文件 test.txt,所以在执行删除操作之前需要先将测试文件备份。以下示例分别演示了 sed 命令的几种常用删除用法。下面命令中 nl 命令用于计算文件的行数,结合该命令可以更加直观地查看到命令执行的结果。

(1)删除第 3 行

[root@localhost ~]# nl chen.txt | sed '3d' 1 #version=DEVEL 2 # System authorization information 4 # Use CDROM installation media 5 cdrom. 6 thethethe.

(2)删除第 3~5 行

[root@localhost ~]# nl chen.txt | sed '3,5d' 1 #version=DEVEL 2 # System authorization information 6 thethethe.

(3)//删除包含cross 的行,原本的第 47行被删除

//删除不包含cross 的行,用!符号表示取反操作,如’/cross/!d’

[root@localhost ~]# nl chen.txt | sed '3,5d' 45 short 46 shirt

(4)删除以小写字母开头的行

[root@localhost ~]# sed '/[^a-z]/d' chen.txt

best test crp tast cross bet wood wd wod woooooooood dfsjdjoooooof short shirt

(5)删除以"."结尾的行

[root@localhost ~]# sed '/\.$/d' chen.txt

(6)删除所有空行

[root@localhost ~]# sed '/^$/d' chen.txt注意: 若是删除重复的空行,即连续的空行只保留一个, 执行“ sed –e ‘/^KaTeX parse error: Expected group after '^' at position 6: /{n;/^̲/d}’test.txt”命令即可实现。其效果与“cat -s test.txt”相同,n 表示读下一行数据。

(三)替换符合条件的文本

(1)在使用 sed 命令进行替换操作时需要用到 s(字符串替换)、c(整行/整块替换)、y(字符转换)命令选项

将每行中的第一个the 替换为 THE

[root@localhost ~]# sed 's/the/THE/' chen.txt #version=DEVEL System authorization information auth --enableshadow --passalgo=sha512 Use CDROM installation media cdrom. THEthethe. THE

(2)/将每行中的第 3 个l 替换为L

[root@localhost ~]# sed 's/l/L/' chen.txt #version=DEVEL System authorization information auLth --enableshadow --passalgo=sha512 Use CDROM instaLlation media cdLrom.

(3)将文件中的所有the 替换为THE

[root@localhost ~]# sed 's/the/THE/g' chen.txt #version=DEVEL System authorization information aulth --enableshadow --passalgo=sha512 Use CDROM installation media cdlrom. THETHETHE. THE THEASDHAS

(4)将文件中的所有o 删除(替换为空串)

[root@localhost ~]# sed 's/o//g' chen.txt

(5)在每行行首插入#号

[root@localhost ~]# sed 's/^/#/g' chen.txt ##version=DEVEL ##System authorization information #aulth --enableshadow --passalgo=sha512 ##Use CDROM installation media #cdlrom. #thethethe. #THE #THEASDHAS ##Use graphical install.

(6)在每行行尾插入字符串EOF

[root@localhost ~]# sed 's/$/EOF/g' chen.txt ersion=DEVELEOF ystem authorization informationEOF aulth --enableshadow --passalgo=sha512EOF se CDROM installation mediaEOF cdlrom.EOF thethethe.EOF THEEOF THEASDHASEOF Use graphical install.EOF

(四)迁移符合条件的文本

H,复制到剪贴板;g、G,将剪贴板中的数据覆盖/追加至指定行;w,保存为文件;r,读取指定文件;a,追加指定内容。

(1)将包含the 的行迁移至文件末尾,{;}用于多个操作

[root@localhost ~]# sed '/the/{H;d};$G' chen.txt #version=DEVEL System authorization information aulth --enableshadow --passalgo=sha512 Use CDROM installation media cdlrom. THE THEASDHAS

(2)将第 1~5 行内容转移至第 17 行后

[root@localhost ~]# sed '1,5{H;d};17G' chen.txt thethethe. THE THEASDHAS Use graphical install. graphical. best

(3)将包含the 的行另存为文件/opt/abc.txt

[root@localhost ~]# sed '/the/w /opt/abc.txt' chen.txt [root@localhost ~]# cd /opt [root@localhost opt]# ls abc.txt rh [root@localhost opt]# cat abc.txt thethethe. #Run the Setup Agent on first boot.

(4)将文件/etc/hostname 的内容添加到包含the 的每行以后

[root@localhost ~]# sed '/the/r /etc/hostname' chen.txt Run the Setup Agent on first boot. localhost.localdomain

(5)在第 3 行后插入一个新行,内容为 New

[root@localhost ~]# sed '3aNEW' chen.txt #version=DEVEL System authorization information aulth --enableshadow --passalgo=sha512 NEW

(6)在包含the 的每行后插入一个新行,内容为 New

[root@localhost ~]# sed '/the/aNEW' chen.txt Run the Setup Agent on first boot. NEW

(7)在第 3 行后插入多行内容,中间的\n 表示换行

[root@localhost ~]# sed '3aNEW\nNEW2\nNEW3' chen.txt version=DEVEL System authorization information aulth --enableshadow --passalgo=sha512 NEW NEW2 NEW3

(五)脚本编辑文件

root@localhost ~]# vim wq.com #!/bin/bash #指定样本文件路径、配置文件路径 SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf " CONFIG="/etc/vsftpd/vsftpd.conf" #备份原来的配置文件,检测文件名为/etc/vsftpd/vsftpd.conf.bak 备份文件是否存在, 若不存在则使用 cp 命令进行文件备份 [ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak #基于样本配置进行调整,覆盖现有文件 sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG sed -i -e '/^local_enable/s/NO/YES/g' -e'/^write_enable/s/NO/YES/g' $CONFIG grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG #启动vsftpd 服务,并设为开机后自动运行systemctl restart vsftpd systemctl enable vsftpd

[root@localhost ~]# chmod +x wq.sh [root@localhost ~]# ./wq.sh

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot 集成dubbo的步骤详解
下一篇:shell脚本正则表达式三剑客之一(grep,egrep)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~