sed 用法介紹

sed 用法介紹

s e d是一個非交互性文本流編輯器。它編輯文件或標準輸入導出的文本拷貝。

引用:
• 抽取域。
• 匹配正則表達式。
• 比較域。
• 增加、附加、替換。
• 基本的s e d命令和一行腳本。


可 以在命令行輸入s e d命令,也可以在一個文件中寫入命令,然後調用s e d,這與a w k基本相同。使用s e d需要記住的一個重要事實是,無論命令是什麼, s e d並不與初始化文件打交道,它操作的只是一個拷貝,然後所有的改動如果沒有重定向到一個文件,將輸出到屏幕。

因爲s e d是一個非交互性編輯器,必須通過行號或正則表達式指定要改變的文本行。
本文介紹s e d用法和功能。本章大多編寫的是一行命令和小腳本。這樣做可以慢慢加深對s e d用法的瞭解,取得寶貴的經驗,以便最終自己編出大的複雜s e d腳本。
和g r e p與a w k一樣, s e d是一種重要的文本過濾工具,或者使用一行命令或者使用管道與g r e p與a w k相結合。

1 sed怎樣讀取數據

s e d從文件的一個文本行或從標準輸入的幾種格式中讀取數據,將之拷貝到一個編輯緩衝區,然後讀命令行或腳本的第一條命令,並使用這些命令查找模式或定位行號編輯它。重複此過程直到命令結束。

2 調用sed

調用s e d有三種方式:在命令行鍵入命令;將s e d命令插入腳本文件,然後調用s e d;將s e d命令插入腳本文件,並使s e d腳本可執行。

使用s e d命令行格式爲:
代碼:
sed [選項] s e d命令輸入文件。

記住在命令行使用s e d命令時,實際命令要加單引號。s e d也允許加雙引號。

使用s e d腳本文件,格式爲:
代碼:
sed [選項] -f sed腳本文件輸入文件


要使用第一行具有s e d命令解釋器的s e d腳本文件,其格式爲:
代碼:
s e d腳本文件[選項] 輸入文件


不管是使用s h e l l命令行方式或腳本文件方式,如果沒有指定輸入文件, s e d從標準輸入中接受輸入,一般是鍵盤或重定向結果。

引用:
s e d選項如下:
n 不打印;s e d不寫編輯行到標準輸出,缺省爲打印所有行(編輯和未編輯)。p命令可以用來打印編輯行。
c 下一命令是編輯命令。使用多項編輯時加入此選項。如果只用到一條s e d命令,此選項無用,但指定它也沒有關係。
f 如果正在調用s e d腳本文件,使用此選項。此選項通知s e d一個腳本文件支持所有的s e d命令,例如:sed -f myscript.sed input_file,這裏m y s c r i p t . s e d即爲支持s e d命令的文件。


2.1 保存sed輸出
由於不接觸初始化文件,如果想要保存改動內容,簡單地將所有輸出重定向到一個文件即可。下面的例子重定向s e d命令的所有輸出至文件‘ m y o u t f i l e’,當對結果很滿意時使用這種方法。
代碼:
$sed 'some-sed-commands' input-file > myoutfile


2.2 使用sed在文件中查詢文本的方式
s e d瀏覽輸入文件時,缺省從第一行開始,有兩種方式定位文本:
引用:
1) 使用行號,可以是一個簡單數字,或是一個行號範圍。
2 ) 使用正則表達式


下面是使用s e d定位文本的一些方式。

代碼:
x x爲一行號,如1
x , y 表示行號範圍從x到y,如2,5表示從第2行到第5行
/ p a t t e r n / 查詢包含模式的行。例如/ d i s k /或/[a-z]/
/ p a t t e r n / p a t t e r n / 查詢包含兩個模式的行。例如/ d i s k / d i s k s /
p a t t e r n / , x 在給定行號上查詢包含模式的行。如/ r i b b o n / , 3
x , / p a t t e r n / 通過行號和模式查詢匹配行。3 . / v d u /
x , y ! 查詢不包含指定行號x和y的行。1 , 2 !


2.3 基本sed編輯命令

