日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Linuxsed命令完全攻略(超級詳細(xì))
我們知道,Vim 采用的是交互式文本編輯模式,你可以用鍵盤命令來交互性地插入、刪除或替換數(shù)據(jù)中的文本。但本節(jié)要講的 sed 命令不同,它采用的是流編輯模式,最明顯的特點(diǎn)是,在 sed 處理數(shù)據(jù)之前,需要預(yù)先提供一組規(guī)則,sed 會(huì)按照此規(guī)則來編輯數(shù)據(jù)。

創(chuàng)新互聯(lián)是專業(yè)的網(wǎng)站建設(shè)公司,提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì)等網(wǎng)站開發(fā)一體化解決方案;包括html5,成都小程序開發(fā),網(wǎng)站定制,企業(yè)網(wǎng)站建設(shè),購物商城網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,建網(wǎng)站,PHP網(wǎng)站建設(shè),軟件開發(fā),軟文營銷,網(wǎng)站營銷。歡迎做網(wǎng)站的企業(yè)前來合作洽談,創(chuàng)新互聯(lián)將竭誠為您服務(wù)!

sed 會(huì)根據(jù)腳本命令來處理文本文件中的數(shù)據(jù),這些命令要么從命令行中輸入,要么存儲(chǔ)在一個(gè)文本文件中,此命令執(zhí)行數(shù)據(jù)的順序如下:

  1. 每次僅讀取一行內(nèi)容;
  2. 根據(jù)提供的規(guī)則命令匹配并修改數(shù)據(jù)。注意,sed 默認(rèn)不會(huì)直接修改源文件數(shù)據(jù),而是會(huì)將數(shù)據(jù)復(fù)制到緩沖區(qū)中,修改也僅限于緩沖區(qū)中的數(shù)據(jù);
  3. 將執(zhí)行結(jié)果輸出。

當(dāng)一行數(shù)據(jù)匹配完成后,它會(huì)繼續(xù)讀取下一行數(shù)據(jù),并重復(fù)這個(gè)過程,直到將文件中所有數(shù)據(jù)處理完畢。

sed 命令的基本格式如下:

[root@localhost ~]# sed [選項(xiàng)] [腳本命令] 文件名

該命令常用的選項(xiàng)及含義,如表 1 所示。

表 1 sed 命令常用選項(xiàng)及含義
選項(xiàng) 含義
-e 腳本命令 該選項(xiàng)會(huì)將其后跟的腳本命令添加到已有的命令中。
-f 腳本命令文件 該選項(xiàng)會(huì)將其后文件中的腳本命令添加到已有的命令中。
-n 默認(rèn)情況下,sed 會(huì)在所有的腳本指定執(zhí)行完畢后,會(huì)自動(dòng)輸出處理后的內(nèi)容,而該選項(xiàng)會(huì)屏蔽啟動(dòng)輸出,需使用 print 命令來完成輸出。
-i 此選項(xiàng)會(huì)直接修改源文件,要慎用。

成功使用 sed 命令的關(guān)鍵在于掌握各式各樣的腳本命令及格式,它能幫你定制編輯文件的規(guī)則。

sed腳本命令

sed s 替換腳本命令

此命令的基本格式為:

[address]s/pattern/replacement/flags

其中,address 表示指定要操作的具體行,pattern 指的是需要替換的內(nèi)容,replacement 指的是要替換的新內(nèi)容。

關(guān)于指定具體操作行(address)的用法,這里先不做解釋,文章后續(xù)會(huì)對其做詳細(xì)介紹。

此命令中常用的 flags 標(biāo)記如表 2 所示。

