1、替换
$ cat data1
this test inside testthis test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog
替换遇到的第一个匹配字符(默认)
this hello inside test
this hello inside test
this si inside hello
this si inside hello
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog
-n 禁止打印,命令p是打印修改后的行
this hello inside test
this hello inside test
this si inside hello
this si inside hello
替换行内指定的匹配字符 2
$ sed -n 's/test/hello/2p' data1
this test inside hello
this test inside hello
替换行内所有的匹配字符 g
this hello inside hello
this hello inside hello
this si inside hello
this si inside hello
$ cat /etc/passwd
zhongxuan:x:1001:1001:,,,:/home/zhongxuan:/bin/bash
用其他符号指定字符分节符 用 !作为分解符
zhongxuan:x:1001:1001:,,,:/yangzhongxuan:/bin/bash
$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog
文本模式匹配替换
The quick green elephant jumps over the red dog
$ sed -n '/fox/s/green/red/p' data1
The quick red elephant jumps over the fox dog
$ cat data1
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog
命令行组合
s/test/test_ch/
s/inside/outside/
}' data1
this test inside test
this test_ch outside test
$ cat data6
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
2、插入
quote> hello this is insert line 1' data6
hello this is insert line 1
This is line number 1.
hello this is insert line 1
This is line number 2.
hello this is insert line 1
This is line number 3.
hello this is insert line 1
This is line number 4.
hello this is insert line 1
This is line number 5.
$ sed '3i\
quote> you are ok insert the line 2.' data6
This is line number 1.
This is line number 2.
you are ok insert the line 2.
This is line number 3.
This is line number 4.
This is line number 5.
$ sed '3a\
you are ok insert the line 2.' data6
This is line number 1.
This is line number 2.
This is line number 3.
you are ok insert the line 2.
This is line number 4.
This is line number 5.
$ sed '3i\
quote> you can insert more line.\
quote> you insert secnd line.\
quote> you insert third line.' data6
This is line number 1.
This is line number 2.
you can insert more line.
you insert secnd line.
you insert third line.
This is line number 3.
This is line number 4.
This is line number 5.
3、修改
quote> you can change line with the sed command c' data6
This is line number 1.
This is line number 2.
you can change line with the sed command c
This is line number 4.
This is line number 5.
$ sed '/number 3/c\
quote> change line with txt mode' data6
This is line number 1.
This is line number 2.
change line with txt mode
This is line number 4.
This is line number 5.
$ sed '/number/c\
quote> chang lines that match the parrten' data6
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
chang lines that match the parrten
$ sed '2,3c\
quote> this line chang the 2 to 3 line of text.' data6
This is line number 1.
this line chang the 2 to 3 line of text.
This is line number 4.
This is line number 5.
4、写入文件
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
$ cat tsetw
This is line number 2.
This is line number 3.
5、读入文件
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog
指定行地址读入
this test inside test
this test inside test
this si inside test
This is line number 2.
This is line number 3.
this si inside test
The quick green elephant jumps over the dog dog
The quick green elephant jumps over the fox dog
指定匹配模式的行地址读入
this test inside test
this test inside test
this si inside test
this si inside test
The quick green elephant jumps over the dog dog
This is line number 2.
This is line number 3.
The quick green elephant jumps over the fox dog
This is line number 2.
This is line number 3.