一,概念
sed ( stream editor),流式编辑器
又称非交互式编辑器;vi / vim 为交互式编辑器
sed一条命令即时一条进程,会从硬盘读取到内存,在自己到内存空间里分为:模式空间;保持空间
所谓流式编辑器与模式空间,是说sed会一行一行在模式空间读取文本信息,处理完默认流出
# 以下对12.txt文本不做任何操作
[root@m01~]# cat 12.txt
111
222
333
[root@m01~]# sed '' 12.txt
111
222
333
# 会在文本基础上,再打印第一条
[root@m01~]# sed '1p' 12.txt
111
111
222
333
# 处理后流出
[root@m01~]# sed '1d' 12.txt
222
333
二,常用参数
-n 取消模式空间的默认输出规则
-r 支持正则及扩展,相当于grep -E;一般情况下操作sed的时候带上此选项
-i 直接修改文本
-e 支持多项编辑
1, -n 示例
[root@m01~]# cat 12.txt
111
222
333
[root@m01~]# sed '' 12.txt
111
222
333
[root@m01~]# sed -n '' 12.txt
2, -i 示例
[root@m01~]# cat 12.txt
111
222
333
[root@m01~]# sed -i '1d' 12.txt
[root@m01~]# cat 12.txt
222
333
支持多项编辑
方式一:
1) -e 用法
[root@m01~]# cat 12.txt
111
222
333
444
[root@m01~]# sed -e '3p' -e '4d' 12.txt
111
222
333
333
2)先执行3p;再3d
#注意先打印第三行,再删除第三行;输出的第三行是3p得出的结果;3d删除的是原文本中的内容
[root@m01~]# cat 12.txt
111
222
333
444
[root@m01~]# sed -e '3p' -e '3d' 12.txt
111
222
333
444
3)先执行3d;再3p
[root@m01~]# cat 12.txt
111
222
333
444
[root@m01~]# sed '3d;3p' 12.txt
111
222
444
方式二:
# 用分号隔开
[root@m01~]# cat 12.txt
111
222
333
444
[root@m01~]# sed '3d;3p' 12.txt
111
222
444
方式三:
[root@m01~]# cat 12.txt
111
222
333
444
[root@m01~]# cat sed.txt
3d
3p
[root@m01~]# sed -f sed.txt 12.txt
111
222
444
# -f从文件中读取规则
三,定位方式
1, 行号定位
[root@m01~]# cat 12.txt
111
222
333
444
555
# 删除1-3行
[root@m01~]# sed -r '1,3d' 12.txt
444
555
# 删除第一行和第三行
[root@m01~]# sed -r '1d;3d' 12.txt
222
444
555
# 从第三行删除到文本结束
[root@m01~]# sed -r '3,$d' 12.txt
111
222
2,正则定位
[root@m01~]# cat mm.txt
/root/ab/
ab
123ab
ab345
2222222a
# 删除包含ab的行
[root@m01~]# sed -r '/ab/d' mm.txt
2222222a
# 删除以ab开头的行
[root@m01~]# sed -r '/^ab/d' mm.txt
/root/ab/
123ab
2222222a
# 删除/root/ab/
方式一
[root@m01~]# sed -r '/\/root\/ab\//d' mm.txt
ab
123ab
ab345
2222222a
# 需要把要删除的内容放在/ /里,特殊符号需要转义
方式二
[root@m01~]# sed -r '/\%/root/ab//d' mm.txt
/root/ab/
ab
123ab
ab345
2222222a
# 为预防第一种方式同样符号有点乱,可以换成其他任意符号来区分,切记,第一个符号需要转义
3, 数字+正则表达式
# 从第三行删除到以a为结尾的行
[root@m01~]# cat mm.txt
/root/ab/
ab
123ab
ab345
2222222a
[root@m01~]# sed -r '3,/a$/d' mm.txt
/root/ab/
ab
四,sed 命令
1,多条命令
[root@m01~]# cat mm.txt
/root/ab/
ab
123ab
ab345
2222222a
[root@m01~]# sed '1,3p;d' mm.txt
/root/ab/
ab
123ab
# 注意这里的d没有定位范围,即指全局;输出的内容是p执行的结果
1,打印1-3行,再执行删除
方式一:
[root@m01~]# sed -r '1,3p;1,3d' mm.txt
/root/ab/
ab
123ab
ab345
2222222a
方式二:
[root@m01~]# sed -r '1,3{p;d}' mm.txt
/root/ab/
ab
123ab
ab345
2222222a
2, -s 替换
[root@m01~]# cat xx.txt
zxx123
ZXX456isbeauty
she is a fiary named zxx
zx omg
1,把第一个zxx替换成egon
[root@m01~]# sed -r 's/zxx/egon/' xx.txt
egon123
ZXX456isbeauty
she is a fiary named egon
zx omg
2,把所有zxx替换成egon
[root@m01~]# sed -r 's/zxx/egon/g' xx.txt
egon123
ZXX456isbeauty
she is a fiary named egon
zx omg
# 注意全局要加上g
3,不区分大小写把zxx替换为egon
[root@m01~]# sed -r 's/zxx/egon/gi' xx.txt
egon123
egon456isbeauty
she is a fiary named egon
zx omg
# 注意在全局g后面加上i
4,把zxx开头的改为egon
[root@m01~]# sed -r 's/^zxx/egon/g' xx.txt
egon123
ZXX456isbeauty
she is a fiary named zxx
zx omg
5,不区分大小写把1-2行的zxx替换为egon
[root@m01~]# sed -r '1,3s/zxx/egon/gi' xx.txt
egon123
egon456isbeauty
she is a fiary named egon
zx omg
6,把zxx开头中的,开头的zxx替换为egon
[root@m01~]# cat xx.txt
zxx123321zxx
ZXX456isbeauty
she is a fiary named zxx
zx omg
[root@m01~]# sed -r '/^zxx/s/^zxx/egon/gi' xx.txt
egon123321zxx
ZXX456isbeauty
she is a fiary named zxx
zx omg
7,把行尾加上egon
[root@m01~]# sed -r '/./s/$/$egon/g' xx.txt
zxx123321zxx22$egon
ZXX456isbeauty$egon
she is a fiary named zxx$egon
zx omg33$egon
8,把行首加上hahahaha
[root@m01~]# sed -r '/./s/^/hahaha&/' xx.txt
hahahazxx123321zxx22
hahahaZXX456isbeauty
hahahashe is a fiary named zxx
hahahazx omg33
[root@m01~]# sed -r '/./s/^/hahahaha/' xx.txt
hahahahazxx123321zxx22
hahahahaZXX456isbeauty
hahahahashe is a fiary named zxx
hahahahazx omg33
9,在ty结尾的行增加555
[root@m01~]# sed -r 's/ty$/&555/' xx.txt
zxx123321zxx22
ZXX456isbeauty555
she is a fiary named zxx
zx omg33
#注意切记加上&符号,否则会被替换掉
[root@m01~]# sed -r 's/ty$/555/' xx.txt
zxx123321zxx22
ZXX456isbeau555
she is a fiary named zxx
zx omg33
10,在数字结尾行加上ing
[root@m01~]# cat xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
[root@m01~]# sed -r 's/[0-9]$/&ing/' xx.txt
zxx123321zxx22ing
ZXX456isbeauty
she is a fiary named zxx
zx omg33ing
11,在以Z开头的前面加上qqq
[root@m01~]# sed -r 's/^Z/qqq&/' xx.txt
zxx123321zxx22
qqqZXX456isbeauty
she is a fiary named zxx
zx omg33
12,把字符和数字调换位置
[root@m01~]# cat xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
[root@m01~]# sed -r 's/([a-zA-Z]+)([0-9]+)/\2\1/' xx.txt
123321zxxzxx22
456ZXXisbeauty
she is a fiary named zxx
zx 33omg
# sed -r 's/([a-zA-Z]+)([0-9]+)/\2\1/中
+代表一次或无穷次
全局替换
[root@m01~]# sed -r 's/([a-zA-Z]+)([0-9]+)/\2\1/g' xx.txt
123321zxx22zxx
456ZXXisbeauty
she is a fiary named zxx
zx 33omg
13,中扩号把多条命令集中起来
[root@m01~]# cat xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
[root@m01~]# sed -r '1,3{p;s/zxx/SSS/g}' xx.txt
zxx123321zxx22
SSS123321SSS22
ZXX456isbeauty
ZXX456isbeauty
she is a fiary named zxx
she is a fiary named SSS
zx omg33
===========补充================
y 一一对应替换
# 把字母zx都替换成ab
[root@m01~]# sed -r 'y/zx/ab/' xx.txt
abb123321abb22
ZXX456isbeauty
she is a fiary named abb
ab omg33
q 停止
# zx替换成ab, 只替换1-3行的就结束
[root@m01~]# sed -r '1,3s/zx/ab/;3q' xx.txt
abx123321zxx22
ZXX456isbeauty
she is a fiary named abx
也可以
[root@m01~]# sed -r '1,3{s/zx/ab/};3q' xx.txt
abx123321zxx22
ZXX456isbeauty
she is a fiary named abx
3, 取反, 插入内容
[root@m01~]# cat xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
1,取反
# 删除1-3行
[root@m01~]# sed -r '1,3d' xx.txt
zx omg33
# 删除1-3行外的
[root@m01~]# sed -r '1,3!d' xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
2,-a 在某行后面插入内容
# 第二行后面插入bbbbb
[root@m01~]# sed -r '2a bbbbb' xx.txt
zxx123321zxx22
ZXX456isbeauty
bbbbb
she is a fiary named zxx
zx omg33
3, -i 在某行前面插入内容
# 在第一行前插入aaaaa
[root@m01~]# sed -r '1i aaaaa' xx.txt
aaaaa
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
4,-c 直接把某行替换
# 把第三行替换成666
[root@m01~]# sed -r '3c 666' xx.txt
zxx123321zxx22
ZXX456isbeauty
666
zx omg33
4, 文本插入及定向
[root@m01~]# cat xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
1,把zz.txt 插入到xx.txt的第一行下面
[root@m01~]# cat zz.txt
666
777
[root@m01~]# sed -r '1r zz.txt' xx.txt
zxx123321zxx22
666
777
ZXX456isbeauty
she is a fiary named zxx
zx omg33
2, 把1-3行的内容写到另外一个文件里
[root@m01~]# sed -r '1,3w /tmp/pp.txt' xx.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
zx omg33
[root@m01~]# cat /tmp/pp.txt
zxx123321zxx22
ZXX456isbeauty
she is a fiary named zxx
补充
把文件的顺序颠倒
[root@m01~]# cat zz.txt
666
777
888
[root@m01~]# tac zz.txt
888
777
666
运用sed的模式空间和保持空间也可以做到
h ------> 覆盖保持空间
H ------>> 追加到保持空间
g <------ 回到模式空间
G <<------ 追加到模式空间
x 两个空间互换
[root@m01~]# sed -r '1h;1d;2G;2h;2d;3G' zz.txt
888
777
666
网友评论