表 2 sed s命令flags標(biāo)記及功能
flags 標(biāo)記 功能
n 1~512 之間的數(shù)字,表示指定要替換的字符串出現(xiàn)第幾次時(shí)才進(jìn)行替換,例如,一行中有 3 個(gè) A,但用戶只想替換第二個(gè) A,這是就用到這個(gè)標(biāo)記;
g 對數(shù)據(jù)中所有匹配到的內(nèi)容進(jìn)行替換,如果沒有 g,則只會(huì)在第一次匹配成功時(shí)做替換操作。例如,一行數(shù)據(jù)中有 3 個(gè) A,則只會(huì)替換第一個(gè) A;
p 會(huì)打印與替換命令中指定的模式匹配的行。此標(biāo)記通常與 -n 選項(xiàng)一起使用。
w file 將緩沖區(qū)中的內(nèi)容寫到指定的 file 文件中;
& 用正則表達(dá)式匹配的內(nèi)容進(jìn)行替換;
\n 匹配第 n 個(gè)子串,該子串之前在 pattern 中用 \(\) 指定。
\ 轉(zhuǎn)義(轉(zhuǎn)義替換部分包含:&、\ 等)。

比如,可以指定 sed 用新文本替換第幾處模式匹配的地方:

[root@localhost ~]# sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.

可以看到,使用數(shù)字 2 作為標(biāo)記的結(jié)果就是,sed 編輯器只替換每行中第 2 次出現(xiàn)的匹配模式。

如果要用新文件替換所有匹配的字符串,可以使用 g 標(biāo)記:

[root@localhost ~]# sed 's/test/trial/g' data4.txt
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ì)輸出修改過的行,將二者匹配使用的效果就是只輸出被替換命令修改過的行,例如:

[root@localhost ~]# cat data5.txt
This is a test line.
This is a different line.
[root@localhost ~]# sed -n 's/test/trial/p' data5.txt
This is a trial line.

w 標(biāo)記會(huì)將匹配后的結(jié)果保存到指定文件中,比如:

[root@localhost ~]# sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line.
[root@localhost ~]#cat test.txt
This is a trial line.

在使用 s 腳本命令時(shí),替換類似文件路徑的字符串會(huì)比較麻煩,需要將路徑中的正斜線進(jìn)行轉(zhuǎn)義,例如:

[root@localhost ~]# sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd

sed d 替換腳本命令

此命令的基本格式為:

[address]d

如果需要?jiǎng)h除文本中的特定行,可以用 d 腳本命令,它會(huì)刪除指定行中的所有內(nèi)容。但使用該命令時(shí)要特別小心,如果你忘記指定具體行的話,文件中的所有內(nèi)容都會(huì)被刪除,舉個(gè)例子:

[root@localhost ~]# cat data1.txt
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
[root@localhost ~]# sed 'd' data1.txt
#什么也不輸出,證明成了空文件

當(dāng)和指定地址一起使用時(shí),刪除命令顯然能發(fā)揮出大的功用??梢詮臄?shù)據(jù)流中刪除特定的文本行。

address 的具體寫法后續(xù)會(huì)做詳細(xì)介紹,這里只給大家舉幾個(gè)簡單的例子:

  • 通過行號指定,比如刪除 data6.txt 文件內(nèi)容中的第 3 行:

    [root@localhost ~]# cat data6.txt
    This is line number 1.
    This is line number 2.
    This is line number 3.
    This is line number 4.
    [root@localhost ~]# sed '3d' data6.txt
    This is line number 1.
    This is line number 2.
    This is line number 4.

  • 或者通過特定行區(qū)間指定,比如刪除 data6.txt 文件內(nèi)容中的第 2、3行:

    [root@localhost ~]# sed '2,3d' data6.txt
    This is line number 1.
    This is line number 4.

  • 也可以使用兩個(gè)文本模式來刪除某個(gè)區(qū)間內(nèi)的行,但這么做時(shí)要小心,你指定的第一個(gè)模式會(huì)“打開”行刪除功能,第二個(gè)模式會(huì)“關(guān)閉”行刪除功能,因此,sed 會(huì)刪除兩個(gè)指定行之間的所有行(包括指定的行),例如:

    [root@localhost ~]#sed '/1/,/3/d' data6.txt
    #刪除第 1~3 行的文本數(shù)據(jù)
    This is line number 4.

  • 或者通過特殊的文件結(jié)尾字符,比如刪除 data6.txt 文件內(nèi)容中第 3 行開始的所有的內(nèi)容:

    [root@localhost ~]# sed '3,$d' data6.txt
    This is line number 1.
    This is line number 2.

