shell工具-sed

sed

sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断反复,直到文件末尾。**文件内容并没有改变**,除非你你使用重定向存储输出

基本用法

sed [选项参数] ‘command’ filename

选项参数说明

-e  直接在指令列模式上进行sed的动作编辑

 命令功能描述

命令功能描述
a 新增,a的后面可以接字串,在下一行出现
d 删除
s 查找并替换

案例实操

数据准备

[root@slave2 testshell]# vim sed.txt
dong shen
guan zhen
wo  wo
lai  lai

le  le

将“mei nv”这个单词插入到sed.txt第二行下,打印。(注意原文件并没有改变)

[root@slave2 testshell]# sed "2a mei nv" sed.txt
dong shen
guan zhen
mei nv
wo  wo
lai  lai

le  le

删除sed.txt文件所包含wo的行

[root@slave2 testshell]# sed "/wo/d" sed.txt
dong shen
guan zhen
lai  lai

le  le

将sed.txt文件中wo替换为ni

[root@slave2 testshell]# sed "s/wo/ni/" sed.txt 
dong shen
guan zhen
ni  wo
lai  lai

le  le
[root@slave2 testshell]# sed "s/wo/ni/g" sed.txt 
dong shen
guan zhen
ni  ni
lai  lai

le  le

将sed.txt文件中的第二行删除并将wo替换为ni

[root@slave2 testshell]# sed -e "2d" -e "s/wo/ni/g" sed.txt 
dong shen
ni  ni
lai  lai

le  le
原文地址:https://www.cnblogs.com/zxbdboke/p/10420527.html