命令格式
sed options script file
-e script 在处理输入时,将script中指定的命令添加到运行的命令中。
-f file 在处理输入时,将file中指定的命令添加到运行的命令行的末尾
-n 不要为每个命令生成输出,等待print命令来输出
1.多个命令
master@master:~/shell$ sed 's/brown/green/; s/dog/cat/' data1
the quik green green fox jumps over the lazy cat
the quik green green fox jumps over the lazy cat
the quik green green fox jumps over the lazy cat
the quik green green fox jumps over the lazy cat
the quik green green fox jumps over the lazy cat
master@master:~/shell$ sed -e '
> s/brown/green/
> s/fox/dog/
> s/bug/ddd/' data1
the quik green green dog jumps over the lazy dog
the quik green green dog jumps over the lazy dog
the quik green green dog jumps over the lazy dog
the quik green green dog jumps over the lazy dog
the quik green green dog jumps over the lazy dog
2.从文件中读取命令
master@master:~/shell$ cat script1
s/brown/green/
s/god/ddd/
s/green/yeyeew/
master@master:~/shell$ sed -f script1 data1
the quik yeyeew green fox jumps over the lazy dog
the quik yeyeew green fox jumps over the lazy dog
the quik yeyeew green fox jumps over the lazy dog
the quik yeyeew green fox jumps over the lazy dog
the quik yeyeew green fo
注意上面的替换只能替换每一行中的第一个单词
3.更多的替换标记
(1)数字 替换第几处
(2)g 替换所有
(3)p 原来行打印出来
(4)w file 将替换的结果写入文件
(1)只替换第一出,且原始文件不变
master@master:~/shell$ cat data1
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
master@master:~/shell$ sed 's/the/the_/' data1
the_ quik brown green fox jumps over the lazy dog
the_ quik brown green fox jumps over the lazy dog
master@master:~/shell$ cat data1
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
(2)p
注意,p回输出匹配的行和修改过的行
master@master:~/shell$ sed 's/test/trial/p' data1
this is a trial line
this is a trial line
this is a different line
master@master:~/shell$ cat data1
this is a test line
this is a different line
可以用-n 来只输出修改过的行
master@master:~/shell$ sed -n 's/test/trial/p' data1
this is a trial line
(3)w 输出到指定的文件
master@master:~/shell$ sed 's/test/trial/w testfile' data1
this is a trial line
this is a different line
master@master:~/shell$ cat testfile
this is a trial line
(3)特殊字符 /
master@master:~/shell$ sed 's//bin/bash//bin/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/csh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
master@master:~/shell$ more /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
也可以换一个分割符号
master@master:~/shell$ sed 's!/bin/bash!/bin/csh!' /etc/passwd
####4.使用寻址
address {
command1
command2
}
master@master:~/shell$ cat data1
this is a test line
this is a different line
master@master:~/shell$ sed '2s/this/that/' data1
this is a test line
that is a different line
master@master:~/shell$
master@master:~/shell$ sed '2,3s/this/that/' data1
master@master:~/shell$ sed '2,$s/this/that/' data1
####5.文本模式过滤器
根据文本模式进行筛选
master@master:~/shell$ grep test data1
this is a test line
master@master:~/shell$ sed '/test/s/test/mytest/' data1
this is a mytest line
this is a different line
####6.组合命令
master@master:~/shell$ sed '1{
s/test/mytest/
}' data1
this is a mytest line
this is a different line
master@master:~/shell$
####7.删除行
master@master:~/shell$ cat data
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
master@master:~/shell$ sed 'd' data
删除第二行
master@master:~/shell$ sed '2d' data
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
master@master:~/shell$ sed '2,4d' data
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
master@master:~/shell$ sed '2,$d' data
the quik brown green fox jumps over the lazy dog
文本匹配删除,如果the3没有,则会从the2一直删除到最后
'/the2/,/the3/d' 会删除之间的
master@master:~/shell$ sed '/over/d' data
master@master:~/shell$ cat data
the quik brown green fox jumps over the lazy dog
the2 quik brown green fox jumps over the lazy dog
the1 quik brown green fox jumps over the lazy dog
the3 quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
master@master:~/shell$ sed '/the2/,/the3/d' data
the quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
####8.删除和附加文本
插入命令i 会在指定的行前增加一个新行
追加命令a 会在指定的行后增加一个新行
master@master:~/shell$ echo "test 1" | sed 'i\Test2'
Test2
test 1
master@master:~/shell$ echo "test 1" | sed 'a\Test2'
test 1
Test2
指定行
master@master:~/shell$ sed '3i\this li insert line' data
the quik brown green fox jumps over the lazy dog
the2 quik brown green fox jumps over the lazy dog
this li insert line
the1 quik brown green fox jumps over the lazy dog
the3 quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
插入多行文本,必须样反斜杠
master@master:~/shell$ sed '3i\thisis line111\
thisli line2222' data
the quik brown green fox jumps over the lazy dog
the2 quik brown green fox jumps over the lazy dog
thisis line111
thisli line2222
the1 quik brown green fox jumps over the lazy dog
the3 quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog
####9.修改行
master@master:~/shell$ sed '3c
this is test' data
the quik brown green fox jumps over the lazy dog
the2 quik brown green fox jumps over the lazy dog
this is test
the3 quik brown green fox jumps over the lazy dog
the quik brown green fox jumps over the lazy dog