命令格式
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)數(shù)字 替換第幾處
(2)g 替換所有
(3)p 原來行打印出來
(4)w file 將替換的結(jié)果寫入文件
(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.文本模式過濾器
根據(jù)文本模式進行篩選
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