在此強(qiáng)調(diào),在默認(rèn)情況下 sed 并不會(huì)修改原始文件,這里被刪除的行只是從 sed 的輸出中消失了,原始文件沒做任何改變。

sed a 和 i 腳本命令

a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行,這里之所以要同時(shí)介紹這 2 個(gè)腳本命令,因?yàn)樗鼈兊幕靖袷酵耆嗤?,如下所示?br />

[address]a(或 i)\新文本內(nèi)容

下面分別就這 2 個(gè)命令,給讀者舉幾個(gè)例子。比如說,將一個(gè)新行插入到數(shù)據(jù)流第三行前,執(zhí)行命令如下:

[root@localhost ~]# sed '3i\
> This is an inserted line.' data6.txt
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.

再比如說,將一個(gè)新行附加到數(shù)據(jù)流中第三行后,執(zhí)行命令如下:

[root@localhost ~]# sed '3a\
> This is an appended line.' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an appended line.
This is line number 4.

如果你想將一個(gè)多行數(shù)據(jù)添加到數(shù)據(jù)流中,只需對要插入或附加的文本中的每一行末尾(除最后一行)添加反斜線即可,例如:

[root@localhost ~]# sed '1i\
> This is one line of new text.\
> This is another line of new text.' data6.txt
This is one line of new text.
This is another line of new text.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

可以看到,指定的兩行都會(huì)被添加到數(shù)據(jù)流中。

sed c 替換腳本命令

c 命令表示將指定行中的所有內(nèi)容,替換成該選項(xiàng)后面的字符串。該命令的基本格式為:

[address]c\用于替換的新文本

舉個(gè)例子:

[root@localhost ~]# sed '3c\
> This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.
在這個(gè)例子中,sed 編輯器會(huì)修改第三行中的文本,其實(shí),下面的寫法也可以實(shí)現(xiàn)此目的:
[root@localhost ~]# sed '/number 3/c\
> This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.

sed y 轉(zhuǎn)換腳本命令

y 轉(zhuǎn)換命令是唯一可以處理單個(gè)字符的 sed 腳本命令,其基本格式如下:

[address]y/inchars/outchars/

轉(zhuǎn)換命令會(huì)對 inchars 和 outchars 值進(jìn)行一對一的映射,即 inchars 中的第一個(gè)字符會(huì)被轉(zhuǎn)換為 outchars 中的第一個(gè)字符,第二個(gè)字符會(huì)被轉(zhuǎn)換成 outchars 中的第二個(gè)字符...這個(gè)映射過程會(huì)一直持續(xù)到處理完指定字符。如果 inchars 和 outchars 的長度不同,則 sed 會(huì)產(chǎn)生一條錯(cuò)誤消息。

舉個(gè)簡單例子:

[root@localhost ~]# sed 'y/123/789/' data8.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.

可以看到,inchars 模式中指定字符的每個(gè)實(shí)例都會(huì)被替換成 outchars 模式中相同位置的那個(gè)字符。

轉(zhuǎn)換命令是一個(gè)全局命令,也就是說,它會(huì)文本行中找到的所有指定字符自動(dòng)進(jìn)行轉(zhuǎn)換,而不會(huì)考慮它們出現(xiàn)的位置,再打個(gè)比方:

[root@localhost ~]# echo "This 1 is a test of 1 try." | sed 'y/123/456/'
This 4 is a test of 4 try.

sed 轉(zhuǎn)換了在文本行中匹配到的字符 1 的兩個(gè)實(shí)例,我們無法限定只轉(zhuǎn)換在特定地方出現(xiàn)的字符。

sed p 打印腳本命令

