$ cat > test << "EOF" This is my cat my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam EOF
替换
1 2 3 4 5 6 7 8 9
$ sed 's/This/That/g'test That is my cat my cat's name is betty That is my dog my dog's name is frank That is my fish my fish's name is george That is my goat my goat's name is adam
我们将每行输出的 This 替换为 That,sed 默认输出是将修改的文件输出到屏幕上,如果想要修改原文件,则需要加上 -i 参数
# This is my cat # my cat's name is betty # This is my dog # my dog's name is frank # This is my fish # my fish's name is george # This is my goat # my goat's name is adam
行尾添加字符
1 2 3 4 5 6 7 8 9 10
$ sed 's/$/ ----/g'test
This is my cat ---- my cat's name is betty ---- This is my dog ---- my dog's name is frank ---- This is my fish ---- my fish's name is george ---- This is my goat ---- my goat's name is adam ----
替换指定行
1 2 3 4 5 6 7 8 9 10
$ sed '3s/my/your/g'test
This is my cat my cat's name is betty This is your dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
替换范围行
1 2 3 4 5 6 7 8 9 10
$ sed '3,5s/my/your/g'test
This is my cat my cat's name is betty This is your dog your dog's name is frank This is your fish my fish's name is george This is my goat my goat's name is adam
替换每行的第 n 个匹配项
1 2 3 4 5 6 7 8 9 10
$ sed 's/s/S/2'test
This iS my cat my cat's name iS betty This iS my dog my dog's name iS frank This iS my fish my fish'S name is george This iS my goat my goat's name iS adam
替换多个匹配规则
1 2 3 4 5 6 7 8 9 10
$ sed 's/my/your/g; s/is/Is/g'test
ThIs Is your cat your cat's name Is betty ThIs Is your dog your dog's name Is frank ThIs Is your fIsh your fIsh's name Is george ThIs Is your goat your goat's name Is adam
或者使用参数 -e
1
$ sed -e 's/my/your/g' -e 's/is/Is/g'test
匹配项作为变量
使用 & 作为匹配项的变量,可以在变量旁边加上符号等
1 2 3 4 5 6 7 8 9 10
$ sed 's/my/[&]/g'test
This is [my] cat [my] cat's name is betty This is [my] dog [my] dog's name is frank This is [my] fish [my] fish's name is george This is [my] goat [my] goat's name is adam
奇数行替换
1 2 3 4 5 6 7 8 9 10
$ sed 'N;s/my/your/'test
This is your cat my cat's name is betty This is your dog my dog's name is frank This is your fish my fish's name is george This is your goat my goat's name is adam
1 2 3 4 5
$ sed 'N;s/\n/,/'test This is my cat, my cat's name is betty This is my dog, my dog's name is frank This is my fish, my fish's name is george This is my goat, my goat's name is adam
i 插入
i 可以在某一行前面插入数据
在第二行前加入
1 2 3 4 5 6 7 8 9 10 11
$ sed '2 i ---------------'test
This is my cat --------------- my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
在第一和第二行插入
1 2 3 4 5 6 7 8 9 10 11
$ sed '1,2 i ---------------'test --------------- This is my cat --------------- my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
匹配指定字符行插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ sed '/my/ i -------'test
------- This is my cat ------- my cat's name is betty ------- This is my dog ------- my dog's name is frank ------- This is my fish ------- my fish's name is george ------- This is my goat ------- my goat's name is adam
a 新增
a 在某一行后面新增数据
1 2 3 4 5 6 7 8 9 10 11
$ sed '2 a ---------------'test
This is my cat my cat's name is betty --------------- This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
c 替换
c 的替换是正行的替换
1 2 3 4 5 6 7 8 9 10
$ sed '2 c My name is wxnacy'test
This is my cat My name is wxnacy This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
在运行该命令时,结果可能会不明显,因为 sed 默认是将最终的文本全部输出,我们也可以加上 -n 参数只输出修改行
1 2
$ sed -n '2 c My name is wxnacy'test My name is wxnacy
d 删除
d 可以删除指定或匹配行
1 2 3 4 5 6 7 8 9
$ sed '1 d'test
my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat my goat's name is adam
1 2 3 4 5 6 7
$ sed '/dog/ d'test This is my cat my cat's name is betty This is my fish my fish's name is george This is my goat my goat's name is adam
1 2 3
$ sed '3,$ d'test This is my cat my cat's name is betty
p 打印
p 可以将匹配行打印出来,可以理解为 grep 命令
1 2
$ sed -n '2 p'test my cat's name is betty
1 2 3 4 5 6 7
$ sed -n '2,/goat/ p'test my cat's name is betty This is my dog my dog's name is frank This is my fish my fish's name is george This is my goat