亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 1053988 | 回復(fù): 391
打印 上一主題 下一主題

[學(xué)習(xí)共享] shell基礎(chǔ)二十篇 [復(fù)制鏈接]

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2004-11-26 10:16 |只看該作者 |倒序?yàn)g覽
本帖最后由 rdcwayx 于 2012-05-03 10:01 編輯

編者按:由 wingger  整理的 shell基礎(chǔ)十二篇 以及L_kernel補(bǔ)充的第十三——二十篇,涉及shell 編程及使用的各個(gè)方面,又附有大量的例子,極適合初學(xué)者系統(tǒng)學(xué)習(xí)。如果配合網(wǎng)中人的shell 十三問(wèn)? ,效果更加明顯。

這里是其中的第十章 sed.  其他各章可察看相應(yīng)的 link.  


shell基礎(chǔ)1:文件安全與權(quán)限
http://72891.cn/thread-434579-1-1.html

附:Linux的用戶和用戶組管理
http://72891.cn/thread-438660-1-1.html

shell基礎(chǔ)二:查找技巧,find及xargs的使用
http://72891.cn/thread-441883-1-1.html

shell基礎(chǔ)三和四:后臺(tái)(crontab,at,&,nohup)及(*,?,[]等)
http://72891.cn/thread-442596-1-1.html

shell基礎(chǔ)五:輸入和輸出(echo,read,cat,管道,tee,重定向等)
http://72891.cn/thread-444209-1-1.html

shell基礎(chǔ)六七:命令執(zhí)行順序(||及&&,{}及())和正則表?
http://72891.cn/thread-445229-1-1.html

shell基礎(chǔ)八:文本過(guò)濾工具(grep)
http://72891.cn/thread-446683-1-1.html

shell基礎(chǔ)九:awk
http://72891.cn/thread-448687-1-1.html

shell基礎(chǔ)十:sed
http://72891.cn/thread-452942-1-1.html

shell基礎(chǔ)11:文件分類、合并和分割(sort,uniq,join,cut,paste,split)
http://72891.cn/thread-457730-1-1.html

shell基礎(chǔ)十二:tr
http://72891.cn/thread-459099-1-1.html

感謝L_kernel補(bǔ)充的第十三——二十篇(cjaizss添加)
shell基礎(chǔ)第十三篇-登錄環(huán)境
http://72891.cn/thread-1820174-1-1.html

shell基礎(chǔ)第十四篇-環(huán)境和shell變量
http://72891.cn/thread-1820194-1-1.html

shell基礎(chǔ)第十五篇-引號(hào)
http://72891.cn/thread-1820220-1-1.html

shell基礎(chǔ)第十六篇-shell腳本介紹
http://72891.cn/thread-1820301-1-1.html

shell基礎(chǔ)學(xué)習(xí)第十七篇-條件測(cè)試
http://72891.cn/thread-1820304-1-1.html

shell基礎(chǔ)第十八篇-控制流結(jié)構(gòu)
http://72891.cn/thread-1820329-1-1.html

shell學(xué)習(xí)基礎(chǔ)第十九篇-shell函數(shù)
http://72891.cn/thread-1820454-1-1.html

shell基礎(chǔ)學(xué)習(xí)第二十篇-向腳本傳遞參數(shù)
http://72891.cn/thread-1823335-1-1.html

其它我的筆記都在我的BLOG上,呵呵,有興趣的可以去看看

shell基礎(chǔ)十:sed


sed 用法介紹

s e d是一個(gè)非交互性文本流編輯器。它編輯文件或標(biāo)準(zhǔn)輸入導(dǎo)出的文本拷貝。

" 抽取域。
" 匹配正則表達(dá)式。
" 比較域。
" 增加、附加、替換。
" 基本的s e d命令和一行腳本。


可以在命令行輸入s e d命令,也可以在一個(gè)文件中寫(xiě)入命令,然后調(diào)用s e d,這與a w k基本相同。使用s e d需要記住的一個(gè)重要事實(shí)是,無(wú)論命令是什么, s e d并不與初始化文件打交道,它操作的只是一個(gè)拷貝,然后所有的改動(dòng)如果沒(méi)有重定向到一個(gè)文件,將輸出到屏幕。

