一、15個(gè)參數(shù)
1、r 從文件讀入
[root@watchout2 ~]# cat file
1
2
3
4
5
[root@watchout2 ~]# cat newfile
a
b
c
d
e
[root@watchout2 ~]# sed '/a/r file' newfile (讀入文件file,并顯示在newfile文件中匹配行a之后)
a
1
2
3
4
5
b
c
d
e
[root@watchout2 ~]# touch /file
[root@watchout2 ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaa" > /file
[root@watchout2 ~]# sed '/a/r /file' newfile (讀入的文件在不同的路徑)
a
aaaaaaaaaaaaaaaaaaaaaaaaaa
b
c
d
e
[root@watchout2 ~]#
2、w寫(xiě)入文件
[root@watchout2 ~]# sed '/a/w bo' newfile
a
b
c
d
e
[root@watchout2 ~]# cat bo ([root@watchout2 ~]# sed -n '/a/w bobo' newfile )
a
3、a 追加命令
[root@watchout2 ~]# sed '/b/a \bobo' newfile
a
b
bobo
c
d
e
4、i 插入
[root@watchout2 ~]# sed '/a/i \bobo' newfile
bobo
a
b
c
d
e
5、n 下一個(gè)
[root@watchout2 ~]# sed -n '/a/{n;p}' newfile(打印匹配行的下一行)
b
[root@watchout2 ~]# sed '/a/{n;s/b/c/p}' newfile
a
c
c
c
d
e
6、y 變形命令
[root@watchout2 ~]# sed '1,3y/abc/ABC/' newfile
A
B
C
d
e
y命令就是將小寫(xiě)轉(zhuǎn)換成了大寫(xiě),正則表達(dá)式元字符不能使用這個(gè)命令。
7、q 退出命令
[root@watchout2 ~]# sed '3q' newfile
a
b
c
打印前三行后退出。
8、h命令 是將pattern space 模式空間(臨時(shí)緩沖區(qū))的內(nèi)容復(fù)制到holding buffer保持緩沖區(qū)
9、G命令 是將holding buffer中的內(nèi)容取得,爾后放回pattern space中,且追加到相應(yīng)行的末 尾
g命令是將holding buffer 中的內(nèi)容取得,爾后放回pattern space 中,且替換相應(yīng)的行
[root@watchout2 ~]# sed -e '/a/h' -e '/d/G' newfile
a
b
c
d
a
e
h命令會(huì)把a(bǔ)匹配行,放入保持緩沖區(qū),G命令會(huì)把保持緩沖區(qū)中的內(nèi)容放入模式空間,并追加到匹配行d的下一行。
[root@watchout2 ~]# sed -e '/a/h' -e '$G' newfile
a
b
c
d
e
a
與上相同,只是$代表最后一行。
[root@watchout2 ~]# sed -e '/a/h' -e '/b/g' newfile
a
a
c
d
e
[root@watchout2 ~]# sed -e '/a/h' -e '$g' newfile
a
b
c
d
a
以上h命令會(huì)把匹配行a,放入保持緩沖區(qū),g命令會(huì)讀取保持緩沖區(qū)的內(nèi)容,將匹配行b(第二個(gè)例子就是$最后一行)替換為a 。注:a將覆蓋匹配行b(第二個(gè)例子就是$最后一行)
10、x命令 是pattern space模式空間將被holding buffer保持緩沖區(qū)中的內(nèi)容替換
[root@watchout2 ~]# sed -e '/a/h' -e '/d/x' newfile
a
b
c
a
e
匹配行d 將被匹配行a替換。
11、-n選項(xiàng)取消sed的默認(rèn)行為,sed 默認(rèn)行為是-p ,
root:/tmp>sed '/6/p' num -p參數(shù)打印num的內(nèi)容,爾后匹配“6”在次打印6,所以6會(huì)出現(xiàn)兩次。
1
2
3
4
5
6
6
7
8
root:/tmp>sed -n '/6/p' num -n選項(xiàng)取消sed的默認(rèn)行為(-p ),所以只打印“6”
6
root:/tmp>
12、刪除:d命令
刪除第6行
root:/tmp>sed '6d' num
1
2
3
4
5
7
8
root:/tmp>
從第6行刪除到行尾
root:/tmp>sed '6,$d' num
1
2
3
4
5
root:/tmp>
d刪除最后一行
root:/tmp>sed '$d' num
1
2
3
4
5
6
7
root:/tmp>
d刪除匹配行
root:/tmp>sed '/6/d' num
1
2
3
4
5
7
8
root:/tmp>
13、替換:s命令
s表示替換,g表示作用范圍整個(gè)行,如果沒(méi)有g(shù)標(biāo)志則只有每行第一個(gè)被替換
root:/tmp>sed 's/1/8/g' num (sed默認(rèn)有-p參數(shù)打印搜索后的所有行。如g后面加p,那么匹配行就會(huì)打印兩次)
8
2
3
4
5
6
7
8
root:/tmp>
行首“1”開(kāi)頭被替換為“8”
root:/tmp>sed 's/^1/8/g' num
8
2
3
4
5
6
7
8
root:/tmp>
&符號(hào)表示替換字符串中被找到的部分,所以每個(gè)數(shù)字后面被追加.6789
root:/tmp>sed 's/[0-9]/&.6789/g' num
1.6789
2.6789
3.6789
4.6789
5.6789
6.6789
7.6789
8.6789
root:/tmp>
\(..\) 保存匹配的字符到標(biāo)簽\1中。 s/\(5\)/\1.6789/ 藍(lán)色部分保存到標(biāo)簽\1中。從表達(dá)式最左邊開(kāi)始,向右最多可以使用9個(gè)標(biāo)簽
root:/tmp>sed 's/\(5\)/\1.6789/g' num
1
2
3
4
5.6789
6
7
8
root:/tmp>sed 's/\([0-9]\)/\1.6789/g' num
1.6789
2.6789
3.6789
4.6789
5.6789
6.6789
7.6789
8.6789
root:/tmp>
s后面的字符是分隔搜索字符串和替換字符串的分隔符。默認(rèn)分隔符是斜杠,不論什么字符緊跟s命令都被認(rèn)為是新的分隔符
root:/tmp>sed 's#6#shell#g' num
1
2
3
4
5
shell
7
8
root:/tmp>
14、多點(diǎn)編輯:e命令
root:/tmp>sed -e '1,6d' -e 's/8/8.shell/g' num
7
8.shell
root:/tmp>
15、-f 引導(dǎo)sed腳本文件名
16、選定行的范圍:逗號(hào)
從第一行到第五行
root:/tmp>sed -n '1,5p' num
1
2
3
4
5
從第一行到第五行,然后在搜索
root:/tmp>sed '1,5s/3/3.linux/g' num
1
2
3.linux
4
5
6
7
8
顯示從第三行到行首為6的中間所有行
root:/tmp>sed -n '3,/^6/p' num
3
4
5
6
root:/tmp>
匹配1和5后面追加“******Total********”
root:/tmp>sed '/1/,/5/s/$/******Total********/g' num
1******Total********
2******Total********
3******Total********
4******Total********
5******Total********
6
7
8
root:/tmp>