1. sed命令簡介
sed是非互動編輯器。它不會修改檔案,除非使用shell重新導向來儲存結果。預設情況下,所有的輸出行都被列印到螢幕上。
sed編輯器逐行處理檔案(或輸入),並將結果發送到螢幕。具體過程如下:首先sed把當前正在處理的行儲存在一個臨時緩衝區中(也稱為模式空間),然後處理臨時緩衝區中的行,完成後把該行發送到螢幕上。sed每處理完一行就將其從臨時緩衝區刪除,然後將下一行讀入,進行處理和顯示。處理完輸入檔案的最後一行後,sed便結束運行。sed把每一行都存在臨時緩衝區中,對這個副本進行編輯,所以不會修改原檔案。 2. sed命令格式
sed命令的一般格式為:sed [選項] “指令碼” 檔案
(下文中“命令”都是指sed的命令部分。)
選項:
sed的選項形如-n, -e, -f等,都有個'-'。
-n 忽略預設輸出(預設輸出就是整個檔案的內容)。
-e 執行多個編輯任務
-f 執行檔案中的命令集合
指令碼:
sed的指令碼有p, d, i\等,文中會重點介紹。
如果指令碼部分為空白,則可以用來列印一個檔案的內容:
[root@ubuntu]sed_test:$ sed "" sed_test.txt
1 It's been a long day without you my friend
2 And I'll tell you all about it when I seeyou again
3 We've come a long way from where we began
4 Oh I'll tell you all about it when I seeyou again
5 When I see you again
sed支援Regex:
|
元字元 |
功能 |
|
^ |
行首定位器 |
|
$ |
行尾定位器 |
|
. |
匹配除分行符號以外的單個字元 |
|
* |
匹配零個或多個前置字元 |
|
\? |
匹配零個或1個前置字元 |
|
\+ |
匹配1個或多個前置字元 |
|
\| |
或操作 |
|
\s |
匹配單個空白字元(如\t) |
|
\S |
匹配單個非空白字元 |
|
\w |
匹配單個單詞 |
|
\W |
匹配單個非單詞 |
|
[] |
匹配指定字元組內的任一字元 |
|
[^] |
匹配不在指定字元組內的任一字元 |
|
[-] |
匹配字元集的字元範圍 |
|
(..) |
儲存已匹配的字元(括弧前面要加反斜線\,csdn沒顯示出來) |
|
& |
儲存尋找串以便在替換串中引用 |
|
\< |
詞首定位器 |
|
\> |
詞尾定位器 |
|
x\{m\} |
連續m個x |
|
x\{m,\} |
至少連續m個x |
|
x\{m,n\} |
至少連續m個,但不超過n個x |
|
(str) |
Regex中對字串的引用(括弧前面要加反斜線\,csdn沒顯示出來) |
|
中括號運算式 |
如[:alnum:],[:alpha:],[:digit:],[:lower:],
[:upper:],[:punct:],[:space:] |
3. sed選項和命令介紹
選項和(指令碼中的)命令我會穿插在一起介紹,大家注意區分帶“-”的是選項,否則是命令。 p 列印行
例:上面的sed_test.txt檔案為例。
列印第3行的內容:
[root@ubuntu]sed_test:$sed -n "3p" sed_test.txt
3 We've come a long way from where we began
把3和p分開也可以,並且更能分辨出p是個命令。
注意加-n選項是為了不顯示預設輸出,如果這裡不加-n,效果是這樣的:
[root@ubuntu]sed_test:$sed "3p" sed_test.txt
1 It's been a long day without you my friend
2 And I'll tell you all about it when I see you again
3 We've come a longway from where we began
3 We've come a longway from where we began
4 Oh I'll tell you all about it when I see you again
5 When I see you again
即既列印出所有內容,又列印出了紅色部分的內容。由紅色部分的位置可知,sed是進行行操作的。
如下,addr1,addr2、addr1,+N、addr1~step、addr1,~N可以表示定址範圍。
列印1-3行的內容(注意行數是從1開始的):
[root@ubuntu]sed_test:$sed -n "1,3p" sed_test.txt
1 It's been a long day without you my friend
2 And I'll tell you all about it when I see you again
3 We've come a long way from where we began
列印第三行及後續2行:
[root@ubuntu]sed_test:$sed -n "3,+2p" sed_test.txt
3 We've come a long way from where we began
4 Oh I'll tell you all about it when I see you again
5 When I see you again
列印第2行以及4-10行:
[root@ubuntu]sed_test:$sed -n "2p;4,10p" sed_test.txt
2 And I'll tell you all about it when I see you again
4 Oh I'll tell you all about it when I see you again
5 When I see you again
“first~step”形式表示從first行作為0計數起點,列印step倍數行的內容。
列印第3行開始2的倍數行,即3,5,7 ...行的內容:
[root@ubuntu]sed_test:$sed -n "3~2p" sed_test.txt
那麼列印所有奇數行:
[root@ubuntu]sed_test:$sed -n "1~2p" sed_test.txt
“addr1,~N”形式表示從addr1行開始,到找到N的倍數行結束。
從第13行開始,到5的倍數行結束,這裡即第15行結束:
[root@ubuntu]sed_test:$sed -n "13,~5p" sed_test.txt /regexp/、\%regexp% 匹配行
用來尋找匹配Regexregexp的行。
例如,列印包含“again”的行:
[root@ubuntu]sed_test:$ sed -n "/again/p" see_you_again.lrc
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
如果需要匹配斜杠“/”,則需要用反斜線轉義“\/”。
\%regexp%的形式仍然是匹配行,其中%可以替換為任一字元。這種形式可以用在regexp裡包含斜杠“/”的情況,這樣就不需要用反斜線轉義。 /regexp/I、\%regexp%I 匹配行(忽略大小寫)
後面添加大寫的I,表示匹配時忽略大小寫。 d 刪除行
刪除2-4行的內容:
[root@ubuntu]sed_test:$sed "2,4d" sed_test.txt
1 It's been a long day without you my friend
5 When I see you again
注意-n的作用就體現出來了,這裡不能加-n。注意,這並沒有修改檔案本身的內容。
移除檔案中的空行:
[root@ubuntu]sed_test:$ sed "/^$/d" sed_test.txt
刪除包含“We”和“When”的行以及他們之間的行:
[root@ubuntu]sed_test:$sed "/We/,/When/d" see_you_again.lrc
It's been a long day without you my friend
And I'll tell you all about it when I see you again Regex
舉例:see_you_again.lrc的內容如下:
[root@ubuntu]sed_test:$sed '' see_you_again.lrc
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We'vecome a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again
列印包含“again”的行:
[root@ubuntu]sed_test:$sed -n "/again/p" see_you_again.lrc
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
列印不以“We”開頭的行:
[root@ubuntu]sed_test:$sed -n "/^We/! p" see_you_again.lrc
It'sbeen a long day without you my friend
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
在kallsyms檔案中尋找包含“[gmac]”的行,並在這些行中找到不以83aa開頭的行:
列印以“again”結尾的行:
[root@ubuntu]sed_test:$sed -n "/again$/p" see_you_again.lrc
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
列印包含“I” “you”,中間為任意多個字元的行:
[root@ubuntu]sed_test:$sed -n "/I.*you/p" see_you_again.lrc
It's been a long day without you my friend
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
列印包含“I”,但後面不跟字母的行:
[root@ubuntu]sed_test:$sed -n "/I[^a-zA-Z]/p" see_you_again.lrc
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
列印包含2個連續的“e”字母的行:
[root@ubuntu]sed_test:$sed -n "/e\{2\}/p" see_you_again.lrc
It's been a long day without you my friend
And I'll tell you all about it when I see you again
Oh I'll tell you all about it when I see you again
When I see you again
注意,上面都是針對單個字元的匹配,如果要匹配一個字串,要加小括弧:
列印包含1到2個連續“bang”的行:
另外需要注意的是,中括弧[]裡都是匹配單個字元的,無法匹配一個字串。
列印包含以“wh”開頭的單詞的行:
[root@ubuntu]sed_test:$sed -n "/\<wh/p" see_you_again.lrc
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again s 用一個字串替換另一個字串 g 在行內進行全域替換 i 替換時不區分大小寫 (…) 對字串分組
將包含“you”的行中的“you”替換為“her”:
[root@ubuntu]sed_test:$sed -n "s/you/her/gp" see_you_again.lrc
It's been a long day without her my friend
And I'll tell her all about it when I see her again
Oh I'll tell her all about it when I see her again
When I see her again
將包含“you”的行中的“you”替換為“her”,只替換每行的第一處:
[root@ubuntu]sed_test:$sed -n "s/you/her/p" see_you_again.lrc
It's been a long day without her my friend
And I'll tell her all about it when I see you again
Oh I'll tell her all about it when I see you again
When I see her again
將包含“long”的行中的“long”替換為“tough”,不區分大小寫:
[root@ubuntu]sed_test:$sed -n "s/LOng/tough/pi" see_you_again.lrc
It's been a tough day without you my friend
We've come a tough way from where we began
可以用(…)對字串進行分組,並且在後面使用1,2,3…依次代替匹配列表,例如,將“long”和“way”中間加一個“-”:
將4-5行的“you”替換為“her”:
[root@ubuntu]sed_test:$sed -n "4,5s/you/her/gp" see_you_again.lrc
Oh I'll tell her all about it when I see her again
When I see her again
將包含“long”的行中的空格替換為逗號:
[root@ubuntu]sed_test:$sed -n "/long/s/ /,/gp" see_you_again.lrc
It's,been,a,long,day,without,you,my,friend
We've,come,a,long,way,from,where,we,began
匹配包含“friend”以及“Oh”的行,並同時列印出他們之間的所有行(應用所有匹配行):
[root@ubuntu]sed_test:$sed -n "/friend/,/Oh/ p" see_you_again.lrc
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
如果逗號前面的模式沒有匹配行,則從第1行開始列印;如果逗號後面的模式沒有匹配行,則列印到最後一行。
列印包含“friend”的行,並同時列印所有匹配行的後續2行:
[root@ubuntu]sed_test:$sed -n "/friend/,+2 p" see_you_again.lrc
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
可見“,”號和“+”號和前面講p命令時有著相同的意義。
分隔字元預設為正斜杠,但可以改變。無論什麼字元(分行符號除外),只要緊跟s命令,就成了新的串分隔字元。如下面的字元z:
[root@ubuntu]sed_test:$sed -n "4,5szyouzherzgp" see_you_again.lrc
Oh I'll tell her all about it when I see her again