技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類(lèi) >

Linux下的sed命令使用詳解

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2018-02-24 12:01┆點(diǎn)擊:

  sed是stream editor的簡(jiǎn)稱(chēng),也就是流編輯器。它一次處理一行內(nèi)容,處理時(shí),把當(dāng)前處理的行存儲(chǔ)在臨時(shí)緩沖區(qū)中,稱(chēng)為“模式空間”pattern space,接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒(méi)有 改變,除非你使用重定向存儲(chǔ)輸出。

  使用語(yǔ)法

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

  復(fù)制代碼

  代碼如下:

  sed [option] 'command' input_file

  其中option是可選的,常用的option有如下幾種:

  -n 使用安靜silent模式(想不通為什么不是-s)。在一般sed的用法中,所有來(lái)自stdin的內(nèi)容一般都會(huì)被列出到屏幕上。但如果加上-n參數(shù)后,則只有經(jīng)過(guò)sed特殊處理的那一行(或者動(dòng)作)才會(huì)被列出來(lái);

  -e 直接在指令列模式上進(jìn)行 sed 的動(dòng)作編輯;

  -f 直接將 sed 的動(dòng)作寫(xiě)在一個(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 :取代/替換行change,c 后面跟上字符串s(多行字符串可以用n分隔),則會(huì)將當(dāng)前選中的行替換成字符串s;

  i :插入行insert,i 后面跟上字符串s(多行字符串可以用n分隔),則會(huì)在當(dāng)前選中的行的前面都插入字符串s;

  d:刪除行delete,該命令會(huì)將當(dāng)前選中的行刪除;

  p:打印print,該命令會(huì)打印當(dāng)前選擇的行到屏幕上;

  s:替換字符串subs,通常s命令的用法是這樣的:1,2s/old/new/g,將old字符串替換成new字符串

  命令示例

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

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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

  本節(jié)將使用該文件詳細(xì)演示每一個(gè)命令的用法。

  a命令(追加行)

  例一

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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 is fifth line

  happy everyday

  end

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

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

  例二

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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 is fifth line

  add one

  happy everyday

  add one

  end

  add one

  本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,從輸出可以看到效果。

  例三

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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 is fifth line

  happy everyday

  end

  本例表示在包含”first”字符串的行的后面加上字符串”add one”,從輸出可以看到第一行包含first,所以第一行之后增加了”add one”

  例四

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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 is fifth line

  happy everyday

  add one

  end

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

  i命令(插入行)

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

  c命令(替換行)

  例五

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed '$c add one' test.txt

  this is first line

  this is second line

  this is third line

  this is fourth line

  this is fifth line

  happy everyday

  add one

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

  例六

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed '4,$c add one' test.txt

  this is first line

  this is second line

  this is third line

  add one

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

  例七

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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 is fifth line

  replace line

  end

  本例將以ha開(kāi)頭,以day結(jié)尾的行替換成”replace line”。

  d命令(刪除行)

  例八

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed '/^ha.*day$/d' test.txt

  this is first line

  this is second line

  this is third line

  this is fourth line

  this is fifth line

  end

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

  例九

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed '4,$d' test.txt

  this is first line

  this is second line

  this is third line

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

  p命令(打印行)

  例十

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed -n '4,$p' test.txt

  this is fourth line

  this is fifth line

  happy everyday

  end

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

  例十一

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed -n '/^ha.*day$/p' test.txt

  happy everyday

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

  s命令(替換字符串)

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

  例十二

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed 's/line/text/g' test.txt

  this is first text

  this is second text

  this is third text

  this is fourth text

  this is fifth text

  happy everyday

  end

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

  例十三

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ 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 is fifth line

  very happy everyday

  end

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

  例十四

  復(fù)制代碼

  代碼如下:

  [qifuguang@winwill~]$ sed 's/(.*)line$/1/g' test.txt

  this is first

  this is second

  this is third

  this is fourth

  this is fifth

  happy everyday

  end