因?yàn)閟 e d是一個(gè)非交互性編輯器,必須通過(guò)行號(hào)或正則表達(dá)式指定要改變的文本行。
本文介紹s e d用法和功能。本章大多編寫(xiě)的是一行命令和小腳本。這樣做可以慢慢加深對(duì)s e d用法的了解,取得寶貴的經(jīng)驗(yàn),以便最終自己編出大的復(fù)雜s e d腳本。
和g r e p與a w k一樣, s e d是一種重要的文本過(guò)濾工具,或者使用一行命令或者使用管道與g r e p與a w k相結(jié)合。

1 sed怎樣讀取數(shù)據(jù)

s e d從文件的一個(gè)文本行或從標(biāo)準(zhǔn)輸入的幾種格式中讀取數(shù)據(jù),將之拷貝到一個(gè)編輯緩沖區(qū),然后讀命令行或腳本的第一條命令,并使用這些命令查找模式或定位行號(hào)編輯它。重復(fù)此過(guò)程直到命令結(jié)束。

2 調(diào)用sed

調(diào)用s e d有三種方式:在命令行鍵入命令;將s e d命令插入腳本文件,然后調(diào)用s e d;將s e d命令插入腳本文件,并使s e d腳本可執(zhí)行。

使用s e d命令行格式為:
  1. sed [選項(xiàng)] s e d命令輸入文件。
復(fù)制代碼
記住在命令行使用s e d命令時(shí),實(shí)際命令要加單引號(hào)。s e d也允許加雙引號(hào)。

使用s e d腳本文件,格式為:
  1. sed [選項(xiàng)] -f sed腳本文件輸入文件
復(fù)制代碼
要使用第一行具有s e d命令解釋器的s e d腳本文件,其格式為:
  1. s e d腳本文件[選項(xiàng)] 輸入文件
復(fù)制代碼
不管是使用s h e l l命令行方式或腳本文件方式,如果沒(méi)有指定輸入文件, s e d從標(biāo)準(zhǔn)輸入中接受輸入,一般是鍵盤或重定向結(jié)果。

s e d選項(xiàng)如下:
n 不打印;s e d不寫(xiě)編輯行到標(biāo)準(zhǔn)輸出,缺省為打印所有行(編輯和未編輯)。p命令可以用來(lái)打印編輯行。
c 下一命令是編輯命令。使用多項(xiàng)編輯時(shí)加入此選項(xiàng)。如果只用到一條s e d命令,此選項(xiàng)無(wú)用,但指定它也沒(méi)有關(guān)系。
f 如果正在調(diào)用s e d腳本文件,使用此選項(xiàng)。此選項(xiàng)通知s e d一個(gè)腳本文件支持所有的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輸出
由于不接觸初始化文件,如果想要保存改動(dòng)內(nèi)容,簡(jiǎn)單地將所有輸出重定向到一個(gè)文件即可。下面的例子重定向s e d命令的所有輸出至文件‘ m y o u t f i l e’,當(dāng)對(duì)結(jié)果很滿意時(shí)使用這種方法。
  1. $sed 'some-sed-commands' input-file > myoutfile
復(fù)制代碼
2.2 使用sed在文件中查詢文本的方式
s e d瀏覽輸入文件時(shí),缺省從第一行開(kāi)始,有兩種方式定位文本:
1) 使用行號(hào),可以是一個(gè)簡(jiǎn)單數(shù)字,或是一個(gè)行號(hào)范圍。
2 ) 使用正則表達(dá)式


下面是使用s e d定位文本的一些方式。
  1. x x為一行號(hào),如1
  2. x , y 表示行號(hào)范圍從x到y(tǒng),如2,5表示從第2行到第5行
  3. / p a t t e r n / 查詢包含模式的行。例如/ d i s k /或/[a-z]/
  4. / p a t t e r n / p a t t e r n / 查詢包含兩個(gè)模式的行。例如/ d i s k / d i s k s /
  5. p a t t e r n / , x 在給定行號(hào)上查詢包含模式的行。如/ r i b b o n / , 3
  6. x , / p a t t e r n / 通過(guò)行號(hào)和模式查詢匹配行。3 . / v d u /
  7. x , y ! 查詢不包含指定行號(hào)x和y的行。1 , 2 !
