shell-----正则表达式之sed

一.sed工具概述

  • sed工具是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。
  • sed也可以在交互的情况下实现相当复杂的文本处理操作,被应用于shell脚本中,用以完成各种自动化处理任务
  • sed的工作流程主要包括读取、执行和显示三个过程
  • 读取:sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区(又称为模式空间,pattern space)
  • 执行:默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行
  • 显示:发送修改后的内容到输出流。再发送数据流,模式空间将会被清空

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

二.sed命令常见用法

  • sed命令有两种格式,sed[选项]  ‘操作’   参数  ; sed [选项]  -f scriptfile 参数
  • 其中,“参数”是指操作的目标文件,当存在多个操作对象时用,文件之间用逗号“,”分隔;而scriptfile表示脚本文件,需要用“-f”选项指定,当脚本文件出现在目标文件之前时,表示通过指定的脚本文件来处理输入的目标文件
  • 有如下选项:
  • -e或-expression=:表示用指定命令或者脚本来处理输入的文本文件
  • -f或-file=:表示用指定的脚本文件来处理输入的文本文件
  • -h或-help:显示帮助
  • -n、--quite或silent:表示仅显示处理后的结果
  • -i :直接编辑文本文件
  • 操作,用于指定具体的动作行为,通常情况下采用”[n1[,n2]]“操作参数的格式。具体如下:
  • a:增加,在当前行下增加一行指定内容
  • c:替换,将选定行替换为指定内容
  • d:删除,删除选定的行
  • i:插入,在选定行上面插入一行指定内容
  • p:打印,如果同时指定行,表达打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。通常与-n一起使用
  • s:替换,替换指定字符
  • y:字符转换

三.sed具体用法实例

  • 输出符合条件的文件,-n表示输出处理后的结果,p表示正常输出
输出所有内容
[root@localhost ~]# sed -n 'p' b.txt
1:the gilrs 
2:the boys  
3:the gg
4:the mm

5:the ff

[root@localhost ~]# 

输出第3行内容
[root@localhost ~]# sed -n '3p' b.txt
3:the gg
[root@localhost ~]# 

输出第3行到第6行内容
[root@localhost ~]# sed -n '3,6p' b.txt
3:the gg
4:the mm

5:the ff
[root@localhost ~]# 

输出奇数行和偶数行(相对的奇和偶)
[root@localhost ~]# sed -n 'p;n' b.txt //奇数行
1:the gilrs 
3:the gg
5:
[root@localhost ~]# sed -n 'n;p' b.txt  //偶数行
2:the boys  
4:the mm
6:the ff
[root@localhost ~]# 

输出从2到5行的奇数行
[root@localhost ~]# sed -n '2,5{p;n}' b.txt
2:the boys  
4:the mm
[root@localhost ~]# 

输出从第3行到最后一行的偶数行
[root@localhost ~]# sed -n '3 ,${n;p}' b.txt
4:the mm
6:the ff
[root@localhost ~]# 

  • sed结合正则表达式,输出相应符合条件的行
输出包含字符o的行
[root@localhost ~]# sed -n '/o/p' a.txt
cold.
wod
often
ifconfig
ontime
AxyzxyozxyzC
[root@localhost ~]# 


输出从第4行开始第一个包含字符o的行,包括第四行
[root@localhost ~]# sed -n '4,/o/p' a.txt
777
wod
[root@localhost ~]# 


输出包含字符o的行的行号
[root@localhost ~]# sed -n '/o/=' a.txt
2
5
7
8
10
15
[root@localhost ~]#

输出以字符o和以字符o结尾的行
[root@localhost ~]# sed -n '/^o/p' a.txt
often
ontime
ontimo
[root@localhost ~]# sed -n '/o$/p' a.txt
ontimo

输出包含单词ontime的行
[root@localhost ~]# sed -n '/\<ontime\>/p' a.txt
ontime
[root@localhost ~]# 
  • 删除符合条件的文本
列出所有内容且以序号标好
[root@localhost ~]# nl b.txt
     1	1:the gilrs 
     2	2:the boys  
     3	3:the gg
     4	4:the mm
     5	5:
     6	6:the ff

删除第三行
[root@localhost ~]# nl b.txt | sed '3d'
     1	1:the gilrs 
     2	2:the boys  
     4	4:the mm
     5	5:
     6	6:the ff

删除第三行到第六行
[root@localhost ~]# nl b.txt | sed '3,6d'
     1	1:the gilrs 
     2	2:the boys 

删除包含字符the的行 
[root@localhost ~]# nl b.txt | sed '/the/d'
     5	5:

删除空行
[root@localhost ~]# nl b.txt
     1	1:the gilrs 
     2	2:the boys  
     3	3:the gg
     4	4:the mm
       
     5	6:the ff
[root@localhost ~]# sed '/^$/d' b.txt
1:the gilrs 
2:the boys  
3:the gg
4:the mm
6:the ff
[root@localhost ~]# 

删除以字母开头或者.结尾的行
[root@localhost ~]# nl b.txt
     1	1:the gilrs 
     2	2:the boys  
     3	3:the gg
     4	4:the mm
     5	abc
     6	12adc.
     7	6:the ff
[root@localhost ~]# sed '/^a/d' b.txt | sed '/\.$/d'
1:the gilrs 
2:the boys  
3:the gg
4:the mm
6:the ff
[root@localhost ~]# 
  • 替换符合条件的文本,s(字符串替换)、c(整行/整块替换)、y(字符转换)
将每一行中第一个A替换成THE
[root@192 ~]# sed 's/A/THE/' a.txt
THEb.
cold.

