sed是一種流編輯器(Stream EDitor seq),它一次處理一行內(nèi)容。處理時(shí),把當(dāng)前處理的行存儲(chǔ)在臨時(shí)緩沖區(qū)中,稱為“模式空間”,接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有改變,除非你使用重定向存儲(chǔ)輸出。
1. 基本用法
sed [選項(xiàng)參數(shù)] ‘command’ filename
2. 選項(xiàng)參數(shù)說明
選項(xiàng)參數(shù) | 功能 |
-e | 直接在指令列模式上進(jìn)行sed的動(dòng)作編輯。 |
-i | 直接編輯文件 |
3. 命令功能描述
命令 | 功能描述 |
a | 新增,a的后面可以接字串,在下一行出現(xiàn) |
d | 刪除 |
s | 查找并替換 |
4. 案例實(shí)操
(0)數(shù)據(jù)準(zhǔn)備
[cda@hadoop102 datas]$ touch sed.txt
[cda@hadoop102 datas]$ vim sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
(1)將“mei nv”這個(gè)單詞插入到sed.txt第二行下,打印。
[cda@hadoop102 datas]$ sed '2a mei nv' sed.txt
dong shen
guan zhen
mei nv
wo wo
lai lai
le le
[cda@hadoop102 datas]$ cat sed.txt
dong shen
guan zhen
wo wo
lai lai
le le
注意:文件并沒有改變
(2)刪除sed.txt文件所有包含wo的行
[cda@hadoop102 datas]$ sed '/wo/d' sed.txt
dong shen
guan zhen
lai lai
le le
(3)將sed.txt文件中wo替換為ni
[cda@hadoop102 datas]$ sed 's/wo/ni/g' sed.txt
dong shen
guan zhen
ni ni
lai lai
le le
注意:‘g’表示global,全部替換
(4)將sed.txt文件中的第二行刪除并將wo替換為ni
[cda@hadoop102 datas]$ sed -e '2d' -e 's/wo/ni/g' sed.txt
dong shen
ni ni
lai lai
le le








暫無數(shù)據(jù)