復(fù)制代碼
2.3 基本sed編輯命令
  1. sed編輯命令
  2. p 打印匹配行
  3. = 顯示文件行號(hào)
  4. a \ 在定位行號(hào)后附加新文本信息
  5. i \ 在定位行號(hào)后插入新文本信息
  6. d 刪除定位行
  7. c \ 用新文本替換定位文本
  8. s 使用替換模式替換相應(yīng)模式
  9. r 從另一個(gè)文件中讀文本
  10. w 寫(xiě)文本到一個(gè)文件
  11. q 第一個(gè)模式匹配完成后推出或立即推出
  12. l 顯示與八進(jìn)制A S C I I代碼等價(jià)的控制字符
  13. { } 在定位行執(zhí)行的命令組
  14. n 從另一個(gè)文件中讀文本下一行,并附加在下一行
  15. g 將模式2粘貼到/pattern n/
  16. y 傳送字符
  17. n 延續(xù)到下一輸入行;允許跨行的模式匹配語(yǔ)句
復(fù)制代碼
sed和正則表達(dá)式

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

[ 本帖最后由 mocou 于 2005-11-18 14:40 編輯 ]

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
2 [報(bào)告]
發(fā)表于 2004-11-26 11:13 |只看該作者

shell基礎(chǔ)十二篇

s e d例子中使用下述文本文件q u o t e . t x t。
  1. [sam@Linux_chenwy sam]$ cat quote.txt
  2. The honeysuckle band played all night long for only $90.
  3. It was an evening of splendid music and company.
  4. Too bad the disco floor fell through at 23:00.
  5. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


1 使用p(rint)顯示行
只打印第二行,用-n
  1. [sam@Linux_chenwy sam]$ sed -n '2p' quote.txt
  2. It was an evening of splendid music and company.
復(fù)制代碼


2 打印范圍
可以指定行的范圍,現(xiàn)打印1到3行,用逗號(hào)分隔行號(hào)。
  1. [sam@Linux_chenwy sam]$ sed -n '1,3p' quote.txt
  2. The honeysuckle band played all night long for only $90.
  3. It was an evening of splendid music and company.
  4. Too bad the disco floor fell through at 23:00.
復(fù)制代碼


