一、15個參數
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寫入文件
[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 下一個
[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命令就是將小寫轉換成了大寫,正則表達式元字符不能使用這個命令。
7、q 退出命令
[root@watchout2 ~]# sed '3q' newfile
a
b
c
打印前三行后退出。
8、h命令 是將pattern space 模式空間(臨時緩沖區)的內容復制到holding buffer保持緩沖區
9、G命令 是將holding buffer中的內容取得,爾后放回pattern space中,且追加到相應行的末 尾
g命令是將holding buffer 中的內容取得,爾后放回pattern space 中,且替換相應的行
[root@watchout2 ~]# sed -e '/a/h' -e '/d/G' newfile
a
b
c
d
a
e
h命令會把a匹配行,放入保持緩沖區,G命令會把保持緩沖區中的內容放入模式空間,并追加到匹配行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命令會把匹配行a,放入保持緩沖區,g命令會讀取保持緩沖區的內容,將匹配行b(第二個例子就是$最后一行)替換為a 。注:a將覆蓋匹配行b(第二個例子就是$最后一行)
10、x命令 是pattern space模式空間將被holding buffer保持緩沖區中的內容替換
[root@watchout2 ~]# sed -e '/a/h' -e '/d/x' newfile
a
b
c
a
e
匹配行d 將被匹配行a替換。
11、-n選項取消sed的默認行為,sed 默認行為是-p ,
root:/tmp>sed '/6/p' num -p參數打印num的內容,爾后匹配“6”在次打印6,所以6會出現兩次。
1
2
3
4
5
6
6
7
8
root:/tmp>sed -n '/6/p' num -n選項取消sed的默認行為(-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標志則只有每行第一個被替換
root:/tmp>sed 's/1/8/g' num (sed默認有-p參數打印搜索后的所有行。如g后面加p,那么匹配行就會打印兩次)
8
2
3
4
5
6
7
8
root:/tmp>
行首“1”開頭被替換為“8”
root:/tmp>sed 's/^1/8/g' num
8
2
3
4
5
6
7
8
root:/tmp>
&符號表示替換字符串中被找到的部分,所以每個數字后面被追加.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>
\(..\) 保存匹配的字符到標簽\1中。 s/\(5\)/\1.6789/ 藍色部分保存到標簽\1中。從表達式最左邊開始,向右最多可以使用9個標簽
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后面的字符是分隔搜索字符串和替換字符串的分隔符。默認分隔符是斜杠,不論什么字符緊跟s命令都被認為是新的分隔符
root:/tmp>sed 's#6#shell#g' num
1
2
3
4
5
shell
7
8
root:/tmp>
14、多點編輯:e命令
root:/tmp>sed -e '1,6d' -e 's/8/8.shell/g' num
7
8.shell
root:/tmp>
15、-f 引導sed腳本文件名
16、選定行的范圍:逗號
從第一行到第五行
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>