linux命令分析---SED (一)
2012-06-24 14:20
218 查看
本文转自http://maketecheasier.com/beginners-guide-to-sed-linux/2012/03/29
Aftergrep,thenextlogicalstepisatutorialonsed.ThesedcommandcomesfromStreamEDitor,andasitsnameindicates,itdealswithtextflux.However,ifsedisoneofthemostpowerfulcommandsinUnix,itsmanualpageisalsoamongthemostenigmatic.Iwilltryinthisarticletosummarizethemostbasicusageofsedandthengiveyouafewexamplesofadvancedscripts.
commandforsedissomethinglike:
Sedwillperformtheoperationsthatyouwanthimtodoonthetextfileanddisplaytheresultinthestandardoutput.Ifyouwanttheresultinatextfile,youcaneitherredirectitviathehabitualmethod:
Orusetheoption“
Nowlet’sbeginworkingonthescript.Themostobviousfirststepisthenullscript:
willjustdisplaythetextintest.txt.
Agoodusageofsedisdeletion.Let’spracticethroughexamples.
willdeletethelines2to4oftest.txt.
Youcanguessthatthesyntaxforthescriptis:
Butthefancypartcomeswhenyouuseregularexpressions,orregex,asdelimiterforthedeletion.Forexample,
willdeleteeverylinethatbeginswith“#”(inotherwords,ifyoucode,itwilldeleteallyourcomments).
Thegeneralsyntaxis
fordeletingthelinecontainingtheregex.
fordeletingtheintervalfromthelinecontainingregex1tothelinecontainingregex2.
Thespecialcharacter“^”thatIusedinthefirstexampleistoindicatethebeginningoftheline.
Then,thesecondbasicusagethatIcanthinkofissubstitution.Thegeneralsyntaxis:
Itwillhaveforeffecttosearchinthefirstlineforregex1,replaceitwithregex2,gotothenextlineandrepeatuntiltheendoftheentryflux.
Agoodexampleis:
Itwillreplacethesymbol“#”atthebeginningofaline,andalltheblankspaceswithnothing.Inotherterms,ituncommentsthetextfile.Thesymbol“*”isameta-characterdesigning0ormoreblankspaceshere.
Ifyouwanttodeletetheemptylinesofafile,youcanusethecommand
Themeta-character“$”meanstheendoftheline,so“^$”designsanemptyline.Then,“{N;D}”isarathercomplexsyntaxforsayingdeletethatline.
Ifyouwanttodeleteeverytaginahtmlfile,thisisthecommandforyou:
The“:start”iscalledalabel.Itisabitlikeatagwithinthescriptthatwewanttogobacktolaterinordertoapplymultiplechangestoasameline.sedsearchesforanythingoftheform“<XXX>”(theregex<[^>]*>)andreplacesitwithnothing,sothefirsthtmltagofthelineisdeleted.Butthen,beforegoingtothenextline,itchecksifthereissomethingelsebeginningwith“<”,andifthereis,itgoesbacktothelabel“:start”andre-appliesthetreatment.
Doyouknowanotherbasiccommandforsed?Ordoyouuseanotheradvancedscriptinvolvingsedthatyouwanttoshare?Pleaseletusknowinthecomments.
After
Basics
Thegeneralsed[option]'{script}'[textfile]
Sedwillperformtheoperationsthatyouwanthimtodoonthetextfileanddisplaytheresultinthestandardoutput.Ifyouwanttheresultina
sed[option]'{script}'[textfile]>[editedtextfile]
Orusetheoption“
-i”thatwilldirectlyedittheinputfile:
sed-i[option]'{script}'[textfile]
Nowlet’sbeginworkingonthescript.Themostobviousfirststepisthenullscript:
sed''test.txt
willjustdisplaythetextintest.
Agoodusageofsedisdeletion.Let’spracticethroughexamples.
sed'2,4d'test.txt
willdeletethelines2to4oftest.txt.
Youcanguessthatthesyntaxforthescriptis:
sed'[firstlinetodelete][lastlinetodelete]d'test.txt
Butthefancypartcomeswhenyouuse
sed'/^#/d'test.txt
willdeleteeverylinethatbeginswith“#”(inotherwords,ifyoucode,itwilldeleteallyourcomments).
Thegeneralsyntaxis
sed'/regex/d'test.txt
fordeletingthelinecontainingtheregex.
sed'/regex1/,/regex2/d'test.txt
fordeletingtheintervalfromthelinecontainingregex1tothelinecontainingregex2.
Thespecialcharacter“^”thatIusedinthefirstexampleistoindicatethebeginningoftheline.
Then,thesecondbasicusagethatIcanthinkofissubstitution.Thegeneralsyntaxis:
sed-re's/regex1/regex2/'test.txt
Itwillhaveforeffecttosearchinthefirstlineforregex1,replaceitwithregex2,gotothenextlineandrepeatuntiltheendoftheentryflux.
Agoodexampleis:
sed-re's/^#*//'test.txt
Itwillreplacethesymbol“#”atthebeginningofaline,andalltheblankspaceswithnothing.Inotherterms,ituncommentsthetextfile.Thesymbol“*”isameta-characterdesigning0ormoreblankspaceshere.
Advanced
Youcandosomeprettyfancystuffwithsed,butyouwillreachthelimitprettyfastifyoudon’tpayattentiontoitsbasicbehavior.Seddealswithfluxlinearly:Itappliesaline-by-linetreatmenttoatextfile.Ifyouwanttodomorethanonemodificationtoasameline,youhavetouselabelsandmulti-linetreatment.Allofthiscanbecomeverycomplex,veryquickly.Iwillnowshowyouafewadvancedexamplesandexplainthemtoyou.Ifyouwantmore,IamsurethatyoucansearchbyyourselfandusethebasicsIgaveyou.Ifyouwanttodeletetheemptylinesofafile,youcanusethecommand
sed-re'/^$/{N;D}'test.txt
Themeta-character“$”meanstheendoftheline,so“^$”designsanemptyline.Then,“{N;D}”isarathercomplexsyntaxforsayingdeletethatline.
Ifyouwanttodeleteeverytaginahtmlfile,thisisthecommandforyou:
sed-re':starts/<[^>]*>//g;/</{N;bstart}'test.txt
The“:start”iscalledalabel.Itisabitlikeatagwithinthescriptthatwewanttogobacktolaterinordertoapplymultiplechangestoasameline.sedsearchesforanythingoftheform“<XXX>”(theregex<[^>]*>)andreplacesitwithnothing,sothefirsthtmltagofthelineisdeleted.Butthen,beforegoingtothenextline,itchecksifthereissomethingelsebeginningwith“<”,andifthereis,itgoesbacktothelabel“:start”andre-appliesthetreatment.
Conclusion
Youarenowreadytostudymoredeeplysed,orjustuseitforsimplemodifications.ItisacommandthatIfindparticularlyusefulinscriptsingeneral,butittookmesometimetounderstanditssyntax.Ihopeitwillbemuchfasterforyou.Doyouknowanotherbasiccommandforsed?Ordoyouuseanotheradvancedscriptinvolvingsedthatyouwanttoshare?Pleaseletusknowinthecomments.
相关文章推荐
- linux命令分析---SED (二)
- 分析一例sed循环实例
- 日志分析查看——grep,sed,sort,awk运用
- Linux命令分析: locate
- 日志分析查看——grep,sed,sort,awk运用
- Linux命令系列十二之sed、awk
- 【Linux】日志分析工具grep sed sort
- shell学习十三天----sed案例分析
- Linux命令分析: chgrp
- 对一个sed"N;P;D"多行模式处理语句的分析
- 常用的查看、筛选、分析日志的Linux命令
- 《Linux命令、编辑器与Shell编程》读书笔记4.1-查找和筛选工具(grep,find,sed)
- linux命令:sed
- linux命令分析
- sed原理及p参数的运用的分析
- Linux命令――sed
- 日志分析查看——grep,sed,sort,awk运用
- sed结构分析 + awk结构分析
- 日志分析查看——grep,sed,sort,awk运用
- sed结构分析 + awk结构分析