3 打印模式
假定要匹配單詞N e a v e,并打印此行,方法如下。使用模式/ p a t t e r n /格式,這里為/ N e a v e /。
  1. [sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt
  2. The honeysuckle band played all night long for only $90.
  3. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


4 使用模式和行號(hào)進(jìn)行查詢
可以將行號(hào)和模式結(jié)合使用。假定要改動(dòng)文件q u o t e . t x t最后一行中的單詞t h e,使用s e d查詢t h e,返回兩行:
  1. [sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt
  2. The honeysuckle band played all night long for only $90.
  3. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


使用模式與行號(hào)的混合方式可以剔除第一行,格式為l i n e _ n u m b e r, / p a t t e r n /。逗號(hào)用來(lái)分隔行號(hào)與模式開(kāi)始部分。為達(dá)到預(yù)期結(jié)果,使用4 , / t h e /。意即只在第四行查詢模式t h e,命令如下:
  1. [sam@Linux_chenwy sam]$ sed -n '4,/The/p' quote.txt
  2. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼

上面有錯(cuò),其實(shí)是把第四行后的都打出來(lái)了
這個(gè)模式應(yīng)該哪果指定行找不到符合條件的,就從下一行開(kāi)始查找,直到找到為止,并把,找到行之前的全部打打印出來(lái)。
如果指定行本身就符合條伯,把本行及后面的行的全部打印出來(lái)


5 匹配元字符
匹配元字符$前,必須使用反斜線\屏蔽其特殊含義。模式為/\$/ p。
  1. [sam@Linux_chenwy sam]$ sed -n '/\$/p' quote.txt
  2. The honeysuckle band played all night long for only $90.
復(fù)制代碼


6 顯示整個(gè)文件
要打印整個(gè)文件,只需將行范圍設(shè)為第一行到最后一行1 , $。$意為最后一行。
  1. [sam@Linux_chenwy sam]$ sed -n '1,$p' quote.txt
  2. The honeysuckle band played all night long for only $90.
  3. It was an evening of splendid music and company.
  4. Too bad the disco floor fell through at 23:00.
  5. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


7 任意字符
匹配任意字母,后跟任意字母的0次或多次重復(fù),并以i n g結(jié)尾,模式為/ . * i n g /?梢允褂眠@個(gè)模式查詢以i n g結(jié)尾的任意單詞。
  1. [sam@Linux_chenwy sam]$ sed -n '/.*ing/p' quote.txt
  2. It was an evening of splendid music and company.
復(fù)制代碼


8 首行
要打印文件第一行,使用行號(hào):
  1. [sam@Linux_chenwy sam]$ sed -n '1p' quote.txt
  2. The honeysuckle band played all night long for only $90.
復(fù)制代碼


9 最后一行
要打印最后一行,使用$。$是代表最后一行的元字符。
  1. [sam@Linux_chenwy sam]$ sed -n '$p' quote.txt
  2. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


10 打印行號(hào)
要打印行號(hào),使用等號(hào)=。打印模式匹配的行號(hào),使用格式/ p a t t e r n / =。
  1. [sam@Linux_chenwy sam]$ sed -e '/music/=' quote.txt
  2. The honeysuckle band played all night long for only $90.
  3. 2
  4. It was an evening of splendid music and company.
  5. Too bad the disco floor fell through at 23:00.
  6. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


整個(gè)文件都打印出來(lái),并且匹配行打印了行號(hào)。如果只關(guān)心實(shí)際行號(hào),使用- e選項(xiàng)。
  1. [sam@Linux_chenwy sam]$ sed -n '/music/=' quote.txt
  2. 2
復(fù)制代碼


如果只打印行號(hào)及匹配行,必須使用兩個(gè)s e d命令,并使用e選項(xiàng)。第一個(gè)命令打印模式匹配行,第二個(gè)使用=選項(xiàng)打印行號(hào),格式為sed -n -e /pattern/p -e /pattern/=
  1. [sam@Linux_chenwy sam]$ sed -n -e '/music/p' -e '/music/='  quote.txt
  2. It was an evening of splendid music and company.
  3. 2
復(fù)制代碼

論壇徽章:
1
榮譽(yù)會(huì)員
日期:2011-11-23 16:44:17
3 [報(bào)告]
發(fā)表于 2004-11-26 12:30 |只看該作者

shell基礎(chǔ)十二篇

終于等到sed啦,好好學(xué)學(xué)
P.S:新版本的sed(GNU sed version 4.0.5)帶有-i選項(xiàng),允許直接編輯修改文件內(nèi)容而不需要重定向到臨時(shí)文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/home/lee#cat file
new
abc

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
4 [報(bào)告]
發(fā)表于 2004-11-26 14:29 |只看該作者

shell基礎(chǔ)十二篇

原帖由 "寂寞烈火" 發(fā)表:
終于等到sed啦,好好學(xué)學(xué)
P.S:新版本的sed(GNU sed version 4.0.5)帶有-i選項(xiàng),允許直接編輯修改文件內(nèi)容而不需要重定向到臨時(shí)文件
例如:
/home/lee#cat file
old
abc
/home/lee#sed -i 's/old/new/' file
/..........


這個(gè)不錯(cuò),呵呵,

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2004-11-26 16:02 |只看該作者

shell基礎(chǔ)十二篇

可等到wingger JJ的Sed啦...多教我哦...

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
6 [報(bào)告]
發(fā)表于 2004-11-26 16:13 |只看該作者

shell基礎(chǔ)十二篇

[quote]原帖由 "雙眼皮的豬"]可等到wingger JJ的Sed啦...多教我哦...[/quote 發(fā)表:


     等著別人教俺

11 附加文本

要附加文本,使用符號(hào)a \,可以將指定文本一行或多行附加到指定行。如果不指定文本放置位置, s e d缺省放在每一行后面。附加文本時(shí)不能指定范圍,只允許一個(gè)地址模式。文本附加操作時(shí),結(jié)果輸出在標(biāo)準(zhǔn)輸出上。注意它不能被編輯,因?yàn)閟 e d執(zhí)行時(shí),首先將文件的一行文本拷貝至緩沖區(qū),在這里s e d編輯命令執(zhí)行所有操作(不是在初始文件上),因?yàn)槲谋局苯虞敵龅綐?biāo)準(zhǔn)輸出,s e d并無(wú)拷貝。
要想在附加操作后編輯文本,必須保存文件,然后運(yùn)行另一個(gè)s e d命令編輯它。這時(shí)文件的內(nèi)容又被移至緩沖區(qū)。
附加操作格式如下:
  1. [address]a\
  2. text\
  3. text\
  4. ......
  5. text
復(fù)制代碼


