一.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
输出第3行内容
[root@localhost ~]# sed -n '3p' b.txt
3:the gg
输出第3行到第6行内容
[root@localhost ~]# sed -n '3,6p' b.txt
3:the gg
4:the mm
5:the ff
输出奇数行和偶数行(相对的奇和偶)
[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
输出从2到5行的奇数行
[root@localhost ~]# sed -n '2,5{p;n}' b.txt
2:the boys
4:the mm
输出从第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
输出以字符o和以字符o结尾的行
[root@localhost ~]# sed -n '/^o/p' a.txt
often
ontime
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
删除空行
[root@localhost ~]# nl b.txt
1 1:the gilrs
2 2:the boys
3 3:the gg
4 4:the mm
替换符合条件的文本,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.Ab.
cold.
777
wod
bet
often
ifconfig
islase
ontime
ontimo
beat
AyzC
AyzyzC
Ayzzy0927C
AyzyozyzC
txt
在每一行的行首加上#符号
[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
#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/w c.txt' b.txt
the gilrs
the boys
the gg
the mm
abc
12adc.
the ff
[root@192 ~]# ls
将/etc/hostname内的内容粘贴到包含the的行下