sed 學習筆記(與大家共勉)(轉)

post0發表於2007-08-11
sed 學習筆記(與大家共勉)(轉)[@more@]

[b:4c6c2a6554]sed 學習筆記(與大家共勉)[/b:4c6c2a6554]

[b:4c6c2a6554]宣告:[/b:4c6c2a6554]這些程式碼只是為了學習和理解sed命令而為之,並不代表問題的唯一解或最佳解,希望各位[b:4c6c2a6554]拍磚[/b:4c6c2a6554]

參考資料:

[b:4c6c2a6554][color=red:4c6c2a6554]一. 替換[/color:4c6c2a6554][/b:4c6c2a6554]

[b:4c6c2a6554]1.神奇變換([color=red:4c6c2a6554]y[/color:4c6c2a6554]命令的使用)[/b:4c6c2a6554][code:1:4c6c2a6554] sed 'y/ori_letter_list/target_letter_list/' filename[/code:1:4c6c2a6554]

[code:1:4c6c2a6554]cat filename

1234567890

2345678901

3456789012

4567890123[/code:1:4c6c2a6554]測試

將檔案中[color=red:4c6c2a6554]1[/color:4c6c2a6554]換成[color=indigo:4c6c2a6554]A[/color:4c6c2a6554]

將檔案中[color=red:4c6c2a6554]2[/color:4c6c2a6554]換成[color=indigo:4c6c2a6554]B[/color:4c6c2a6554]

...

將檔案中[color=red:4c6c2a6554]0[/color:4c6c2a6554]換成[color=indigo:4c6c2a6554]J[/color:4c6c2a6554]

[code:1:4c6c2a6554]sed 'y/1234567890/ABCDEFGHIJ/' filename

ABCDEFGHIJ

BCDEFGHIJA

CDEFGHIJAB

DEFGHIJABC[/code:1:4c6c2a6554]注意變換關係是按兩個list的位置對應變換,y是一個管局命令,拒絕使用字尾flag/[color=red:4c6c2a6554]g[/color:4c6c2a6554]

list1:1234567890

list2:ABCDEFGHIJ

下面再作一個與前例相反的變換

[code:1:4c6c2a6554]sed 'y/0987654321/ABCDEFGHIJ/' filename

JIHGFEDCBA

IHGFEDCBAJ

HGFEDCBAJI

GFEDCBAJIH[/code:1:4c6c2a6554]

[b:4c6c2a6554]2.替換每行第一個匹配[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed 's/regexpr/anyword/' filename

sed 's/regexpr/anyword/1' filename[/code:1:4c6c2a6554]

舉例:

[quote:4c6c2a6554]cat filename

1234567890 2345678901

3456789012 4567890123

sed 's/5/五/' filename

1234五67890 234[color=green:4c6c2a6554]5[/color:4c6c2a6554]678901

34五6789012 4[color=green:4c6c2a6554]5[/color:4c6c2a6554]67890123[/quote:4c6c2a6554]

[b:4c6c2a6554]3.替換每行第n(如果有的話)個匹配[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed "s/regexpr/anyword/${n}" filename

cat filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444

舉例

sed "s/4/ 四 /8" filename

111111111111111111

222222222222222222

333333333333333333

4444444 四 4444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554]4.替換每行所有匹配[/b:4c6c2a6554]

[code:1:4c6c2a6554]cat filename

1234567890 2345678901

3456789012 4567890123[/code:1:4c6c2a6554]

[code:1:4c6c2a6554]舉例:

sed 's/3/三/g' filename

12三4567890 2三45678901

三456789012 456789012三[/code:1:4c6c2a6554]

[b:4c6c2a6554][color=red:4c6c2a6554]二.行號處理[/color:4c6c2a6554][/b:4c6c2a6554]

[b:4c6c2a6554][color=blue:4c6c2a6554]1.[/color:4c6c2a6554]為檔案加行號[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed = filename|sed 'N;s/ /:/'

cat filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]sed = filename|sed 'N;s/ /:/' filename

1:111111111111111111

2:222222222222222222

3:333333333333333333

4:444444444444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554]2.僅為檔案中的正文行加行號[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed /./= a|sed '/./N;s/ /:/'[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]cat filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444

sed /./= a|sed '/./N;s/ /:/' filename

1:111111111111111111

3:222222222222222222

4:333333333333333333

6:444444444444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554][color=red:4c6c2a6554]三.字串翻轉[/color:4c6c2a6554][/b:4c6c2a6554]

[code:1:4c6c2a6554]sed '/ /!G;s/(.)(.* )/&21/;s/.//'[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]echo 1234567890|sed '/ /!G;s/(.)(.* )/&21/;s/.//'

0987654321[/code:1:4c6c2a6554]

[b:4c6c2a6554][color=red:4c6c2a6554]四.選擇性輸出[/color:4c6c2a6554][/b:4c6c2a6554]

