Shell -----sed

7 篇文章 0 订阅

一.什么是sed?

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

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

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

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

3.显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。

二.sed命令常见用法

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

有如下选项:
​ 1.-e或-expression=:表示用指定命令或者脚本来处理输入的文本文件
​ 2.-f或-file=:表示用指定的脚本文件来处理输入的文本文件
​ 3.-h或-help:显示帮助
​ 4.-n、–quite或silent:表示仅显示处理后的结果
​ 5.-i :直接编辑文本文件
​ 操作,用于指定具体的动作行为,通常情况下采用”[n1[,n2]]“操作参数的格式。具体如下:
​ 6.a:增加,在当前行下增加一行指定内容
​ 7.c:替换,将选定行替换为指定内容
​ 8.d:删除,删除选定的行
​ 9.i:插入,在选定行上面插入一行指定内容
​ 10.p:打印,如果同时指定行,表达打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。通常与-n一起使用
​ 11.s:替换,替换指定字符
​ 12.y:字符转换

三.具体用法

  1. 输出:(p)
    1.输出符合条件的文件,-n表示输出处理后的结果,p表示正常输出
[root@localhost ~]# sed -n 'p' 123.txt
the a
the b
the c
the d
the e
the f
the g
the h
cold
do
wod
wood
111
222

2.输出第3行内容

[root@localhost ~]# sed -n '3p' 123.txt
the c

3.输出第3行到第6行内容

[root@localhost ~]# sed -n '3,6p' 123.txt
the c
the d
the e
the f

4.输出奇数行和偶数行(相对的奇和偶)

[root@localhost ~]# sed -n 'p;n' 123.txt 
the a
the c
the e
the g
cold
wod
111
333
444
otten
oo
xyz
Axyz
AxyzxyzC

[root@localhost ~]# sed -n 'n;p' 123.txt 
the b
the d
the f
the h
do
wood
222

555
on
ooo
xyzxyz
AxyzC

5.输出从2到5行的奇数行

[root@localhost ~]# sed -n '2,5{p;n}' 123.txt
the b
the d

6.输出从第3行到最后一行的偶数行

[root@localhost ~]# sed -n '3 ,${n;p}' 123.txt
the d
the f
the h
do
wood
222

555
on
ooo
xyzxyz
AxyzC

7.输出包含字符o的行

[root@localhost ~]# sed -n '/o/p' 123.txt
cold
do
wod
wood
otten
on
oo
ooo

8.输出从第4行开始第一个包含字符o的行,包括第四行

[root@localhost ~]# sed -n '4,/o/p' 123.txt
the d
the e
the f
the g
the h
cold

9.输出包含字符o的行的行号

[root@localhost ~]# sed -n '/o/=' 123.txt
9
10
11
12
19
20
21
22

10.输出以字符o和以字符o结尾的行

[root@localhost ~]# sed -n '/^o/p' 123.txt
otten
on
oo
ooo
[root@localhost ~]# sed -n '/o$/p' 123.txt
do
oo
ooo

11.输出包含单词the的行

[root@localhost ~]# sed -n '/\<the\>/p' 123.txt
the a
the b
the c
the d
the e
the f
the g
the h
  1. 删除符合条件的文本(d):
    1.删除第 3 行
[root@localhost ~]# nl 123.txt | sed '3d'	

2.删除第 3~5 行

[root@localhost ~]# nl 123.txt | sed '3,5d'

3.删除第 3 行

[root@localhost ~]# nl 123.txt | sed '3d'

4.//删除包含cross 的行,原本的第 8 行被删除
//删除不包含cross 的行,用!符号表示取反操作,如’/cross/!d’

[root@localhost ~]# nl 123.txt |sed '/cross/d'       

5.删除以小写字母开头的行

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

6.删除以"."结尾的行

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

7.删除所有空行

[root@localhost ~]# sed '/^$/d' 123.txt      
  1. 替换符合条件的文本,s(字符串替换)、c(整行/整块替换)、y(字符转换)
sed 's/the/THE/' test.txt       #将每行中的第一个the 替换为 THE

sed 's/l/L/2' test.txt          #将每行中的第 2 个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

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

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 表示换行

5) 使用脚本编辑文件
使用脚本编辑文件,使用sed脚本,将多个编辑指令存放到文件中,通过“-f”选项来调用。

sed '1,5{H;d};17G‘ test.txt     #将第 1~5 行内容转移至第 17 行后

[root@localhost ~]# sed -f opt.list test.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值