http://witmax.cn/linux-sed-usage.htmlphp
http://www.linuxsir.org/bbs/showthread.php?t=189620html
Sed學習筆記linux
1. sed簡介git
sed 是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩衝區中,稱爲「模式空間」(pattern space),接着用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往屏幕。接着處理下一行,這樣不斷重複,直到文件末尾。文件內容並無 改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件;簡化對文件的反覆操做;編寫轉換程序等。如下介紹的是Gnu版本的Sed 3.02。正則表達式
2. 定址shell
能夠經過定址來定位你所但願編輯的行,該地址用數字構成,用逗號分隔的兩個行數表示以這兩行爲起止的行的範圍(包括行數表示的那兩行)。如1,3表示1,2,3行,美圓符號($)表示最後一行。範圍能夠經過數據,正則表達式或者兩者結合的方式肯定 。express
3. sed命令緩存
調用sed命令有兩種形式:
sed [options] ‘command’ file(s)
sed [options] -f scriptfile file(s)
a\
在當前行後面加入一行文本。
b lable
分支到腳本中帶有標記的地方,若是分支不存在則分支到腳本的末尾。
c\
用新的文本改變本行的文本。
d
從模板塊(Pattern space)位置刪除行。
D
刪除模板塊的第一行。
i\
在當前行上面插入文本。
h
拷貝模板塊的內容到內存中的緩衝區。
H
追加模板塊的內容到內存中的緩衝區
g
得到內存緩衝區的內容,並替代當前模板塊中的文本。
G
得到內存緩衝區的內容,並追加到當前模板塊文本的後面。
l
列表不能打印字符的清單。
n
讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。
N
追加下一個輸入行到模板塊後面並在兩者間嵌入一個新行,改變當前行號碼。
p
打印模板塊的行。
P(大寫)
打印模板塊的第一行。
q
退出Sed。
r file
從file中讀行。
t label
if分支,從最後一行開始,條件一旦知足或者T,t命令,將致使分支到帶有標號的命令處,或者到腳本的末尾。
T label
錯誤分支,從最後一行開始,一旦發生錯誤或者T,t命令,將致使分支到帶有標號的命令處,或者到腳本的末尾。
w file
寫並追加模板塊到file末尾。
W file
寫並追加模板塊的第一行到file末尾。
!
表示後面的命令對全部沒有被選定的行發生做用。
s/re/string/
用string替換正則表達式re。
=
打印當前行號碼。
#
把註釋擴展到下一個換行符之前。
如下的是替換標記bash
4. 選項編輯器
-e command, –expression=command
容許多臺編輯。
-h, –help
打印幫助,並顯示bug列表的地址。
-n, –quiet, –silent
取消默認輸出。
-f, –filer=script-file
引導sed腳本文件名。
-V, –version
打印版本和版權信息。
5. 元字符集^
錨定行的開始 如:/^sed/匹配全部以sed開頭的行。
$
錨定行的結束 如:/sed$/匹配全部以sed結尾的行。
.
匹配一個非換行符的字符 如:/s.d/匹配s後接一個任意字符,而後是d。
*
匹配零或多個字符 如:/*sed/匹配全部模板是一個或多個空格後緊跟sed的行。
[]
匹配一個指定範圍內的字符,如/[Ss]ed/匹配sed和Sed。
[^]
匹配一個不在指定範圍內的字符,如:/[^A-RT-Z]ed/匹配不包含A-R和T-Z的一個字母開頭,緊跟ed的行。
\(..\)
保存匹配的字符,如s/\(love\)able/\1rs,loveable被替換成lovers。
&
保存搜索字符用來替換其餘字符,如s/love/**&**/,love這成**love**。
\<
錨定單詞的開始,如:/\<love/匹配包含以love開頭的單詞的行。
\>
錨定單詞的結束,如/love\>/匹配包含以love結尾的單詞的行。
x\{m\}
重複字符x,m次,如:/0\{5\}/匹配包含5個o的行。
x\{m,\}
重複字符x,至少m次,如:/o\{5,\}/匹配至少有5個o的行。
x\{m,n\}
重複字符x,至少m次,很少於n次,如:/o\{5,10\}/匹配5–10個o的行。
6. 實例
刪除:d命令
*
$ sed ’2d’ example—–刪除example文件的第二行。
*
$ sed ’2,$d’ example—–刪除example文件的第二行到末尾全部行。
*
$ sed ‘$d’ example—–刪除example文件的最後一行。
*
$ sed ‘/test/’d example—–刪除example文件全部包含test的行。
替換:s命令
*
$ sed ‘s/test/mytest/g’ example—–在整行範圍內把test替換爲mytest。若是沒有g標記,則只有每行第一個匹配的test被替換成mytest。
*
$ sed -n ‘s/^test/mytest/p’ example—–(-n)選項和p標誌一塊兒使用表示只打印那些發生替換的行。也就是說,若是某一行開頭的test被替換成mytest,就打印它。
*
$ sed ‘s/^192.168.0.1/&localhost/’ example—–&符號表示替換換字符串中被找到的部份。全部以192.168.0.1開頭的行都會被替換成它自已加 localhost,變成192.168.0.1localhost。
*
$ sed -n ‘s/\(love\)able/\1rs/p’ example—–love被標記爲1,全部loveable會被替換成lovers,並且替換的行會被打印出來。
*
$ sed ‘s#10#100#g’ example—–不論什麼字符,緊跟着s命令的都被認爲是新的分隔符,因此,「#」在這裏是分隔符,代替了默認的「/」分隔符。表示把全部10替換成100。
選定行的範圍:逗號
*
$ sed -n ‘/test/,/check/p’ example—–全部在模板test和check所肯定的範圍內的行都被打印。
*
$ sed -n ’5,/^test/p’ example—–打印從第五行開始到第一個包含以test開始的行之間的全部行。
*
$ sed ‘/test/,/check/s/$/sed test/’ example—–對於模板test和west之間的行,每行的末尾用字符串sed test替換。
多點編輯:e命令
*
$ sed -e ’1,5d’ -e ‘s/test/check/’ example—–(-e)選項容許在同一行裏執行多條命令。如例子所示,第一條命令刪除1至5行,第二條命令用check替換test。命令的執 行順序對結果有影響。若是兩個命令都是替換命令,那麼第一個替換命令將影響第二個替換命令的結果。
*
$ sed –expression=’s/test/check/’ –expression=’/love/d’ example—–一個比-e更好的命令是–expression。它能給sed表達式賦值。
從文件讀入:r命令
*
$ sed ‘/test/r file’ example—–file裏的內容被讀進來,顯示在與test匹配的行後面,若是匹配多行,則file的內容將顯示在全部匹配行的下面。
寫入文件:w命令
*
$ sed -n ‘/test/w file’ example—–在example中全部包含test的行都被寫入file裏。
追加命令:a命令
*
$ sed ‘/^test/a\\—>this is a example’ example<—–’this is a example’被追加到以test開頭的行後面,sed要求命令a後面有一個反斜槓。
插入:i命令
$ sed ‘/test/i\\
new line
————————-’ example
若是test被匹配,則把反斜槓後面的文本插入到匹配行的前面。
下一個:n命令
*
$ sed ‘/test/{ n; s/aa/bb/; }’ example—–若是test被匹配,則移動到匹配行的下一行,替換這一行的aa,變爲bb,並打印該行,而後繼續。
變形:y命令
*
$ sed ’1,10y/abcde/ABCDE/’ example—–把1–10行內全部abcde轉變爲大寫,注意,正則表達式元字符不能使用這個命令。
退出:q命令
*
$ sed ’10q’ example—–打印完第10行後,退出sed。
保持和獲取:h命令和G命令
*
$ sed -e ‘/test/h’ -e ‘$G example—–在sed處理文件的時候,每一行都被保存在一個叫模式空間的臨時緩衝區中,除非行被刪除或者輸出被取消,不然全部被處理的行都將 打印在屏幕上。接着模式空間被清空,並存入新的一行等待處理。在這個例子裏,匹配test的行被找到後,將存入模式空間,h命令將其複製並存入一個稱爲保 持緩存區的特殊緩衝區內。第二條語句的意思是,當到達最後一行後,G命令取出保持緩衝區的行,而後把它放回模式空間中,且追加到如今已經存在於模式空間中 的行的末尾。在這個例子中就是追加到最後一行。簡單來講,任何包含test的行都被複制並追加到該文件的末尾。
保持和互換:h命令和x命令
*
$ sed -e ‘/test/h’ -e ‘/check/x’ example —–互換模式空間和保持緩衝區的內容。也就是把包含test與check的行互換。
7. 腳本
Sed腳本是一個sed的命令清單,啓動Sed時以-f選項引導腳本文件名。Sed對於腳本中輸入的命令很是挑剔,在命令的末尾不能有任何空白或文本,若是在一行中有多個命令,要用分號分隔。以#開頭的行爲註釋行,且不能跨行。
8. 小技巧
在sed的命令行中引用shell變量時要使用雙引號,而不是一般所用的單引號。下面是一個根據name變量的內容來刪除named.conf文件中zone段的腳本:name=’zone\ 「localhost」‘
sed 「/$name/,/};/d」 named.conf
做者:Jims of 肥肥世家 <jims.yang@gmail.com >
Copyright © 2004,2005, 本文聽從GNU 的自由文檔許可證(Free Document License)的條款,歡迎轉載、修改、散佈。
***********分***********隔***********線***********
sed新手使用進階全功略
看了各個linux 論壇的帖子,感受sed的介紹很多,但有點零亂,在這裏整理一下,但願能對學習者有所幫助!
注:sed使用中除了多個命令時, -e選項通常能夠省略!
基礎:
正則表達式(Regular Expression)
在學習sed前,首先了解RE的基本知識,大致上最基本也須要知道下面這些,若是不瞭解正則表達式,那麼您將很難進階
引用:
- 錨點(anchor) 用以標識 RE 於句子中的位置所在. 常見有: ^: 表示句首. 如 ^abc 表示以 abc 開首的句子. $: 表示句尾. 如 abc$ 表示以 abc 結尾的句子. \<: 表示詞首. 如 \<abc 表示以 abc 開首的詞. \>: 表示詞尾. 如 abc\> 表示以 abc 結尾的詞.- 修飾字符(modifier) 獨立表示時自己不具意義, 專門用以修改前一個 char. set 的出現次數. 常見有: *: 表示前一個 char. set 的出現次數為 0 或屢次. 如 ab*c 表示 a 與 c 之間可有 0 或多個 b 存在. ?: 表示前一個 char. set 的出現次數為 0 或 1 次. 如 ab?c 表示 a 與 c 之間可有 0 或 1 個 b 存在. +: 表示前一個 char. set 的出現次數為 1 或屢次. 如 ab+c 表示 a 與 c 之間可有 1 或多個 b 存在. {n}: 表示前一個 char. set 的出現次數必須為 n 次. 如 ab{3,}c 表示 a 與 c 之間必須有 3 個 b 存在.{n,}: 表示前一個 char. set 的出現次數至少為 n 次. 如 ab{3,}c 表示 a 與 c 之間至少有 3 個 b 存在. {n,m}: 表示前一個 char. set 的出現次數為 n 到 m 次. 如 ab{3,5}c 表示 a 與 c 之間有 3 到 5 個 b 存在. |
. : 匹配任意一個字符(1個)
.*:匹配任意多個字符(1或多個)
sed全功略
1.sed用法介紹
sed是一個非交互性文本流編輯器。它編輯文件或標準輸入導出的文本拷貝。
• 抽取域。
• 匹配正則表達式。
• 比較域。
• 增長、附加、替換。
• 基本的sed命令和一行腳本。
不管命令是什麼, sed並不與初始化文件打交道,它操做的只是一個拷貝,而後全部的改動若是沒有重定向到一個文件,將輸出到屏幕。
基本格式:
代碼:
1
2
|
sed [-n] [-e] 'command' file(s)
sed [-n] -f scriptfile file(s)
|
(1)sed怎樣讀取數據
s e d從文件的一個文本行或從標準輸入的幾種格式中讀取數據,將之拷貝到一個編輯緩衝區,而後讀命令行或腳本的第一條命令,並使用這些命令查找模式或定位行號編輯它。重複此過程直到命令結束。
(2)調用sed
調用s e d有三種方式:
a.在命令行鍵入命令; sed [選項] s e d命令輸入文件
b.將s e d命令插入腳本文件,而後調用s e d; sed [選項] -f sed腳本文件輸入文件
c.將s e d命令插入腳本文件,並使s e d腳本可執行。 sed腳本文件 [選項] 輸入文件
2.sed選項
s e d選項以下:
-n 不打印;s e d不寫編輯行到標準輸出,缺省爲打印全部行(編輯和未編輯)。p命令能夠用來打印編輯行。
-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命令的文件。
-c 下一命令是編輯命令。使用多項編輯時加入此選項。若是隻用到一條s e d命令,此選項無用,但指定它也沒有關係。
-i 編輯原文件(此選項慎用,若是使用則原文件就會被修改,沒法恢復)。
3.保存sed輸出
重定向(下面將sed命令的全部輸出至文件 output-file 中)
代碼:
1
|
zhyfly@zhyfly:~$ sed 'some-sed-commands' input-file>output-file
|
w 寫文本到一個文件
代碼:
1
|
[address [,address]]w filename
|
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
|
zhyfly@zhyfly:~/bash$ cat test.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.
zhyfly@zhyfly:~/bash$ sed -e '1,2w test.bak' test.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.
zhyfly@zhyfly:~/bash$ cat test.bak
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
|
r 從另外一個文件中讀文本
代碼:
1
|
address r filename
|
4.sed基礎用法
使用sed在文件中查詢文本的方式:
sed瀏覽輸入文件時,缺省從第一行開始,有兩種方式定位文本:
a. 使用行號,能夠是一個簡單數字,或是一個行號範圍。
b. 使用正則表達式。
例:
x #爲一行號,如1
x,y #表示行號範圍從x到y,如2,5表示從第2行到第5行
/pattern/ #查詢包含模式的行。例如/disk/或/[a-z]/
/pattern/pattern/ #查詢包含兩個模式的行。例如/disk/disks/
/pattern/,x #在給定行號上查詢包含模式的行。如/ribbon/,3
x,/pattern/ #經過行號和模式查詢匹配行。3,/vdu/
x,y! #查詢不包含指定行號x和y的行。1,2!
基本sed編輯命令:
sed編輯命令
p 打印匹配行
= 顯示文件行號
a\ 在定位行號後附加新文本信息
i\ 在定位行號後插入新文本信息
d 刪除定位行
c\ 用新文本替換定位文本
s 使用替換模式替換相應模式
r 從另外一個文件中讀文本
w 寫文本到一個文件
q 第一個模式匹配完成後推出或當即推出
l 顯示與八進制A S C I I代碼等價的控制字符
{ } 在定位行執行的命令組
n 從另外一個文件中讀文本下一行,並附加在下一行
g 將模式2粘貼到/pattern n/
y 傳送字符
n 延續到下一輸入行;容許跨行的模式匹配語句
舉例:
代碼:
1
2
3
4
5
|
zhyfly@zhyfly:~/bash$ cat test.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
2
3
4
5
6
7
8
|
zhyfly@zhyfly:~/bash$ sed -e '1p' test.txt
The honeysuckle band played all night long for only $90.
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.
zhyfly@zhyfly:~/bash$ sed -n -e '1p' test.txt
The honeysuckle band played all night long for only $90.
|
代碼:
1
2
|
zhyfly@zhyfly:~/bash$ sed -n -e '2p' test.txt
It was an evening of splendid music and company.
|
代碼:
1
2
3
|
zhyfly@zhyfly:~/bash$ sed -n -e '2,3p' test.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
|
代碼:
1
2
|
zhyfly@zhyfly:~/bash$ sed -n -e '/company/p' test.txt
It was an evening of splendid music and company.
|
代碼:
1
2
3
|
zhyfly@zhyfly:~/bash$ sed -n -e '2,/23:00/p' test.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
|
代碼:
1
2
3
|
zhyfly@zhyfly:~/bash$ sed -n -e '2,3!p' test.txt
The honeysuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendance.
|
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
zhyfly@zhyfly:~/bash$ sed -e '=' test.txt
1
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '=' test.txt
1
2
3
4
zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' test.txt
It was an evening of splendid music and company.
zhyfly@zhyfly:~/bash$ sed -n -e '/music/=' test.txt
2
zhyfly@zhyfly:~/bash$ sed -n -e '/music/p' -e '/music/=' test.txt
It was an evening of splendid music and company.
2
zhyfly@zhyfly:~/bash$ sed -e '=;p' test.txt
1
The honeysuckle band played all night long for only $90.
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '=;p' test.txt
1
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -n -e '=' -e 'p' test.txt
1
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:00.
4
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$
|
附加 [address]a\附加內容 #缺省放在每一行後面
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
zhyfly@zhyfly:~/bash$ sed -e 'a\this line will be added to the end of each line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
this line will be added to the end of each line!oooooooooo
It was an evening of splendid music and company.
this line will be added to the end of each line!oooooooooo
Too bad the disco floor fell through at 23:00.
this line will be added to the end of each line!oooooooooo
The local nurse Miss P.Neave was in attendance.
this line will be added to the end of each line!oooooooooo
zhyfly@zhyfly:~/bash$ sed -e '/music/a\this line will be added to the end of the matching line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
this line will be added to the end of the matching line!oooooooooo
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
|
插入 [address]i\插入內容 #缺省放在每一行前面
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
zhyfly@zhyfly:~/bash$ sed -e 'i\this line will be inserted to the begin of each line!oooooooooo' test.txt
this line will be inserted to the begin of each line!oooooooooo
The honeysuckle band played all night long for only $90.
this line will be inserted to the begin of each line!oooooooooo
It was an evening of splendid music and company.
this line will be inserted to the begin of each line!oooooooooo
Too bad the disco floor fell through at 23:00.
this line will be inserted to the begin of each line!oooooooooo
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '/music/i\this line will be inserted to the begin of the matching line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
this line will be inserted to the begin of the matching line!oooooooooo
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.
|
更改行 [address]c\更改後整行內容 #缺省修改每一行
代碼:
01
02
03
04
05
06
07
08
09
10
|
zhyfly@zhyfly:~/bash$ sed -e 'c\this line will be modified to the each line!oooooooooo' test.txt
this line will be modified to the each line!oooooooooo
this line will be modified to the each line!oooooooooo
this line will be modified to the each line!oooooooooo
this line will be modified to the each line!oooooooooo
zhyfly@zhyfly:~/bash$ sed -e '/music/c\this line will be modified to the matching line!oooooooooo' test.txt
The honeysuckle band played all night long for only $90.
this line will be modified to the matching line!oooooooooo
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
|
刪除定位行
代碼:
1
2
3
4
5
6
7
8
9
|
zhyfly@zhyfly:~/bash$ cat test.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.
zhyfly@zhyfly:~/bash$ sed -e '/music/d' test.txt
The honeysuckle band played all night long for only $90.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.
|
5.sed高級用法—替換
替換
1
2
3
4
|
[address]s/old/new/g
[address]s/pattern/replacement #the first occurrence on the address(缺省全部行)
[address]s/pattern/replacement/g #all occurrences on the address(缺省全部行)
|
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
zhyfly@zhyfly:~/bash$ cat test.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.
zhyfly@zhyfly:~/bash$ sed -e 's/h/ooooo/' test.txt
Toooooe honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad toooooe disco floor fell through at 23:00.
Toooooe local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e 's/h/ooooo/g' test.txt
Toooooe ooooooneysuckle band played all nigooooot long for only $90.
It was an evening of splendid music and company.
Too bad toooooe disco floor fell tooooorougooooo at 23:00.
Toooooe local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '1,2s/h/ooooo/' test.txt
Toooooe 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.
zhyfly@zhyfly:~/bash$ sed -e '1,2s/h/ooooo/g' test.txt
Toooooe ooooooneysuckle band played all nigooooot 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.
zhyfly@zhyfly:~/bash$ sed -e '/\$/s/h/ooooo/g' test.txt
Toooooe ooooooneysuckle band played all nigooooot 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.
zhyfly@zhyfly:~/bash$ sed -e '/^It/,/23:00/s/l/ooooo/g' test.txt
The honeysuckle band played all night long for only $90.
It was an evening of spoooooendid music and company.
Too bad the disco fooooooor feoooooooooo through at 23:00.
The local nurse Miss P.Neave was in attendance.
|
使用替換修改字符串 &
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
zhyfly@zhyfly:~/bash$ cat test.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.
zhyfly@zhyfly:~/bash$ sed -e 's/$90/&230/g' test.txt
The honeysuckle band played all night long for only $90230.
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.
zhyfly@zhyfly:~/bash$ sed -e 's/90/120,&/g' test.txt
The honeysuckle band played all night long for only $120,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
2
3
4
|
zhyfly@zhyfly:~/bash$ echo $PWD
/home/zhyfly/bash
zhyfly@zhyfly:~/bash$ echo $PWD|sed -e 's:/:@:g'
@home@zhyfly@bash
|
規則表達式混亂
題目
1
2
|
zhyfly@zhyfly:~/bash$ cat test
This</b> is what <b>I</b> meant.
|
想要獲得的答案:
This is what I meant.
錯誤解法:
代碼:
1
2
3
4
|
zhyfly@zhyfly:~/bash$ cat test
This</b> is what <b>I</b> meant.
zhyfly@zhyfly:~/bash$ sed -e 's/<.*>//g' test
This meant.
|
正確解法:
代碼:
1
2
|
zhyfly@zhyfly:~/bash$ sed -e 's/<[^>]*>//g' test
This is what I meant.
|
題目
要用sed把字符「\」轉化成「’」該怎麼寫?
錯誤解法:
代碼:
01
02
03
04
05
06
07
08
09
10
|
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"
hello\hh\haha
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/'/g'
>
> bash: unexpected EOF while looking for matching `''
bash: syntax error: unexpected end of file
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/\'/g'
>
> bash: unexpected EOF while looking for matching `''
bash: syntax error: unexpected end of file
|
正確解法:
代碼:
1
2
3
|
zhyfly@zhyfly:~/bash$ echo "hello\hh\haha"|sed -e 's/\\/'"'"'/g'
hello'hh'haha
zhyfly@zhyfly:~/bash$
|
你看出來了嗎?
更多字符匹配:
‘[a-x]*’
這將匹配零或多個所有爲 ‘a’、’b'、’c'…’v'、’w'、’x’ 的字符。另外,可使用 ‘[:space:]‘ 字符類來匹配空格。
如下是可用字符類的至關完整的列表:
字符類 描述
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
|
[:alnum:] 字母數字 [a-z A-Z 0-9]
[:alpha:] 字母 [a-z A-Z]
[:blank:] 空格或製表鍵
[:cntrl:] 任何控制字符
[:digit:] 數字 [0-9]
[:graph:] 任何可視字符(無空格)
[:lower:] 小寫 [a-z]
[:print:] 非控制字符
[:punct:] 標點字符
[:space:] 空格
[:upper:] 大寫 [A-Z]
[:xdigit:] 十六進制數字 [0-9 a-f A-F]
|
域
關於域應用一個較複雜的例子講解
題目:
[file1.txt]
1C2
1C3
1C31
1C32
1C4
2C3
2C4
1D1
1D10
1D12
1D2
1D3
1D31
1RC2
1RC20
1RC21
1RC3
1RC31
1WR1
1WR2
1WR20
1WR21
1WR23
排序後
[file2.txt]
1 C 2
1 C 3
2 C 3
1 C 4
2 C 4
1 C 31
1 C 32
1 D 1
1 D 2
1 D 3
1 D 10
1 D 12
1 D 31
1 RC 2
1 RC 3
1 RC 20
1 RC 21
1 RC 31
1 WR 1
1 WR 2
1 WR 20
1 WR 21
1 WR 23
規律:將每行分紅三部分: 「數字1」 「字符串」 「數字2」(即三個域)
第1、三字段按numberic順序排序,中間部分按字母排序 ,優先級順序2 3 1
解答思路:
首先須要將文件的每行分紅三個域,這就利用到sed的分域功能,能夠這樣分(其中包含分域的格式):
代碼:
1
|
/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3/
|
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
zhyfly@zhyfly:~/bash$ cat file1.txt
1C2
1C3
1C31
1C32
1C4
2C3
2C4
1D1
1D10
1D12
1D2
1D3
1D31
1RC2
1RC20
1RC21
1RC3
1RC31
1WR1
1WR2
1WR20
1WR21
1WR23
|
分域後
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt
1 C 2
1 C 3
1 C 31
1 C 32
1 C 4
2 C 3
2 C 4
1 D 1
1 D 10
1 D 12
1 D 2
1 D 3
1 D 31
1 RC 2
1 RC 20
1 RC 21
1 RC 3
1 RC 31
1 WR 1
1 WR 2
1 WR 20
1 WR 21
1 WR 23
|
排序後
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
zhyfly@zhyfly:~/bash$ sed -e 's/^\([0-9]*\)\([A-Z]*\)\([0-9]*\)/\1 \2 \3 /g' file1.txt|sort +1 -2 +2n +0 -1
1 C 2
1 C 3
2 C 3
1 C 4
2 C 4
1 C 31
1 C 32
1 D 1
1 D 2
1 D 3
1 D 10
1 D 12
1 D 31
1 RC 2
1 RC 3
1 RC 20
1 RC 21
1 RC 31
1 WR 1
1 WR 2
1 WR 20
1 WR 21
1 WR 23
|
下面的例子比較複雜:
題目
文件內容file.txt:
123456 345678 2005-05-06 123456
123456 234567 2003-5-6 234567
345555 987644 2003-4-23 543333
555555 999999 2004-11-5 999999
要將第四列數據變成正常的年月日,將2003-5-6 變成2003-05-0;
2003-4-23變成2003-04-23; 2004-11-5變成 2004-11-05
解答
首先將須要改變的部分分域
代碼:
1
2
|
/-\([0-9]\)-/-0\1-/ #月
/-\([0-9]\) /-0\1 / #日
|
注意實現起來又有多種方法:
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
zhyfly@zhyfly:~/bash$ cat file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-5-6 234567
345555 987644 2003-4-23 543333
555555 999999 2004-11-5 999999
zhyfly@zhyfly:~/bash$ sed -e 's/-\([0-9]\)-/-0\1-/g' -e 's/-\([0-9]\) /-0\1 /g' file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-05-06 234567
345555 987644 2003-04-23 543333
555555 999999 2004-11-05 999999
zhyfly@zhyfly:~/bash$ sed -e 's/-\([0-9]\)-/-0\1-/g;s/-\([0-9]\) /-0\1 /g' file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-05-06 234567
345555 987644 2003-04-23 543333
555555 999999 2004-11-05 999999
zhyfly@zhyfly:~/bash$ sed -e :a -e 's/-\([0-9]\)\([- ]\)/-0\1\2/;ta' file.txt
123456 345678 2005-05-06 123456
123456 234567 2003-05-06 234567
345555 987644 2003-04-23 543333
555555 999999 2004-11-05 999999
|
注:
:a – label
ta – goto to :a to rerun sed if the last sed is finished successfully.
簡單說, 就是循環。
多個命令經常使用的方法
主要有
a.利用多個-e選項
b.利用 ;
c.使用腳本文件
代碼:
01
02
03
04
05
06
07
08
09
10
|
zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g' -e '2,3s/d/ddddd/g' test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -e '2,3s/a/ooooo/g;2,3s/d/ddddd/g' test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.
|
前兩種方法比較簡單,下面重點講一下第三種方法:
建立sed腳本文件–>賦予執行權限–>執行文件
代碼:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
zhyfly@zhyfly:~/bash$ cat sub.sed
#!/bin/sed -f
2,3s/a/ooooo/g
2,3s/d/ddddd/g
zhyfly@zhyfly:~/bash$ sudo chmod +x sub.sed
zhyfly@zhyfly:~/bash$ cat test.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.
zhyfly@zhyfly:~/bash$ ./sub.sed test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.
zhyfly@zhyfly:~/bash$ sed -f sub.sed test.txt
The honeysuckle band played all night long for only $90.
It wooooos ooooon evening of splendddddiddddd music ooooonddddd compooooony.
Too boooooddddd the dddddisco floor fell through ooooot 23:00.
The local nurse Miss P.Neave was in attendance.
|