777
wod
bet
often
ifconfig
islase
ontime
ontimo
beat
THExyzC
THExyzxyzC
THExyzxzy0927C
THExyzxyozxyzC

将全文本中x替换成THE
[root@192 ~]# sed 's/x/THE/g' a.txt
Ab.
cold.

777
wod
bet
often
ifconfig
islase
ontime
ontimo
beat
ATHEyzC
ATHEyzTHEyzC
ATHEyzTHEzy0927C
ATHEyzTHEyozTHEyzC

将全文本中的x删除
[root@192 ~]# sed 's/x//g' a.txt
Ab.
cold.

777
wod
bet
often
ifconfig
islase
ontime
ontimo
beat
AyzC
AyzyzC
Ayzzy0927C
AyzyozyzC

在每一行的行首加上#符号
[root@192 ~]# sed 's/^/#/' a.txt
#Ab.
#cold.
#
#777
#wod
#bet
#often
#ifconfig
#islase
#ontime
#ontimo
#beat
#AxyzC
#AxyzxyzC
#Axyzxzy0927C
#AxyzxyozxyzC
#

在每一行的行尾加上#
[root@192 ~]# sed 's/$/#/' a.txt
Ab.#
cold.#
#
777#
wod#
bet#
often#
ifconfig#
islase#
ontime#
ontimo#
beat#
AxyzC#
AxyzxyzC#
Axyzxzy0927C#
AxyzxyozxyzC#
#

在3到5行的行首加上#
[root@192 ~]# sed '3,5s/^/#/' a.txt
Ab.
cold.
#
#777
#wod
bet
often
ifconfig
islase
ontime
ontimo
beat
AxyzC
AxyzxyzC
Axyzxzy0927C
AxyzxyozxyzC

在包含的A字符的行首加上#
[root@192 ~]# sed '/A/s/^/#/' a.txt
#Ab.
cold.

777
wod
bet
often
ifconfig
islase
ontime
ontimo
beat
#AxyzC
#AxyzxyzC
#Axyzxzy0927C
#AxyzxyozxyzC

  • 迁移符合条件的文本,H,复制到剪切板;g、G,将剪切板中的数据覆盖/追加至指定行;w,保存为文件;r,读取指定文件;a,追加指定内容
将第一行到第三行的内容复制到第五行下面
[root@192 ~]# sed '1,3{H;d};5G' b.txt
the mm
abc

the gilrs 
the boys  
the gg
12adc.
the ff

将包含the的行复制,粘贴到最后一行下面
[root@192 ~]# sed '/the/{H};$G' b.txt
the gilrs 
the boys  
the gg
the mm
abc
12adc.
the ff

the gilrs 
the boys  
the gg
the mm
the ff

把含有the的行保存到c.txt文件中
[root@192 ~]# sed '/the/w c.txt' b.txt
the gilrs 
the boys  
the gg
the mm
abc
12adc.
the ff
[root@192 ~]# ls
anaconda-ks.cfg  b.txt                 公共  图片  音乐
a.txt            c.txt                 模板  文档  桌面
awkprof.out      initial-setup-ks.cfg  视频  下载
[root@192 ~]# cat c.txt
the gilrs 
the boys  
the gg
the mm
the ff

将/etc/hostname内的内容粘贴到包含the的行下
[root@192 ~]# sed '/the/r /etc/hostname' b.txt
the gilrs 
localhost.localdomain
the boys  
localhost.localdomain
the gg
localhost.localdomain
the mm
localhost.localdomain
abc
12adc.
the ff
localhost.localdomain

在第三行下面新加一个内容为NEW的新行
[root@192 ~]# sed '3aNEW' b.txt
the gilrs 
the boys  
the gg
NEW
the mm
abc
12adc.
the ff

在含有the的行下新加一个内容为NEW的行
[root@192 ~]# sed '/the/aNEW' b.txt
the gilrs 
NEW
the boys  
NEW
the gg
NEW
the mm
NEW
abc
12adc.
the ff
NEW

在第三行下新加两行,内容分别为NEW1,NEW2
[root@192 ~]# sed '3aNEW1\nNEW2' b.txt
the gilrs 
the boys  
the gg
NEW1
NEW2
the mm
abc
12adc.
the ff
[root@192 ~]# 
  • 使用脚本编辑文件,使用sed脚本,将多个编辑指令存放到文件中,通过“-f”选项来调用
[root@192 ~]# sed '/the/{H};$G' b.txt            //将含有the的行复制到最后一行下面
the gilrs 
the boys  
the gg
the mm
abc
12adc.
the ff

the gilrs 
the boys  
the gg
the mm
the ff
[root@192 ~]# vim b.sh             //sed脚本

#!/bin/bash
/the/H
$G

[root@192 ~]# sed -f b.sh b.txt         //执行sed脚本的格式
the gilrs 
the boys  
the gg
the mm
abc
12adc.
the ff

the gilrs 
the boys  
the gg
the mm
the ff
[root@192 ~]# 
  • 在脚本中使用sed,操作文件
#!/bin/bash
yum install vsftpd -y
#样本配置文件路径
A="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf"
#主配置文件路径
B="/etc/vsftpd/vsftpd.conf"
#备份主配置文件
[ ! -e "$B.bak" ] && cp $B $B.bak
#修改匿名用户的权限,且把样本文件写入主配置文件
sed -e '/^anonymous_enable/s/YES/NO/g' $A > $B
#修改主配置文件中的本地用户的权限
sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enalbe/s/NO/YES/g' $B
#在主配置文件中添加监听权限
grep "listen" $B || sed -i '$alisten=YES' $B
#设置开机自启ftp服务
systemctl restart vsftpd
systemctl enable vsftpd

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值