sed处理缓冲区的内容,将处理结果打印在屏幕。文件内容本身不发生改变。(缓冲区又叫做虚拟空间) 。
sed命令的格式: sed 选项 script filename
[root@slave_server ~]# echo "This is a dog" | sed 's/dog/cat/'
This is a cat
[root@slave_server ~]# sed 's/dog/cat/' file1
this is a cat
[root@slave_server ~]#
-e 可以同时修改替换多个字段
[root@slave_server ~]# cat zhousi
"lll
b
s
dlkjsn fdkl kjolm
lod
[root@slave_server ~]# sed -e 's/b/hello/; s/s/world/' zhousi
注:配合 -f 一起使用,-f 代表作用文件
sed -f 命令文件 作用文件
将要修改的多行放入一个文件中(file2,将要执行的替换命令放入另一个文件中(file2.sed)
注:此处的.sed文件是为了区分其他文件
cat file2 #要修改的多行文本
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
cat file1.sed #要执行的替换命令
s/this/that/
s/cat/dog/
[root@slave_server ~]# sed -f file1.sed file2
that is a dog
that is a dog
that is a dog
that is a dog
that is a dog
that is a dog
cat file3 #查看文本文件
we need draw attention to partenal leave we are feet.
we need take seriously everything we are eyes.
sed 's/we/they/2' file3
#将每行第二次出现的we替换为they(若想替换某行第三次出现的字段,可以将/2换为/3)
we need draw attention to partenal leave they are feet.
we need take seriously everything they are eyes.
若不加参数,则默认只匹配到第一个字段
file3.sed file3 they need draw attention to partenal leave we are feet. they need take seriously everything we are eyes. ```
[root@slave_server ~]# sed 's/we/they/g' file3 #匹配所有we进行替换
they need draw attention to partenal leave they are feet.
they need take seriously everything they are eyes.
注:会标记所替换字段行,但是sed编辑器同时也会输出替换后整个文本的内容
[root@slave_server ~]# sed 's/leave/haha/p' file3
#标记所替换字段行,并将标记行单独输出
we need draw attention to partenal haha we are feet.
# sed编辑器输出替换字段后的整个文本内容
we need draw attention to partenal haha we are feet.
we need take seriously everything we are eyes.
注:该标记通常与p一起使用,两者配合将输出被替换字段的所在行。如列子所示会只打印修改过的行
[root@slave_server ~]# sed -n 's/leave/haha/p' file3
#只输出修改行内容
we need draw attention to partenal haha we are feet.
[root@slave_server ~]# cat file5 #创建文件5
this is a num1.
this is a num2.
this is a num3.
this is a num4.
this is a num5.
[root@slave_server ~]# sed '3d' file5 #删除缓冲区的第二行,不会真正作用于文本
this is a num1.
this is a num2.
this is a num4.
this is a num5.
[root@slave_server ~]# cat file5 #再次查看文本会发现并未改变文本内容
this is a num1.
this is a num2.
this is a num3.
this is a num4.
this is a num5.
[root@slave_server ~]# cat file5 #再次查看文本会发现并未改变文本内容
this is a num1.
this is a num2.
this is a num3.
this is a num4.
this is a num5.
[root@slave_server ~]# sed '2,3d' file5 #删除缓冲区的二三行
this is a num1.
this is a num4.
this is a num5.
[root@slave_server ~]# sed '2,$d' file5 #删除缓冲区的第二行直至结束
this is a num1.
[root@slave_server ~]#
sed编辑器会删除带着指定标记的相应行
也可以通过模式匹配特性去删除相应行
[root@slave_server ~]# sed '/1/d' file5 #匹配1所在的行进行缓冲区删除
this is a num2.
this is a num3.
this is a num4.
this is a num5.
命令格式: sed ‘[address]command\newline’
[root@slave_server ~]# echo "these is 2" | sed 'i\these is 1'
#sed 'i\these 1' 代表将echo的文本 插入‘these iS 1’之后
these is 1
these is 2
[root@slave_server ~]# echo "these is 2" | sed 'a\these is 1'
#sed 'a\these iS 1' 代表将echo的文本 添加到 ‘these is 1’之前
these is 2
these is 1
注意:插入和附加文本所能做的是将行插入文本流某一行的前面或后面,而不是插入某字段的前面或后面
[root@slave_server ~]# sed '3i\this is a 3' file5
# 将'this is a 3'插入file5文件的第三行前
this is a num1.
this is a num2.
this is a 3
this is a num3.
this is a num4.
this is a num5.
[root@slave_server ~]# sed '3a\
#插入file5文件的第三行后
this is a 3' file5
this is a num1.
this is a num2.
this is a num3.
this is a 3
this is a num4.
this is a num5.
$代表文本的最后一行数据
[root@slave_server ~]# sed '$a\ #将字符串插入file5文件的最后一行
this is the end' file5
this is a num1.
this is a num2.
this is a num3.
this is a num4.
this is a num5.
this is the end