p 命令表示搜索符號條件的行,并輸出該行的內(nèi)容,此命令的基本格式為:

[address]p

p 命令常見的用法是打印包含匹配文本模式的行,例如:

[root@localhost ~]# cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@localhost ~]# sed -n '/number 3/p' data6.txt
This is line number 3.

可以看到,用 -n 選項(xiàng)和 p 命令配合使用,我們可以禁止輸出其他行,只打印包含匹配文本模式的行。

如果需要在修改之前查看行,也可以使用打印命令,比如與替換或修改命令一起使用??梢詣?chuàng)建一個(gè)腳本在修改行之前顯示該行,如下所示:

[root@localhost ~]# sed -n '/3/{
> p
> s/line/test/p
> }' data6.txt
This is line number 3.
This is test number 3.

sed 命令會(huì)查找包含數(shù)字 3 的行,然后執(zhí)行兩條命令。首先,腳本用 p 命令來打印出原始行;然后它用 s 命令替換文本,并用 p 標(biāo)記打印出替換結(jié)果。輸出同時(shí)顯示了原來的行文本和新的行文本。

sed w 腳本命令

w 命令用來將文本中指定行的內(nèi)容寫入文件中,此命令的基本格式如下:

[address]w filename

這里的 filename 表示文件名,可以使用相對路徑或絕對路徑,但不管是哪種,運(yùn)行 sed 命令的用戶都必須有文件的寫權(quán)限。

下面的例子是將數(shù)據(jù)流中的前兩行打印到一個(gè)文本文件中:

[root@localhost ~]# sed '1,2w test.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@localhost ~]# cat test.txt
This is line number 1.
This is line number 2.

當(dāng)然,如果不想讓行直接輸出,可以用 -n 選項(xiàng),再舉個(gè)例子:

[root@localhost ~]# cat data11.txt
Blum, R       Browncoat
McGuiness, A  Alliance
Bresnahan, C  Browncoat
Harken, C     Alliance
[root@localhost ~]# sed -n '/Browncoat/w Browncoats.txt' data11.txt
cat Browncoats.txt
Blum, R       Browncoat
Bresnahan, C  Browncoat

可以看到,通過使用 w 腳本命令,sed 可以實(shí)現(xiàn)將包含文本模式的數(shù)據(jù)行寫入目標(biāo)文件。

sed r 腳本命令

r 命令用于將一個(gè)獨(dú)立文件的數(shù)據(jù)插入到當(dāng)前數(shù)據(jù)流的指定位置,該命令的基本格式為:

[address]r filename

sed 命令會(huì)將 filename 文件中的內(nèi)容插入到 address 指定行的后面,比如說:

[root@localhost ~]# cat data12.txt
This is an added line.
This is the second added line.
[root@localhost ~]# sed '3r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.

如果你想將指定文件中的數(shù)據(jù)插入到數(shù)據(jù)流的末尾,可以使用 $ 地址符,例如:

[root@localhost ~]# sed '$r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is an added line.
This is the second added line.

sed q 退出腳本命令

q 命令的作用是使 sed 命令在第一次匹配任務(wù)結(jié)束后,退出 sed 程序,不再進(jìn)行對后續(xù)數(shù)據(jù)的處理。

比如:

[root@localhost ~]# sed '2q' test.txt
This is line number 1.
This is line number 2.

可以看到,sed 命令在打印輸出第 2 行之后,就停止了,是 q 命令造成的,再比如:

[root@localhost ~]# sed '/number 1/{ s/number 1/number 0/;q; }' test.txt
This is line number 0.

使用 q 命令之后,sed 命令會(huì)在匹配到 number 1 時(shí),將其替換成 number 0,然后直接退出。

sed 腳本命令的尋址方式

前面在介紹各個(gè)腳本命令時(shí),我們一直忽略了對 address 部分的介紹。對各個(gè)腳本命令來說,address 用來表明該腳本命令作用到文本中的具體行。

