Sed.docx
- 文档编号:27690529
- 上传时间:2023-07-04
- 格式:DOCX
- 页数:7
- 大小:16.38KB
Sed.docx
《Sed.docx》由会员分享,可在线阅读,更多相关《Sed.docx(7页珍藏版)》请在冰豆网上搜索。
Sed
Sed...
Sed-AnIntroductionLastupdateSunSep2419:
44:
00EDT2006
ThankstoKeelanEvansforspottingsometypos.
ThankstoWimStolkeraswell.TableofContents
TheAwfulTruthaboutsed
Theessentialcommand:
sforsubstitution
Theslashasadelimiter
Using&asthematchedstring
Using1tokeeppartofthepattern
SubstituteFlags
/g-Globalreplacement
Issedrecursive?
/1,/2,etc.Specifyingwhichoccurrence
/p-print
Writetoafilewith/wfilename
Combiningsubstitutionflags
Argumentsandinvocationofsed
Multiplecommandswith-ecommand
Filenamesonthecommandline
sed-n:
noprinting
sed-fscriptname
sedinshellscript
QuotingmultiplesedlinesintheCshell
QuotingmultiplesedlinesintheBourneshell
Asedinterpreterscript
SedComments
Passingargumentsintoasedscript
Usingsedinashellhere-isdocument
Multiplecommandsandorderofexecution
AddressesandRangesofText
Restrictingtoalinenumber
Patterns
Rangesbylinenumber
Rangesbypatterns
Deletewithd
Printingwithp
Reversingtherestrictionwith!
Relationshipsbetweend,p,and!
Theqorquitcommand
Groupingwith{and}
Writingafilewiththe‘w‘command
Readinginafilewiththe‘r‘command
SunOSandthe#CommentCommand
Adding,Changing,Insertingnewlines
Appendalinewith‘a‘
Insertalinewith‘i‘
Changealinewith‘c‘
Leadingtabsandspacesinasedscript
Addingmorethanoneline
Addinglinesandthepatternspace
Addressrangesandtheabovecommands
Multi-LinePatterns
Printlinenumberwith=
Transformwithy
Displayingcontrolcharacterswithal
WorkingwithMultipleLines
Usingnewlinesinsedsecripts
TheHoldBuffer
Exchangewithx
ExampleofContextGrep
HoldwithhorH
Keepingmorethanonelineintheholdbuffer
GetwithgorG
FlowControl
Testingwitht
Analternatewayofaddingcomments
Thepoorlyundocumented;
Passingregularexpressionsasarguments
CommandSummary
InConclusion
Copyright2001,2005BruceBarnettandGeneralElectricCompany
Allrightsreserved
Youareallowedtoprintcopiesofthistutorialforyourpersonal
use,andlinktothispage,butyouarenotallowedtomakeelectronic
copies,orredistributethistutorialinanyformwithoutpermission.IntroductiontoSed
Howtousesed,aspecialeditorformodifyingfilesautomatically.
Ifyouwanttowriteaprogramtomakechangesinafile,sedisthe
tooltouse.
ThereareafewprogramsthataretherealworkhorseintheUnix
toolbox.
Theseprogramsaresimpletouseforsimpleapplications,yethavea
richsetofcommandsforperformingcomplexactions.
Don‘tletthecomplexpotentialofaprogramkeepyoufrommakinguse
ofthesimpleraspects.Thischapter,likealloftherest,startwith
thesimpleconceptsandintroducestheadvancedtopicslateron.Anoteoncomments.WhenIfirstwrotethis,mostversionsofsed
didnotallowyoutoplacecommentsinsidethescript.
Linesstartingwiththe‘#‘charactersarecomments.
Newerversionsofsedmaysupportcommentsattheendofthelineaswell.
TheAwfulTruthaboutsed
Sedistheultimatestreameditor.
Ifthatsoundsstrange,pictureastreamflowingthroughapipe.
Okay,youcan‘tseeastreamifit‘sinsideapipe.
That‘swhatIgetforattemptingaflowinganalogy.
Youwantliterature,readJamesJoyce.
Anyhow,
sedisamarvelousutility.
Unfortunately,mostpeopleneverlearnitsrealpower.
Thelanguageisverysimple,butthe
documentationisterrible.TheSolarison-linemanualpagesfor
sedarefivepageslong,and
twoofthosepagesdescribethe34differenterrorsyoucanget.
Aprogramthatspendsasmuchspacedocumentingtheerrorsthanit
does
documentingthelanguagehasaseriouslearningcurve.
Donotfret!
Itisnotyourfaultyoudon‘tunderstand
sed.Iwillcover
sedcompletely.
ButIwilldescribethefeaturesintheorderthatIlearnedthem.
Ididn‘tlearneverythingatonce.Youdon‘tneedtoeither.
Theessentialcommand:
sforsubstitution
Sedhasseveralcommands,butmostpeopleonlylearnthesubstitutecommand:
s.Thesubstitutecommandchangesalloccurrencesoftheregular
expressionintoanewvalue.Asimpleexampleischanging
"day"to
"night:
"
seds/day/night/newIdidn‘tputquotesaroundtheargumentbecausethisexampledidn‘tneedthem.
Ifyoureadmyearliertutorial,youwouldunderstandwhyitdoesn‘t
needquotes.Ifyouhavemeta-charactersinthecommand,quotesarenecessary.
Inanycase,quotingisagoodhabit,andIwillhenceforthquotefuture
examples.
Thatis:
sed‘s/day/night/‘newTherearefourpartstothissubstitutecommand:
sSubstitutecommand
/../../Delimiter
dayRegularExpressionPatternString
nightReplacementstring
We‘vecovered
quotingand
regularexpressions..
That‘s90%ofthe
effortneededtolearnthesubstitutecommand.
Toputitanotherway,youalreadyknowhowtohandle90%ofthemost
frequentusesof
sed.Thereareafewfinepointsthatmustbecovered.
Theslashasadelimiter
Thecharacterafterthe
sisthedelimiter.Itisconventionallyaslash,because
thisiswhat
ed,
more,and
viuse.
Itcanbeanythingyouwant,however.
Ifyouwanttochangeapathnamethatcontainsaslash-say/usr/local/binto/common/bin-youcould
usethebackslashtoquotetheslash:
sed‘s/\/usr\/local\/bin/\/common\/bin/‘newGulp.Itiseasiertoreadifyouuseanunderlineinsteadofaslash
asadelimiter:
sed‘s_/usr/local/bin_/common/bin_‘newSomepeopleusecommas,othersusethe
"|"character.Pickoneyoulike.Aslongasit‘snotinthestringyouarelookingfor,anythinggoes.
Using&asthematchedstring
Sometimesyouwanttosearchforapatternandaddsomecharacters,
likeparenthesis,
aroundornear
thepatternyoufound.Itiseasytodothisifyouarelookingfora
particularstring:
sed‘s/abc/(abc)/‘newThiswon‘tworkifyoudon‘tknowexactlywhatyouwillfind.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Sed