shell三剑客之sed
一、sed 编辑器的具体功能
sed处理缓冲区的内容,将处理结果打印在屏幕。文件内容本身不发生改变。(缓冲区又叫做虚拟空间) 。
二、sed基本操作
sed命令的格式: sed 选项 script filename
1.如何将文件或是字符串中的文本替换掉
对命令行使用sed
[root@slave_server ~]# echo "This is a dog" | sed 's/dog/cat/'
This is a cat
对整个文件使用sed命令
[root@slave_server ~]# sed 's/dog/cat/' file1
this is a cat
[root@slave_server ~]#
2.同时修改替换多个字段
-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
3.若想同时修改多行的多处位置
注:配合 -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
三、4个替换标记
1. ‘2’ 替换同一行中第二次出现的需替换字段
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. ```
2. ‘g’ 匹配文件中所有需要替换的字段(该替换标记适用于修改文本中需要替换的所有字符)
[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.
3. ‘p’ 该标记会打印命令中指定替换字段相应的行
注:会标记所替换字段行,但是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.
4. -n选项禁止sed编辑器输出。
注:该标记通常与p一起使用,两者配合将输出被替换字段的所在行。如列子所示会只打印修改过的行
[root@slave_server ~]# sed -n 's/leave/haha/p' file3
#只输出修改行内容
we need draw attention to partenal haha we are feet.
四、sed删除文本流中的特定行
1.与指定地址一同使用,可以从数据流中删除特定的文本行。
[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.
2.删除文本的具体行数
[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 ~]#
3.通过查找字段相应行去删除
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插入和附加文本
1.在命令行中实现插入和附加文本
命令格式: 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
2.为数据流文本内部插入或附加数据(定位想要插入行的某个字段,用此字段作为标记从而去寻址找到所标记行)
注意:插入和附加文本所能做的是将行插入文本流某一行的前面或后面,而不是插入某字段的前面或后面
[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