linux之sed的简单使用

sed编辑器被称作非交互式的流编辑器(stream editor)。

  • 在默认情况下,只对流经命令的文本进行修改,并将修改后的结果打印到标准输出中(也就是屏幕),并不改变文件本身。
  • 以行为单位进行处理,每处理完一行立即打印,并随即处理下一行
  • 可进行的操作包括删除,查找替换,添加,插入等

sed命令的基本格式如下:

sed [option] [command] 文件名

该命令常用的选项及含义:

选项含义
-e 脚本命令该选项会将其后跟的脚本命令添加到已有的命令中
-f 脚本命令文件该选项会将其后文件中的脚本命令添加到已有的命令中
-n默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出
-i此选项会直接修改源文件,要慎用
-V版本信息
-h帮助信息

1.s替换命令
s命令(substitute)来在行中替换文本。命令格式如下:

s/pattern/replacement/flags

pattern 指的是需要替换的内容,replacement 指的是要替换的新内容。flags为替换标记(substitution flag)。常用的flags如下:

flags含义
n1-512之间的数字,表示指定要替换的字符串出现第几次时才替换
g对文件中所有匹配到的内容进行替换
p会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。
w file将替换的结果写到文件中。

默认情况下它只替换每行中出现的第一处。例如:

[root@node1 ~]# cat sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

this is line 4,this is Third line
this is line 5,this is Fifth line
[root@node1 ~]# sed 's/line/LINE/' sed.txt 
this is LINE 1,this is First line
this is LINE 2,the Second line,Empty line followed

this is LINE 4,this is Third line
this is LINE 5,this is Fifth line

例如指定第二次出现才替换:

[root@node1 ~]# sed 's/line/LINE/2' sed.txt 
this is line 1,this is First LINE
this is line 2,the Second LINE,Empty line followed

this is line 4,this is Third LINE
this is line 5,this is Fifth LINE

例如所有出现line的都替换:

[root@node1 ~]# sed 's/line/LINE/g' sed.txt 
this is LINE 1,this is First LINE
this is LINE 2,the Second LINE,Empty LINE followed

this is LINE 4,this is Third LINE
this is LINE 5,this is Fifth LINE

例如sed命令的输出输入到newsed.txt:

[root@node1 ~]# sed 's/line/LINE/w newsed.txt' sed.txt  
this is LINE 1,this is First line
this is LINE 2,the Second line,Empty line followed

this is LINE 4,this is Third line
this is LINE 5,this is Fifth line
[root@node1 ~]# cat newsed.txt 
this is LINE 1,this is First line
this is LINE 2,the Second line,Empty line followed
this is LINE 4,this is Third line
this is LINE 5,this is Fifth line

2.d删除命令
d命令用来做删除操作。使用该命令时要特别小心,如果忘记指定具体行的话,文件中的所有内容都会被删除。例如:

[root@node1 ~]# sed 'd' sed.txt 
[root@node1 ~]# 

删除第一行:

[root@node1 ~]# sed '1d' sed.txt 
this is line 2,the Second line,Empty line followed

this is line 4,this is Third line
this is line 5,this is Fifth line

删除第2,3行:

[root@node1 ~]# sed '2,3d' sed.txt 
this is line 1,this is First line
this is line 4,this is Third line
this is line 5,this is Fifth line

从第2行删除到最后一行:

[root@node1 ~]# sed '2,$d' sed.txt 
this is line 1,this is First line

删除空行:

[root@node1 ~]# sed '/^$/d' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
this is line 4,this is Third line
this is line 5,this is Fifth line

3.a 和 i 脚本命令
a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行。命令格式如下:

[address]a(或 i)\新文本内容

address 表示指定要操作的具体行。例如:在第3行前插入一句新的内容:

[root@node1 ~]# sed '3i\hello world' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
hello world

this is line 4,this is Third line
this is line 5,this is Fifth line

例如:在第3行后插入一句新的内容:

[root@node1 ~]# sed '3a\hello world' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

hello world
this is line 4,this is Third line
this is line 5,this is Fifth line

4.c命令
c 命令表示将指定行中的所有内容,替换成该选项后面的字符串。该命令的基本格式为:

[address]c\用于替换的新文本

例如将第3行替换成hello world:

[root@node1 ~]# sed '3c\hello world' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed
hello world
this is line 4,this is Third line
this is line 5,this is Fifth line

5.y转换命令
y 转换命令是唯一可以处理单个字符的 sed 脚本命令,其基本格式如下:

[address]y/inchars/outchars/

转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符…这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。
例如将1234换成abcd:

[root@node1 ~]# sed 'y/1234/abcd/' sed.txt 
this is line a,this is First line
this is line b,the Second line,Empty line followed

this is line d,this is Third line
this is line 5,this is Fifth line

当 inchars 和 outchars长度不一致时,报错如下:

[root@node1~]# sed 'y/1234/acd/' sed.txt 
sed: -e expression #1, char 11: strings for `y' command are different lengths

6.r读取命令
r命令允许将一个独立文件中的数据插入到数据流中。命令格式如下:

[address]r filename

例如将文件r.txt插在sed.txt的第三行后

[root@ecs-24a1 ~]# cat r.txt 
my name is bob
[root@ecs-24a1 ~]# sed '3r r.txt' sed.txt 
this is line 1,this is First line
this is line 2,the Second line,Empty line followed

my name is bob
this is line 4,this is Third line
this is line 5,this is Fifth line
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远不要矫情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值