sed編輯器基礎(chǔ)操作

  1. 替換命令(substitute)
    $ cat data4
    This is a test of the test script
    This is the second test of the test script

替換命令默認(rèn)情況下,只替換每行中出現(xiàn)的第一處。要替換不同地方出現(xiàn)的文本必須使用【替換標(biāo)記】,替換標(biāo)記會(huì)在替換命令字符串之后設(shè)置
$ sed 's/test/trial/' data4
This is a trial of the test script
This is the second trial of the test script

替換標(biāo)記(flags)
格式:s/pattern/replacement/flags
有四種可用的替換標(biāo)記:
數(shù)字:表明新文件將替換第幾處模式匹配的地方
g :表明新文件將會(huì)替換所有匹配的文本
p : 表明原先行的內(nèi)容要打印出來(lái)
w file :將替換的結(jié)果寫(xiě)入到文件中

替換每行中的第2處匹配的文本
$ sed 's/test/trial/2' data4
This is a test of the trial script
This is the second test of the trial script

替換整個(gè)文件中的匹配文本
$ sed 's/test/trial/g' data4
This is a trial of the trial script
This is the second trial of the trial script

-n選項(xiàng)會(huì)禁止sed編輯器輸出,p替換標(biāo)記會(huì)輸出修改后的行。二者結(jié)合使用就是:只輸出被替換命令修改后的行
$ sed -n 's/second/last/p' data4
This is the last test of the test script

  1. 使用地址
    $ cat data1
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog

默認(rèn)情況下,替換命令作用于文本的所有行,若要作用于特定行或某些行,則使用【行尋址】,兩種形式的行尋址:
以數(shù)字形式表示行區(qū)間;
用文本模式過(guò)濾器。
1)指定單個(gè)行號(hào)
$ sed '2s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

2)用起始行號(hào),逗號(hào),結(jié)尾行號(hào)指定一定范圍區(qū)間的行
$ sed '2,3s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog

3)從某行開(kāi)始的所有行,使用特殊地址$符號(hào)

sed '2,$s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

4)使用文本模式過(guò)濾器
格式:/pattern/command (必須用正斜線將指定的pattern封起來(lái))
$ sed '/fox/s/dog/cat/' data1
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

5)命令組合
格式: address{
command1
command2
...
}
$ sed '2{
s/fox/elphent/
s/dog/cat/
}' data1
The quick brown fox jumps over the lazy dog
The quick brown elphent jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

sed '3,$ {
s/brown/green/
s/lazy/cative/
}' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick green fox jumps over the cative dog
The quick green fox jumps over the cative dog

  1. 插入(insert)和附加(append)文本
    $ cat data6
    This is line number 1
    This is line number 2
    This is line number 3
    This is line number 4

插入(insert)命令(i)會(huì)在指定行前增加一個(gè)新行
附加(append)命令(a)會(huì)在指定行后增加一個(gè)新行

在第3行前增加一行
sed '3i\
$> This is an inserted line' data6
This is line number 1
This is line number 2
This is an inserted line
This is line number 3
This is line number 4

在第3行后增加一行
sed '3a\
$>This is an append line' data6
This is line number 1
This is line number 2
This is line number 3
This is an append line
This is line number 4

在最后一行增加一行
  1. 修改行
    修改(change)命令(c),必須在sed命令中單獨(dú)指定新行
    $ cat data6
    This is line number 1
    This is line number 2
    This is line number 3
    This is line number 4

替換第二行信息

$ sed '2c\

This is a change line of test' data6
This is line number 1
This is a change line of test
This is line number 3
This is line number 4

使用文本模式過(guò)濾器 替換 匹配到 number 3 模式的行

$ sed '/number 3/c
This is a change line of test' data6
This is line number 1
This is line number 2
This is a change line of test
This is line number 4

使用行尋址指定行區(qū)間第2行~第3行 ,并進(jìn)行替換

$ sed '2,3c\
This is a change line of test' data6
This is line number 1
This is a change line of test
This is line number 4

  1. 刪除行
    刪除(delete)命令(d),可以通過(guò)特定行區(qū)間、特殊的文件結(jié)尾符 、模式匹配。可使用兩個(gè)文本模式來(lái)刪除某個(gè)區(qū)間內(nèi)的行,第一個(gè) 模式會(huì)“打開(kāi)”行刪除功能,第二個(gè)模式會(huì)“關(guān)閉”行刪除功能 cat data6
    This is line number 1
    This is line number 2
    This is line number 3
    This is line number 4

刪除第3行

$ sed '3d' data6
This is line number 1
This is line number 2
This is line number 4

刪除第2、3行

$ sed '2,3d' data6
This is line number 1
This is line number 4

從第3行刪除到最后一行

sed '3,d' data6
This is line number 1
This is line number 2

使用文本模式過(guò)濾器,刪除 匹配到 number 2模式的行

$ sed '/number 2/d' data6
This is line number 1
This is line number 3
This is line number 4

使用兩個(gè)文本模式來(lái)刪除,匹配到number 1模式“開(kāi)啟”刪除功能,匹配到number 3模式“關(guān)閉”刪除功能

$ sed '/number 1/,/number 3/d' data6
This is line number 4

  1. 使用sed處理文件

1)寫(xiě)入文件,使用w命令向文件中寫(xiě)入行
格式:[address] w filename
address 支持單個(gè)行號(hào)、文本模式、行區(qū)間等尋址方式
filename 可以使用相對(duì)路徑或絕對(duì)路徑

將data6中的第1,2行寫(xiě)入到testwrite文件中

sed -n '1,2 w testwrite' data6
$ cat testwrite
This is line number 1
This is line number 2

2)從文件中讀取數(shù)據(jù)
讀?。╮ead)命令(r)允許將一個(gè)獨(dú)立的文件中的數(shù)據(jù)插入到數(shù)據(jù)流中。sed編輯器會(huì)將文件中文本插入到指定地址后
格式:[address] r filename
address 支持單個(gè)行號(hào)、文本模式。

$ cat testread
This is test read from file and add

將文件中的信息添加到data6文件第3行之后

$ sed '3r testread' data6
This is line number 1
This is line number 2
This is line number 3
This is test read from file and add
This is line number 4

  1. 轉(zhuǎn)換命令
    轉(zhuǎn)換(transform)命令(y)可以處理單個(gè)字符
    格式:[address]y/inchars/outchars/
    轉(zhuǎn)換命令會(huì)對(duì)inchars和outchars的值進(jìn)行一對(duì)一的映射,inchars的第一個(gè)字符會(huì)被轉(zhuǎn)換成outchars中的第一個(gè)字符,依此類(lèi)推,直到處理完指定的字符。
    $ cat data6
    This is line number 1
    This is line number 2
    This is line number 3
    This is line number 4

將1234分別替換為5678

$ sed 'y/1234/5678/' data6
This is line number 5
This is line number 6
This is line number 7
This is line number 8

  1. sed命令,-i 選項(xiàng)直接修改讀取的文件內(nèi)容,而不是輸出到終端。直接對(duì)文本文件進(jìn)行操作
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容