默認(rèn)情況下,sed 命令會(huì)作用于文本數(shù)據(jù)的所有行。如果只想將命令作用于特定行或某些行,則必須寫明 address 部分,表示的方法有以下 2 種:

  1. 以數(shù)字形式指定行區(qū)間;
  2. 用文本模式指定具體行區(qū)間。

以上兩種形式都可以使用如下這 2 種格式,分別是:

[address]腳本命令

或者

address {
    多個(gè)腳本命令
}

以上兩種形式在前面例子中都有具體實(shí)例,因此這里不再做過多贅述。

以數(shù)字形式指定行區(qū)間

當(dāng)使用數(shù)字方式的行尋址時(shí),可以用行在文本流中的行位置來引用。sed 會(huì)將文本流中的第一行編號為 1,然后繼續(xù)按順序?yàn)榻酉聛淼男蟹峙湫刑枴?/p>

在腳本命令中,指定的地址可以是單個(gè)行號,或是用起始行號、逗號以及結(jié)尾行號指定的一定區(qū)間范圍內(nèi)的行。這里舉一個(gè) sed 命令作用到指定行號的例子:

[root@localhost ~]#sed '2s/dog/cat/' data1.txt
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

可以看到,sed 只修改地址指定的第二行的文本。下面的例子中使用了行地址區(qū)間:

[root@localhost ~]# sed '2,3s/dog/cat/' data1.txt
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

在此基礎(chǔ)上,如果想將命令作用到文本中從某行開始的所有行,可以用特殊地址——美元符($):

[root@localhost ~]# sed '2,$s/dog/cat/' data1.txt
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

用文本模式指定行區(qū)間

sed 允許指定文本模式來過濾出命令要作用的行,格式如下:

/pattern/command

注意,必須用正斜線將要指定的 pattern 封起來,sed 會(huì)將該命令作用到包含指定文本模式的行上。

舉個(gè)例子,如果你想只修改用戶 demo 的默認(rèn) shell,可以使用 sed 命令,執(zhí)行命令如下:

[root@localhost ~]# grep demo /etc/passwd
demo:x:502:502::/home/Samantha:/bin/bash
[root@localhost ~]# sed '/demo/s/bash/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
demo:x:502:502::/home/demo:/bin/csh
...

雖然使用固定文本模式能幫你過濾出特定的值,就跟上面這個(gè)用戶名的例子一樣,但其作用難免有限,因此,sed 允許在文本模式使用正則表達(dá)式指明作用的具體行。正則表達(dá)式允許創(chuàng)建高級文本模式匹配表達(dá)式來匹配各種數(shù)據(jù)。這些表達(dá)式結(jié)合了一系列通配符、特殊字符以及固定文本字符來生成能夠匹配幾乎任何形式文本的簡練模式。

關(guān)于正則表達(dá)式,本節(jié)不做過多介紹,有興趣的讀者可閱讀《正則表達(dá)式入門教程》一文,這里僅給讀者提供一個(gè)簡單示例:

[root@localhost ~]# cat test.txt

First Wed

h1Helloh1
h2Helloh2
h3Helloh3


#使用正則表示式給所有第一個(gè)的h1、h2、h3添加<>,給第二個(gè)h1、h2、h3添加
[root@localhost ~]# cat sed.sh
/h[0-9]/{
    s//\<&\>/1
    s//\<\/&\>/2
}
[root@localhost ~]# sed -f sed.sh test.txt

Hello


Hello


Hello

收到篇幅的限制,本節(jié)僅介紹了 sed 命令每次只讀取一行內(nèi)容并處理,有關(guān) sed 命令如何一次處理多行文本內(nèi)容,放到下節(jié)繼續(xù)講解,讀者可點(diǎn)擊《Linux sed命令高級用法》繼續(xù)學(xué)習(xí)。


名稱欄目:Linuxsed命令完全攻略(超級詳細(xì))
瀏覽地址:http://www.dlmjj.cn/article/djsgoii.html