代碼:
sed編輯命令
p 打印匹配行
= 顯示文件行號
a / 在定位行號後附加新文本信息
i / 在定位行號後插入新文本信息
d 刪除定位行
c / 用新文本替換定位文本
s 使用替換模式替換相應模式
r 從另一個文件中讀文本
w 寫文本到一個文件
q 第一個模式匹配完成後推出或立即推出
l 顯示與八進制A S C I I代碼等價的控制字符
{ } 在定位行執行的命令組
n 從另一個文件中讀文本下一行,並附加在下一行
g 將模式2粘貼到/pattern n/
y 傳送字符
n 延續到下一輸入行;允許跨行的模式匹配語句




sed和正則表達式

s e d識別任何基本正則表達式和模式及其行匹配規則。記住規則之一是:如果要定位一特殊字符,必須使用( /)屏蔽其特殊含義


s e d例子中使用下述文本文件q u o t e . t x t。

代碼:
[sam@Linux_chenwy sam]$ cat quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.



1 使用p(rint)顯示行
只打印第二行,用-n

代碼:
[sam@Linux_chenwy sam]$ sed -n '2p' quote.txt
It was an evening of splendid music and company.



2 打印範圍
可以指定行的範圍,現打印1到3行,用逗號分隔行號。

代碼:
[sam@Linux_chenwy sam]$ sed -n '1,3p' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.



3 打印模式
假定要匹配單詞N e a v e,並打印此行,方法如下。使用模式/ p a t t e r n /格式,這裏爲/ N e a v e /。

