欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂紅帽Linux開發(fā)培訓(xùn)之sed流編輯器命令詳解

2018-08-20 13:02:15 1135瀏覽

  今天扣丁學(xué)堂紅帽Linux培訓(xùn)老師給大家介紹一下關(guān)于sed流編輯器命令,首先sed是streameditor的簡稱,也就是流編輯器。它一次處理一行內(nèi)容,處理時(shí),把當(dāng)前處理的行存儲(chǔ)在臨時(shí)緩沖區(qū)中,稱為“模式空間”(patternspace),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有改變,除非你使用重定向存儲(chǔ)輸出。

sed命令的使用規(guī)則是這樣的:

sed [option] 'command' input_file
-n 使用安靜(silent)模式(想不通為什么不是-s)。在一般sed的用法中,所有來自stdin的內(nèi)容一般都會(huì)被列出到屏幕上。但如果加上-n參數(shù)后,則只有經(jīng)過sed特殊處理的那一行(或者動(dòng)作)才會(huì)被列出來;其中option是可選的,常用的option有如下幾種:
-e 直接在指令列模式上進(jìn)行 sed 的動(dòng)作編輯;
-f 直接將 sed 的動(dòng)作寫在一個(gè)文件內(nèi), -f filename 則可以執(zhí)行filename內(nèi)的sed命令;
-r 讓sed命令支持?jǐn)U展的正則表達(dá)式(默認(rèn)是基礎(chǔ)正則表達(dá)式);
-i 直接修改讀取的文件內(nèi)容,而不是由屏幕輸出。

常用的命令有以下幾種:

a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),則會(huì)在當(dāng)前選擇的行的后面都加上字符串s;

c \: 取代/替換字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),則會(huì)將當(dāng)前選中的行替換成字符串s;
d: delete即刪除,該命令會(huì)將當(dāng)前選中的行刪除;
i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),則會(huì)在當(dāng)前選中的行的前面都插入字符串s;
p: print即打印,該命令會(huì)打印當(dāng)前選擇的行到屏幕上;
s: 替換,通常s命令的用法是這樣的:1,2s/old/new/g,將old字符串替換成new字符串
  動(dòng)作說明: [n1[,n2]]function
n1, n2 :不見得會(huì)存在,一般代表『選擇進(jìn)行動(dòng)作的行數(shù)』,舉例來說,如果我的動(dòng)作是需要在 10 到 20 行之間進(jìn)行的,則『 10,20[動(dòng)作行為] 』

命令示例,假設(shè)有一個(gè)本地文件test.txt,文件內(nèi)容如下:

[root@master rh]# cat test.txt 
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end
[root@master rh]# sed '1a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用$表示最后一行,比如2,$表示第二行到最后一行中間所有的行(包含第二行和最后一行)。

本例的作用是在第一行之后增加字符串”addone”,從輸出可以看到具體效果。

[root@master rh]# sed '1,$a \add one' test.txt
this is first line
add one
this is second line
add one
this is third line
add one
this is fourth line
add one
this fifth line
add one
happy everyday
add one
end
add one

本例表示在第一行和最后一行所有的行后面都加上”addone”字符串,從輸出可以看到效果。
[root@master rh]# sed '/first/a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end

本例表示在包含”first”字符串的行的后面加上字符串”addone”,從輸出可以看到第一行包含first,所以第一行之后增加了”add one”
 
[root@master rh]# sed '/^ha.*day$/a \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
add one
end

本例使用正則表達(dá)式匹配行,^ha.*day$表示以ha開頭,以day結(jié)尾的行,則可以匹配到文件的”happyeveryday”這樣,所以在該行后面增加了”addone”字符串。

i命令

i命令使用方法和a命令一樣的,只不過是在匹配的行的前面插入字符串,所以直接將上面a命令的示例的a替換成i即可,在此就不啰嗦了。

c命令

[root@master rh]# sed '$c \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
add one

本例表示將最后一行替換成字符串”addone”,從輸出可以看到效果。

[root@master rh]# sed '4,$c \add one' test.txt
this is first line
this is second line
this is third line
add one

本例將第四行到最后一行的內(nèi)容替換成字符串”addone”。

[root@master rh]# sed '/^ha.*day$/c \replace line' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
replace line
end

d命令

[root@master rh]# sed '/^ha.*day$/d' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
end

本例刪除以ha開頭,以day結(jié)尾的行。

[root@master rh]# sed '4,$d' test.txt
this is first line
this is second line
this is third line

本例刪除第四行到最后一行中的內(nèi)容。

p命令

[root@master rh]# sed -n '4,$p' test.txt
this is fourth line
this fifth line
happy everyday
end

本例在屏幕上打印第四行到最后一行的內(nèi)容,p命令一般和-n選項(xiàng)一起使用。

[root@master rh]# sed -n '/^ha.*day$/p' test.txt
happy everyday

本例打印以ha開始,以day結(jié)尾的行。

s命令

實(shí)際運(yùn)用中s命令式最常使用到的。

[root@master rh]# sed 's/line/text/g' test.txt
this is first text
this is second text
this is third text
this is fourth text
this fifth text
happy everyday
end

本例將文件中的所有l(wèi)ine替換成text,最后的g是global的意思,也就是全局替換,如果不加g,則只會(huì)替換本行的第一個(gè)line。

[root@master rh]# sed '/^ha.*day$/s/happy/very happy/g' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
very happy everyday
end

本例首先匹配以ha開始,以day結(jié)尾的行,本例中匹配到的行是”happyeveryday”這樣,然后再將該行中的happy替換成veryhappy。

[root@master rh]# sed 's/\(.*\)line$/\1/g' test.txt
this is first 
this is second 
this is third 
this is fourth 
this fifth 
happy everyday
end

這個(gè)例子有點(diǎn)復(fù)雜,先分解一下。首先s命令的模式是s/old/new/g這樣的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的內(nèi)容表示正則表達(dá)式的第n部分,序號(hào)從1開始計(jì)算,本例中只有一個(gè)\(\)所以\(.*\)表示正則表達(dá)式的第一部分,這部分匹配任意字符串,所以\(.*\)line$匹配的就是以line結(jié)尾的任何行。然后將匹配到的行替換成正則表達(dá)式的第一部分(本例中相當(dāng)于刪除line部分),使用\1表示匹配到的第一部分,同樣\2表示第二部分,\3表示第三部分,可以依次這樣引用。比如下面的例子:

[root@master rh]# sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txt
this  first 
this  second 
this  third 
this  fourth 
th fifth 
happy everyday
end

正則表達(dá)式中is兩邊的部分可以用\1和\2表示,該例子的作用其實(shí)就是刪除中間部分的is,希望對(duì)同學(xué)們有所幫助,想要了解更多關(guān)于Linux開發(fā)方面內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)咨詢??鄱W(xué)堂有大量的Linux視頻教程讓學(xué)員免費(fèi)觀看學(xué)習(xí),想要快速學(xué)習(xí)Linux開發(fā)就到由專業(yè)老師授課的扣丁學(xué)堂學(xué)習(xí)吧??鄱W(xué)堂Linux技術(shù)交流群:422345477。 

扣丁學(xué)堂微信公眾號(hào)


【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】



查看更多關(guān)于“Linux培訓(xùn)資訊”的相關(guān)文章>>

標(biāo)簽: Linux培訓(xùn) Linux視頻教程 紅帽Linux視頻 Linux學(xué)習(xí)視頻 Linux入門視頻

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部