[b:4c6c2a6554]1.列印文件奇數行(隔行輸出)[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed 'n;d'

sed 'x;$!N;x'

sed -n 'p;n'

1

3

5

7[/code:1:4c6c2a6554]

[b:4c6c2a6554]2.列印偶數行(隔行輸出)[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed -n 'n;p'

sed '1d;n;d;'

2

4

6

8[/code:1:4c6c2a6554]

[b:4c6c2a6554]3.刪除連續重複行(大量使用了pattern space 檔案太大時要注意)[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed '$!N; /^(.*) 1$/!P; D'

#使用 $!N 要當心記憶體溢位[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]cat file

111111111111111111

222222222222222222

222222222222222222

333333333333333333

444444444444444444

444444444444444444

444444444444444444

444444444444444444

444444444444444444

sed '$!N; /^(.*) 1$/!P; D' filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554]4.合併上下行並以空格相分隔[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed '$!N;s/ / /'[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]cat file

1234567890

0987654321

執行命令後

1234567890 0987654321[/code:1:4c6c2a6554]

[b:4c6c2a6554]5.將以符號結尾的行與下行合併並以空格分隔(拼接斷行)[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed -e :a -e '/$/N; s/ / /; ta'[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]cat filename

1 111111111111111111

2 222222222222222222

3 333333333333333333

4 444444444444444444

sed -e :a -e '/$/N; s/ / /; ta' filename

1 111111111111111111 2 222222222222222222

3 333333333333333333 4 444444444444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554]6.按關鍵字拼接行[/b:4c6c2a6554]

如果某行以=開始,則合併到上一行並替代=為空格

[code:1:4c6c2a6554]sed -e :a -e '$!N;s/ =/ /;ta' -e 'P;D'[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]cat file

111111111111111111

222222222222222222

=333333333333333333

444444444444444444

sed -e :a -e '$!N;s/ =/ /;ta' -e 'P;D' filename

111111111111111111

222222222222222222 333333333333333333

444444444444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554]7.輸出匹配行的下一行[/b:4c6c2a6554]

[code:1:4c6c2a6554]sed -n '/regexpr/{n;p;}' filename[/code:1:4c6c2a6554]

舉例

[code:1:4c6c2a6554]cat filename

1 111111111111111111

2 222222222222222222

3 333333333333333333

4 444444444444444444

sed -n '/^3/{n;p;}' filename

4 444444444444444444[/code:1:4c6c2a6554]

[b:4c6c2a6554]8.顯示匹配行的行號並輸出匹配行的上行、匹配行、下行[/b:4c6c2a6554]

sed -n -e '/regexpr/{=;x;1!p;g;$!N;p;D;}' -e h

舉例

[code:1:4c6c2a6554]cat filename

1 111111111111111111

2 222222222222222222

3 333333333333333333

4 444444444444444444

sed -n -e '/^3/{=;x;1!p;g;$!N;p;D;}' -e h filename

3 #匹配行的行號

2 222222222222222222 #上一行

3 333333333333333333 #匹配行

4 444444444444444444 #下一行[/code:1:4c6c2a6554]

[b:4c6c2a6554]9.刪除文件中某標誌區域內的關鍵字匹配行[/b:4c6c2a6554]

刪除文件中從being開到end結束的塊中包含myword的行

[code:1:4c6c2a6554]sed '/^begin/,/^end/{/myword/d;}' filename[/code:1:4c6c2a6554]

[quote:4c6c2a6554]cat filename

myword

begin

[color=blue:4c6c2a6554]myword

Number!

myword

Number!

myword

Number!

myword

Number![/color:4c6c2a6554]

end

myword

Number![/quote:4c6c2a6554]測試

[quote:4c6c2a6554]myword

begin

[color=darkblue:4c6c2a6554]Number!

Number!

Number!

Number![/color:4c6c2a6554]

end

myword

Number![/quote:4c6c2a6554]

[b:4c6c2a6554][color=red:4c6c2a6554]五.字串解析[/color:4c6c2a6554][/b:4c6c2a6554]

[b:4c6c2a6554]1.從字串中解析出兩個子串(前2各字元和後9個字元)[/b:4c6c2a6554]

[code:1:4c6c2a6554]echo "WeLoveChinaUnix"|sed -e 'H;s/(..).*/1/;x;s/.*(.{9})$/1/;x;G;s/ / /'

We ChinaUnix[/code:1:4c6c2a6554]

[b:4c6c2a6554]2.分解日期串[/b:4c6c2a6554]

[code:1:4c6c2a6554]echo 20030922|sed 's/(....)(..)(..)/1 2 3/'|read year month day

echo $year $month $day

2003 09 22[/code:1:4c6c2a6554

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944851/,如需轉載,請註明出處,否則將追究法律責任。

相關文章