代碼:
[sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt
The honeysuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendance.



4 使用模式和行號進行查詢
可以將行號和模式結合使用。假定要改動文件q u o t e . t x t最後一行中的單詞t h e,使用s e d查詢t h e,返回兩行:

代碼:
[sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt
The honeysuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendance.



使用模式與行號的混合方式可以剔除第一行,格式爲l i n e _ n u m b e r, / p a t t e r n /。逗號用來分隔行號與模式開始部分。爲達到預期結果,使用4 , / t h e /。意即只在第四行查詢模式t h e,命令如下:

代碼:
[sam@Linux_chenwy sam]$ sed -n '4,/The/p' quote.txt
The local nurse Miss P.Neave was in attendance.


上面有錯,其實是把第四行後的都打出來了
這個模式應該哪果指定行找不到符合條件的,就從下一行開始查找,直到找到爲止,並把,找到行之前的全部打打印出來。
如果指定行本身就符合條伯,把本行及後面的行的全部打印出來


5 匹配元字符
匹配元字符$前,必須使用反斜線/屏蔽其特殊含義。模式爲//$/ p。

代碼:
[sam@Linux_chenwy sam]$ sed -n '//$/p' quote.txt
The honeysuckle band played all night long for only $90.



6 顯示整個文件
要打印整個文件,只需將行範圍設爲第一行到最後一行1 , $。$意爲最後一行。

代碼:
[sam@Linux_chenwy sam]$ sed -n '1,$p' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.



7 任意字符
匹配任意字母,後跟任意字母的0次或多次重複,並以i n g結尾,模式爲/ . * i n g /。可以使用這個模式查詢以i n g結尾的任意單詞。

代碼:
[sam@Linux_chenwy sam]$ sed -n '/.*ing/p' quote.txt
It was an evening of splendid music and company.



8 首行
要打印文件第一行,使用行號:

代碼:
[sam@Linux_chenwy sam]$ sed -n '1p' quote.txt
The honeysuckle band played all night long for only $90.



9 最後一行
要打印最後一行,使用$。$是代表最後一行的元字符。

代碼:
[sam@Linux_chenwy sam]$ sed -n '$p' quote.txt
The local nurse Miss P.Neave was in attendance.



10 打印行號
要打印行號,使用等號=。打印模式匹配的行號,使用格式/ p a t t e r n / =。

代碼:
[sam@Linux_chenwy sam]$ sed -e '/music/=' quote.txt
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.



整個文件都打印出來,並且匹配行打印了行號。如果只關心實際行號,使用- e選項。

代碼:
[sam@Linux_chenwy sam]$ sed -n '/music/=' quote.txt
2



如果只打印行號及匹配行,必須使用兩個s e d命令,並使用e選項。第一個命令打印模式匹配行,第二個使用=選項打印行號,格式爲sed -n -e /pattern/p -e /pattern/=

代碼:
[sam@Linux_chenwy sam]$ sed -n -e '/music/p' -e '/music/='  quote.txt
It was an evening of splendid music and company.
2


P.S:新版本的sed(GNU sed version 4.0.5)帶有-i選項,允許直接編輯修改文件內容而不需要重定向到臨時文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/home/lee#cat file
new
abc

11 附加文本

要 附加文本,使用符號a /,可以將指定文本一行或多行附加到指定行。如果不指定文本放置位置, s e d缺省放在每一行後面。附加文本時不能指定範圍,只允許一個地址模式。文本附加操作時,結果輸出在標準輸出上。注意它不能被編輯,因爲s e d執行時,首先將文件的一行文本拷貝至緩衝區,在這裏s e d編輯命令執行所有操作(不是在初始文件上),因爲文本直接輸出到標準輸出,s e d並無拷貝。
要想在附加操作後編輯文本,必須保存文件,然後運行另一個s e d命令編輯它。這時文件的內容又被移至緩衝區。
附加操作格式如下:

代碼:
[address]a/
text/
text/
......
text


地 址指定一個模式或行號,定位新文本附加位置。a/ 通知s e d對a /後的文本進行實際附加操作。觀察格式,注意每一行後面有一斜劃線,這個斜劃線代表換行。s e d執行到這兒,將創建一新行,然後插入下一文本行。最後一行不加斜劃線, s e d假定這是附加命令結尾。

當附加或插入文本或鍵入幾個s e d命令時,可以利用輔助的s h e l l提示符以輸入多行命令。當附加或插入文本或鍵入幾個s e d命令時,可以利用輔助的s h e l l提示符以輸入多行命令。

創建sed腳本文件

創建腳本文件a p p e n d . s e d:
第一行是s e d命令解釋行。腳本在這一行查找s e d以運行命令,這裏定位在/ b i n。
第二行以/ c o m p a n y /開始,這是附加操作起始位置。a /通知s e d這是一個附加操作,首先應插入一個新行。
第三行是附加操作要加入到拷貝的實際文本。
輸出顯示附加結果。如果要保存輸出,重定向到一個文件。
代碼:
[sam@chenwy sam]$ cat append.sed
#!/bin/sed -f
/company/ a/
Then suddenly it happed.

保存它,增加可執行權限,運行

代碼:
[sam@chenwy sam]chmod u+x append.sed
[sam@chenwy sam]$ ./append.sed quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Then suddenly it happed.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


或直接用命令行:
代碼:
[sam@chenwy sam]$ sed "/company/a/Then suddenly it happened." quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Then suddenly it happened.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.



[sam@chenwy sam]$ sed "/company/i/utter confusion followed." quote.txt
The honeysuckle band played all night long for only $90.
utter confusion followed.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

插入文本:
插入命令類似於附加命令,只是在指定行前面插入。和附加命令一樣,它也只接受一個地址。
如在a t t e n d a n c e結尾的行前插入文本utter confusion followed。
代碼:
[sam@chenwy sam]$ sed "/company/i/Utter confusion followed." quote.txt


也可以指定行:
代碼:
[sam@chenwy sam]$ cat insert.sed
#!/bin/sed -f
4 i/
Utter confusion followed.


執行結果
代碼:
[sam@chenwy sam]$ chmod u+x insert.sed
[sam@chenwy sam]$ ./insert.sed quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
Utter confusion followed.
The local nurse Miss P.Neave was in attendance.


修改文本
修改命令將在匹配模式空間的指定行用新文本加以替代,格式如下:
將 第一行The honeysuckle band played all night long for only $90替換爲The office Di b b l e band played well。首先要匹配第一行的任何部分,可使用模式‘ / H o n e y s u c k l e /’。s e d腳本文件爲c h a n g e . s e d。內容如下:
代碼:
[sam@chenwy sam]$ cat change.sed
#!/bin/sed -f
3 c/
The office Dibble band played well.


代碼:
[sam@chenwy sam]$ chmod u+x change.sed
[sam@chenwy sam]$ ./change.sed quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The office Dibble band played well.
The local nurse Miss P.Neave was in attendance.


或命令行:
代碼:
[sam@chenwy sam]$ sed "/honeysuck/c/The Office Dibble band played well." quote.txt
The Office Dibble band played well.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


可以對同一個腳本中的相同文件進行修改、附加、插入三種動作匹配和混合操作。
代碼:
[sam@chenwy sam]$ cat mix.sed
#!/bin/sed -f
1 c/
The Dibble band were grooving.

/evening/ i/
They played some great tunes.

3 a/
Where was the nurse to help?


代碼:
[sam@chenwy sam]$ chmod u+x mix.sed
[sam@chenwy sam]$ ./mix.sed quote.txt
The Dibble band were grooving.
They played some great tunes.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
Where was the nurse to help?
The local nurse Miss P.Neave was in attendance.


刪除文本
s e d刪除文本格式:
代碼:
[ a d d r e s s [,a d d r e s s ] ] d


刪除第一行;1 d意爲刪除第一行。
代碼:
[sam@chenwy sam]$ sed '1d' quote.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


刪除第一到第三行:
代碼:
[sam@chenwy sam]$ sed '1,3d' quote.txt
The local nurse Miss P.Neave was in attendance.


刪除最後一行:
代碼:
[sam@chenwy sam]$ sed '$d' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.


也可以使用正則表達式進行刪除操作。下面的例子刪除包含文本‘ N e a v e’的行。
代碼:
[sam@chenwy sam]$ sed '/Neave/d' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.


替換文本
替換命令用替換模式替換指定模式,格式爲:
代碼:
[ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]

s選項通知s e d這是一個替換操作,並查詢p a t t e r n - t o - f i n d,成功後用r e p l a c e m e n t - p a t t e r n替換它。
替換選項如下:

引用:
g 缺省情況下只替換第一次出現模式,使用g選項替換全局所有出現模式。
p 缺省s e d將所有被替換行寫入標準輸出,加p選項將使- n選項無效。- n選項不打印輸出結果。
w 文件名使用此選項將輸出定向到一個文件。


如替換n i g h t爲N I G H T,首先查詢模式n i g h t,然後用文本N I G H T替換它。
代碼:
[sam@chenwy sam]$ sed 's/night/NIGHT/' quote.txt
The honeysuckle band played all NIGHT long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


要從$ 9 0 中刪除$ 符號(記住這是一個特殊符號,必須用/ 屏蔽其特殊含義),在r e p l a c e m e n t - p a t t e r n部分不寫任何東西,保留空白,但仍需要用斜線括起來。在s e d中也可以這樣刪除一個字符串。
代碼:
[sam@chenwy sam]$ sed 's//$//' quote.txt
The honeysuckle band played all night long for only 90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


要進行全局替換,即替換所有出現模式,只需在命令後加g選項。下面的例子將所有T h e替換成Wo w!。
代碼:
[sam@chenwy sam]$ sed 's/The/Wow!/g' quote.txt
Wow! honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
Wow! local nurse Miss P.Neave was in attendance.


將替換結果寫入一個文件用w選項,下面的例子將s p l e n d i d替換爲S P L E N D I D的替換結果寫入文件s e d . o u t:
代碼:
[sam@chenwy sam]$ sed 's/splendid/SPLENDID/w sed.out' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of SPLENDID music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


注意要將文件名括在s e d的單引號裏。文件結果如下:
代碼:
[sam@chenwy sam]$ cat sed.out
It was an evening of SPLENDID music and company.

使用替換修改字符串
如果要附加或修改一個字符串,可以使用(&)命令,&命令保存發現模式以便重新調用它,然後把它放在替換字符串裏面。
先給出一個被替換模式,然後是一個準備附加在第一個模式後的另一個模式,並且後面帶有&,這樣修改模式將放在匹配模式之前。
例如, s e d語句s/nurse/"Hello"&/p 的結果如下
代碼:
[sam@chenwy sam]$ sed -n 's/nurse/"hello" &/p' quote.txt
The local "hello" nurse Miss P.Neave was in attendance.

原句是文本行The local nurse Miss P.Neave was in attendance。
記住模式中要使用空格,因爲輸出結果表明應加入空格。

還有一個例子:
代碼:
[sam@chenwy sam]$ sed -n 's/played/from Hockering &/p' quote.txt
The honeysuckle band from Hockering played all night long for only $90.

原句是The honeysuckle band played all night long for only $90。

將sed結果寫入文件命令
像使用>文件重定向發送輸出到一個文件一樣,在s e d命令中也可以將結果輸入文件。格式有點像使用替換命令:
代碼:
[ a d d r e s s [,address]]w filename

‘w’選項通知s e d將結果寫入文件。f i l e n a m e是自解釋文件名。
下面有兩個例子。
代碼:
[sam@chenwy sam]$ sed '1,2 w filedt' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


文件q u o t e . t x t輸出到屏幕。模式範圍即1,2行輸出到文件f i l e d t。
代碼:
[sam@chenwy sam]$ cat filedt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.


下面例子中查詢模式N e a v e,匹配結果行寫入文件f i l e d h t。
代碼:
[sam@chenwy sam]$ sed '/Neave/ w dht' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.


代碼:
[sam@chenwy sam]$ cat dht
The local nurse Miss P.Neave was in attendance.



從文件中讀文本
處理文件時, s e d允許從另一個文件中讀文本,並將其文本附加在當前文件。此命令放在模式匹配行後,格式爲:
代碼:
address r filename

這裏r通知s e d將從另一個文件源中讀文本。f i l e n a m e是其文件名。

現在創建一個小文件s e d e x . t x t,內容如下:
代碼:
[sam@chenwy sam]$ echo "Boom boom went the music" >sedex.txt
[sam@chenwy sam]$ cat sedex.txt
Boom boom went the music


將s e d e x . t x t內容附加到文件q u o t e . t x t的拷貝。在模式匹配行/ c o m p a n y /後放置附加文本。本例爲第三行。注意所讀的文件名需要用單引號括起來。
代碼:
[sam@chenwy sam]$ sed '/company./r sedex.txt' quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Boom boom went the music
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.



匹配後退出
有時需要在模式匹配首次出現後退出s e d,以便執行其他處理腳本。退出命令格式爲:
代碼:
address q

下面的例子假定查詢模式/ . a . * /,意爲任意字符後跟字符a,再跟任意字符0次或任意多次。
查詢首次出現模式,然後退出。需要將q放在s e d語句末尾。
代碼:
[sam@chenwy sam]$ sed '/.a.*/q' quote.txt
The honeysuckle band played all night long for only $90.

顯示文件中的控制字符

1、$vi dos.txt
進入vi後,用ctrl+v 再用ctrl+M產生控制字符^M不知對不對


使用cat -v filename命令查看編輯好的文件
代碼:
[sam@chenwy sam]$ cat -v dos.txt
12332##DISO##45.12^M
00332##LPSO##23.14^M
01299##USPD##34.46^M


s e d格式爲:
代碼:
[ a d d r e s s,[ a d d r e s s ] ] l

‘l’意爲列表。一般情況下要列出整個文件,而不是模式匹配行,因此使用l要從第一到最後一行。模式範圍1,$即爲此意。
代碼:
[sam@chenwy sam]$ sed -n '1,$l' dos.txt
12332##DISO##45.12/r$
00332##LPSO##23.14/r$
01299##USPD##34.46/r$


處理控制字符
使用s e d實現的一個重要功能是在另一個系統中下載的文件中剔除控制字符。
下面是傳送過來的文件( d o s . t x t)的部分腳本。必須去除所有可疑字符,以便於帳號所有者使用文件。

刪除所有的#字符很容易,可以使用全局替換命令。這裏用一個空格替換兩個或
更多的#符號。
代碼:
[sam@chenwy sam]$ sed 's/##/ /g' dos.txt
12332 DISO 45.12
00332 LPSO 23.14
01299 USPD 34.46


。刪除所有行首的0。使用^符號表示模式從行首開始, ^ 0 *表示行首任意個0。模式s / ^ 0 * / / g設置替換部分爲空,即爲刪除模式,正是要求所在。
代碼:
[sam@chenwy sam]$ sed 's/##/ /g;s/^0*/ /g' dos.txt
12332 DISO 45.12
332 LPSO 23.14
1299 USPD 34.46


最後去除行尾^ M符號,爲此需做全局替換。設置替換部分爲空。模式爲:
‘s / ^ m / / g’,注意‘^ M’,這是一個控制字符。
在命令行裏也必須用^M控制字符耶!?
代碼:
[sam@chenwy sam]$ sed 's/##/ /g;s/^0*/ /g;s/^M/ /g' dos.txt
12332 DISO 45.12
332 LPSO 23.14
1299 USPD 34.46



代碼:
[sam@chenwy sam]$ cat dos.txt | sed 's/^0*/ /g' | sed 's/^M/ /g' | sed 's/##/ /g'
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章