地址指定一個(gè)模式或行號(hào),定位新文本附加位置。a\ 通知s e d對(duì)a \后的文本進(jìn)行實(shí)際附加操作。觀察格式,注意每一行后面有一斜劃線,這個(gè)斜劃線代表?yè)Q行。s e d執(zhí)行到這兒,將創(chuàng)建一新行,然后插入下一文本行。最后一行不加斜劃線, s e d假定這是附加命令結(jié)尾。

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

論壇徽章:
1
榮譽(yù)會(huì)員
日期:2011-11-23 16:44:17
7 [報(bào)告]
發(fā)表于 2004-11-26 16:35 |只看該作者

shell基礎(chǔ)十二篇

文革MM的學(xué)習(xí)精神深深感染了大家!向文革MM致敬!

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
8 [報(bào)告]
發(fā)表于 2004-11-27 14:15 |只看該作者

shell基礎(chǔ)十二篇

http://www.chinalinuxpub.com/doc/oreillybookself/unix/sedawk/index.htm

這個(gè)不錯(cuò)哦,awk&sed,嘻嘻

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
9 [報(bào)告]
發(fā)表于 2004-11-27 17:25 |只看該作者

shell基礎(chǔ)十二篇

創(chuàng)建sed腳本文件

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

保存它,增加可執(zhí)行權(quán)限,運(yùn)行

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


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



[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.

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


也可以指定行:
  1. [sam@chenwy sam]$ cat insert.sed
  2. #!/bin/sed -f
  3. 4 i\
  4. Utter confusion followed.
復(fù)制代碼


執(zhí)行結(jié)果
  1. [sam@chenwy sam]$ chmod u+x insert.sed
  2. [sam@chenwy sam]$ ./insert.sed quote.txt
  3. The honeysuckle band played all night long for only $90.
  4. It was an evening of splendid music and company.
  5. Too bad the disco floor fell through at 23:00.
  6. Utter confusion followed.
  7. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


修改文本
修改命令將在匹配模式空間的指定行用新文本加以替代,格式如下:
將第一行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。內(nèi)容如下:
  1. [sam@chenwy sam]$ cat change.sed
  2. #!/bin/sed -f
  3. 3 c\
  4. The office Dibble band played well.
復(fù)制代碼

  1. [sam@chenwy sam]$ chmod u+x change.sed
  2. [sam@chenwy sam]$ ./change.sed quote.txt
  3. The honeysuckle band played all night long for only $90.
  4. It was an evening of splendid music and company.
  5. The office Dibble band played well.
  6. The local nurse Miss P.Neave was in attendance.
復(fù)制代碼


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


可以對(duì)同一個(gè)腳本中的相同文件進(jìn)行修改、附加、插入三種動(dòng)作匹配和混合操作。
  1. [sam@chenwy sam]$ cat mix.sed
  2. #!/bin/sed -f
  3. 1 c\
  4. The Dibble band were grooving.

  5. /evening/ i\
  6. They played some great tunes.

  7. 3 a\
  8. Where was the nurse to help?
復(fù)制代碼

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


刪除文本
s e d刪除文本格式:
  1. [ a d d r e s s [,a d d r e s s ] ] d
復(fù)制代碼


刪除第一行;1 d意為刪除第一行。
  1. [sam@chenwy sam]$ sed '1d' quote.txt
  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.
復(fù)制代碼


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


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


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


替換文本
替換命令用替換模式替換指定模式,格式為:
  1. [ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n]
復(fù)制代碼

s選項(xiàng)通知s e d這是一個(gè)替換操作,并查詢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替換它。
替換選項(xiàng)如下:

g 缺省情況下只替換第一次出現(xiàn)模式,使用g選項(xiàng)替換全局所有出現(xiàn)模式。
p 缺省s e d將所有被替換行寫(xiě)入標(biāo)準(zhǔn)輸出,加p選項(xiàng)將使- n選項(xiàng)無(wú)效。- n選項(xiàng)不打印輸出結(jié)果。
w 文件名使用此選項(xiàng)將輸出定向到一個(gè)文件。


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


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


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


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


注意要將文件名括在s e d的單引號(hào)里。文件結(jié)果如下:
  1. [sam@chenwy sam]$ cat sed.out
  2. It was an evening of SPLENDID music and company.
復(fù)制代碼

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2004-11-28 14:55 |只看該作者

shell基礎(chǔ)十二篇

佩服,佩服!
以后要向你多請(qǐng)教。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP