SED—ANon-interactiveTextEditor LeeE.McMahon Contextsearch Editing ABSTRACT Sedisanon-interactivecontexteditorthatrunsontheUNIX◯roperating system.Sedisdesignedtobeespeciallyusefulinthreecases: 1)Toeditfilestoolargeforcomfortableinteractiveediting; 2)Toeditanysizefilewhenthesequenceofeditingcommandsistoocomplicatedtobe comfortablytypedininteractivemode. 3)Toperformmultiple‘global’editingfunctionsefficientlyinonepassthroughtheinput. Thismemorandumconstitutesamanualforusersofsed. August15,1978 SED—ANon-interactiveTextEditor LeeE.McMahon Contextsearch Editing Introduction Sedisanon-interactivecontexteditordesignedtobeespeciallyusefulinthreecases: 1)Toeditfilestoolargeforcomfortableinteractiveediting; 2)Toeditanysizefilewhenthesequenceofeditingcommandsistoocomplicatedtobecomfortablytypedininteractivemode; 3)Toperformmultiple‘global’editingfunctionsefficientlyinonepassthroughtheinput. Sinceonlyafewlinesoftheinputresideincoreatonetime,andnotemporaryfilesareused,the effectivesizeoffilethatcanbeeditedislimitedonlybytherequirementthattheinputandoutputfitsimultaneouslyintoavailablesecondarystorage. Complicatededitingscriptscanbecreatedseparatelyandgiventosedasacommandfile.For complexedits,thissavesconsiderabletyping,anditsattendanterrors.Sedrunningfromacommandfileismuchmoreefficientthananyinteractiveeditorknowntotheauthor,evenifthateditorcanbedrivenbyapre-writtenscript. Theprincipallossoffunctionscomparedtoaninteractiveeditorarelackofrelativeaddressing (becauseoftheline-at-a-timeoperation),andlackofimmediateverificationthatacommandhas donewhatwasintended. SedisalinealdescendantoftheUNIXeditor,ed.Becauseofthedifferencesbetweeninteractive andnon-interactiveoperation,considerablechangeshavebeenmadebetweenedandsed;even confirmedusersofedwillfrequentlybesurprised(andprobablychagrined),iftheyrashlyusesed withoutreadingSections2and3ofthisdocument.Themoststrikingfamilyresemblance betweenthetwoeditorsisintheclassofpatterns(‘regularexpressions’)theyrecognize;thecode formatchingpatternsiscopiedalmostverbatimfromthecodefored,andthedescriptionofregularexpressionsinSection2iscopiedalmostverbatimfromtheUNIXProgrammer’sManual[1]. (BothcodeanddescriptionwerewrittenbyDennisM.Ritchie.) 1.OverallOperation Sedbydefaultcopiesthestandardinputtothestandardoutput,perhapsperformingoneormore editingcommandsoneachlinebeforewritingittotheoutput.Thisbehaviormaybemodifiedby flagsonthecommandline;seeSection1.1below. Thegeneralformatofaneditingcommandis: [address1,address2][function][arguments] Oneorbothaddressesmaybeomitted;theformatofaddressesisgiveninSection2.Anynumber ofblanksortabsmayseparatetheaddressesfromthefunction.Thefunctionmustbepresent; theavailablecommandsarediscussedinSection3.Theargumentsmayberequiredoroptional, accordingtowhichfunctionisgiven;again,theyarediscussedinSection3undereachindividual function. Tabcharactersandspacesatthebeginningoflinesareignored.

-2-1.1.Command-lineFlags Threeflagsarerecognizedonthecommandline: -n:tellssednottocopyalllines,butonlythosespecifiedbypfunctionsorpflagsafters functions(seeSection3.3); -e:tellssedtotakethenextargumentasaneditingcommand; -f:tellssedtotakethenextargumentasafilename;thefileshouldcontaineditingcom-mands,onetoaline. 1.2.OrderofApplicationofEditingCommands Beforeanyeditingisdone(infact,beforeanyinputfileisevenopened),alltheeditingcommands arecompiledintoaformwhichwillbemoderatelyefficientduringtheexecutionphase(whenthe commandsareactuallyappliedtolinesoftheinputfile).Thecommandsarecompiledinthe orderinwhichtheyareencountered;thisisgenerallytheorderinwhichtheywillbeattemptedat executiontime.Thecommandsareappliedoneatatime;theinputtoeachcommandistheoutputofallprecedingcommands. Thedefaultlinearorderofapplicationofeditingcommandscanbechangedbytheflow-of-control commands,tandb(seeSection3).Evenwhentheorderofapplicationischangedbythesecommands,itisstilltruethattheinputlinetoanycommandistheoutputofanypreviouslyapplied command. 1.3.Pattern-space Therangeofpatternmatchesiscalledthepatternspace.Ordinarily,thepatternspaceisoneline oftheinputtext,butmorethanonelinecanbereadintothepatternspacebyusingtheNcommand(Section3.6.). 1.4.Examples Examplesarescatteredthroughoutthetext.Exceptwhereotherwisenoted,theexamplesall assumethefollowinginputtext: InXanadudidKublaKhan Astatelypleasuredomedecree: WhereAlph,thesacredriver,ran Throughcavernsmeasurelesstoman Downtoasunlesssea. (InnocaseistheoutputofthesedcommandstobeconsideredanimprovementonColeridge.) Example: Thecommand 2q willquitaftercopyingthefirsttwolinesoftheinput.Theoutputwillbe: InXanadudidKublaKhan Astatelypleasuredomedecree: 2.ADDRESSES:Selectinglinesforediting Linesintheinputfile(s)towhicheditingcommandsaretobeappliedcanbeselectedby addresses.Addressesmaybeeitherlinenumbersorcontextaddresses. Theapplicationofagroupofcommandscanbecontrolledbyoneaddress(oraddress-pair)by groupingthecommandswithcurlybraces(‘{}’)(Sec.3.6.).

-3-2.1.Line-numberAddresses Alinenumberisadecimalinteger.Aseachlineisreadfromtheinput,aline-numbercounteris incremented;aline-numberaddressmatches(selects)theinputlinewhichcausestheinternal countertoequaltheaddressline-number.Thecounterrunscumulativelythroughmultipleinput files;itisnotresetwhenanewinputfileisopened. Asaspecialcase,thecharacter$matchesthelastlineofthelastinputfile. 2.2.ContextAddresses Acontextaddressisapattern(‘regularexpression’)enclosedinslashes(‘/’).Theregularexpressionsrecognizedbysedareconstructedasfollows: 1)Anordinarycharacter(notoneofthosediscussedbelow)isaregularexpression,andmatches thatcharacter. 2)Acircumflex‘ˆ ’atthebeginningofaregularexpressionmatchesthenullcharacteratthe beginningofaline. 3)Adollar-sign‘$’attheendofaregularexpressionmatchesthenullcharacterattheendofa line. 4)Thecharacters‘\n’matchanimbeddednewlinecharacter,butnotthenewlineattheendof thepatternspace. 5)Aperiod‘.’matchesanycharacterexcepttheterminalnewlineofthepatternspace. 6)Aregularexpressionfollowedbyanasterisk‘*’matchesanynumber(including0)ofadjacent occurrencesoftheregularexpressionitfollows. 7)Astringofcharactersinsquarebrackets‘[]’matchesanycharacterinthestring,andnoothers.If,however,thefirstcharacterofthestringiscircumflex‘ˆ ’,theregularexpressionmatches anycharacterexceptthecharactersinthestringandtheterminalnewlineofthepatternspace. 8)Aconcatenationofregularexpressionsisaregularexpressionwhichmatchestheconcatenation ofstringsmatchedbythecomponentsoftheregularexpression. 9)Aregularexpressionbetweenthesequences‘\(’and‘\)’isidenticalineffecttotheunadorned regularexpression,buthasside-effectswhicharedescribedunderthescommandbelowandspeci-fication10)immediatelybelow. 10)Theexpression‘\d’meansthesamestringofcharactersmatchedbyanexpressionenclosedin ‘\(’and‘\)’earlierinthesamepattern.Heredisasingledigit;thestringspecifiedisthatbeginningwiththedthoccurrenceof‘\(’countingfromtheleft.Forexample,theexpression ‘ˆ \(.*\)\1’matchesalinebeginningwithtworepeatedoccurrencesofthesamestring. 11)Thenullregularexpressionstandingalone(e.g.,‘//’)isequivalenttothelastregularexpressioncompiled. Touseoneofthespecialcharacters(ˆ $.*[]\/)asaliteral(tomatchanoccurrenceofitselfin theinput),precedethespecialcharacterbyabackslash‘\’. Foracontextaddressto‘match’theinputrequiresthatthewholepatternwithintheaddress matchsomeportionofthepatternspace. 2.3.NumberofAddresses Thecommandsinthenextsectioncanhave0,1,or2addresses.Undereachcommandthemaximumnumberofallowedaddressesisgiven.Foracommandtohavemoreaddressesthanthe maximumallowedisconsideredanerror. Ifacommandhasnoaddresses,itisappliedtoeverylineintheinput. Ifacommandhasoneaddress,itisappliedtoalllineswhichmatchthataddress. Ifacommandhastwoaddresses,itisappliedtothefirstlinewhichmatchesthefirstaddress,and toallsubsequentlinesuntil(andincluding)thefirstsubsequentlinewhichmatchesthesecond address.Thenanattemptismadeonsubsequentlinestoagainmatchthefirstaddress,andthe processisrepeated.

-4-Twoaddressesareseparatedbyacomma. Examples: /an/matcheslines1,3,4inoursampletext /an.*an/matchesline1 /ˆ an/matchesnolines /./matchesalllines /\./matchesline5 /r*an/matcheslines1,3,4(number=zero!) /\(an\).*\1/matchesline1 3.FUNCTIONS Allfunctionsarenamedbyasinglecharacter.Inthefollowingsummary,themaximumnumber ofallowableaddressesisgivenenclosedinparentheses,thenthesinglecharacterfunctionname, possibleargumentsenclosedinangles(<>),anexpandedEnglishtranslationofthesinglecharactername,andfinallyadescriptionofwhateachfunctiondoes.Theanglesaroundthe argumentsarenotpartoftheargument,andshouldnotbetypedinactualeditingcommands. 3.1.Whole-lineOrientedFunctions (2)d--deletelines Thedfunctiondeletesfromthefile(doesnotwritetotheoutput)allthoselines matchedbyitsaddress(es). Italsohasthesideeffectthatnofurthercommandsareattemptedonthecorpse ofadeletedline;assoonasthedfunctionisexecuted,anewlineisreadfromthe input,andthelistofeditingcommandsisre-startedfromthebeginningonthe newline. (2)n--nextline Thenfunctionreadsthenextlinefromtheinput,replacingthecurrentline.The currentlineiswrittentotheoutputifitshouldbe.Thelistofeditingcommands iscontinuedfollowingthencommand. (1)a\ <text>--appendlines Theafunctioncausestheargument<text>tobewrittentotheoutputafterthe linematchedbyitsaddress.Theacommandisinherentlymulti-line;amust appearattheendofaline,and<text>maycontainanynumberoflines.To preservetheone-command-to-a-linefiction,theinteriornewlinesmustbehidden byabackslashcharacter(‘\’)immediatelyprecedingthenewline.The<text> argumentisterminatedbythefirstunhiddennewline(thefirstonenotimmediatelyprecededbybackslash). Onceanafunctionissuccessfullyexecuted,<text>willbewrittentotheoutput regardlessofwhatlatercommandsdotothelinewhichtriggeredit.Thetriggeringlinemaybedeletedentirely;<text>willstillbewrittentotheoutput. The<text>isnotscannedforaddressmatches,andnoeditingcommandsare attemptedonit.Itdoesnotcauseanychangeintheline-numbercounter. (1)i\ <text>--insertlines Theifunctionbehavesidenticallytotheafunction,exceptthat<text>iswrittentotheoutputbeforethematchedline.Allothercommentsaboutthea -5-functionapplytotheifunctionaswell. (2)c\ <text>--changelines Thecfunctiondeletesthelinesselectedbyitsaddress(es),andreplacesthem withthelinesin<text>.Likeaandi,cmustbefollowedbyanewlinehidden byabackslash;andinteriornewlinesin<text>mustbehiddenbybackslashes. Theccommandmayhavetwoaddresses,andthereforeselectarangeoflines.If itdoes,allthelinesintherangearedeleted,butonlyonecopyof<text>iswrittentotheoutput,notonecopyperlinedeleted.Aswithaandi,<text>isnot scannedforaddressmatches,andnoeditingcommandsareattemptedonit.It doesnotchangetheline-numbercounter. Afteralinehasbeendeletedbyacfunction,nofurthercommandsareattempted onthecorpse. Iftextisappendedafteralinebyaorrfunctions,andthelineissubsequently changed,thetextinsertedbythecfunctionwillbeplacedbeforethetextofthea orrfunctions.(TherfunctionisdescribedinSection3.4.) Note:Withinthetextputintheoutputbythesefunctions,leadingblanksandtabswilldisappear,asalwaysinsedcommands.Togetleadingblanksandtabsintotheoutput,precedethe firstdesiredblankortabbyabackslash;thebackslashwillnotappearintheoutput. Example: Thelistofeditingcommands: n a\ XXXX d appliedtoourstandardinput,produces: InXanadudidKubhlaKhan XXXX WhereAlph,thesacredriver,ran XXXX Downtoasunlesssea. Inthisparticularcase,thesameeffectwouldbeproducedbyeitherofthetwofollowingcommand lists: nn i\c\ XXXXXXXX d 3.2.SubstituteFunction Oneveryimportantfunctionchangespartsoflinesselectedbyacontextsearchwithintheline. (2)s<pattern><replacement><flags>--substitute Thesfunctionreplacespartofaline(selectedby<pattern>)with<replacement>.Itcanbestberead: Substitutefor<pattern>,<replacement> The<pattern>argumentcontainsapattern,exactlylikethepatternsin addresses(see2.2above).Theonlydifferencebetween<pattern>andacontext -6-addressisthatthecontextaddressmustbedelimitedbyslash(‘/’)characters; <pattern>maybedelimitedbyanycharacterotherthanspaceornewline. Bydefault,onlythefirststringmatchedby<pattern>isreplaced,butseetheg flagbelow. The<replacement>argumentbeginsimmediatelyaftertheseconddelimiting characterof<pattern>,andmustbefollowedimmediatelybyanotherinstanceof thedelimitingcharacter.(Thusthereareexactlythreeinstancesofthedelimiting character.) The<replacement>isnotapattern,andthecharacterswhicharespecialinpatternsdonothavespecialmeaningin<replacement>.Instead,othercharacters arespecial: &isreplacedbythestringmatchedby<pattern> \d(wheredisasingledigit)isreplacedbythedthsubstringmatchedby partsof<pattern>enclosedin‘\(’and‘\)’.Ifnestedsubstrings occurin<pattern>,thedthisdeterminedbycountingopening delimiters(‘\(’). Asinpatterns,specialcharactersmaybemadeliteralbyprecedingthemwithbackslash(‘\’). The<flags>argumentmaycontainthefollowingflags: g--substitute<replacement>forall(non-overlapping)instancesof <pattern>intheline.Afterasuccessfulsubstitution,thescan forthenextinstanceof<pattern>beginsjustaftertheendof theinsertedcharacters;charactersputintothelinefrom <replacement>arenotrescanned. p--printthelineifasuccessfulreplacementwasdone.Thepflagcauses thelinetobewrittentotheoutputifandonlyifasubstitution wasactuallymadebythesfunction.Noticethatifseverals functions,eachfollowedbyapflag,successfullysubstituteinthe sameinputline,multiplecopiesofthelinewillbewrittentothe output:oneforeachsuccessfulsubstitution. w<filename>--writethelinetoafileifasuccessfulreplacementwas done.Thewflagcauseslineswhichareactuallysubstitutedby thesfunctiontobewrittentoafilenamedby<filename>.If <filename>existsbeforesedisrun,itisoverwritten;ifnot,itis created. Asinglespacemustseparatewand<filename>. Thepossibilitiesofmultiple,somewhatdifferentcopiesofone inputlinebeingwrittenarethesameasforp. Amaximumof10differentfilenamesmaybementionedafterw flagsandwfunctions(seebelow),combined. Examples: Thefollowingcommand,appliedtoourstandardinput, s/to/by/wchanges produces,onthestandardoutput: InXanadudidKubhlaKhan Astatelypleasuredomedecree:

-7-WhereAlph,thesacredriver,ran Throughcavernsmeasurelessbyman Downbyasunlesssea. and,onthefile‘changes’: Throughcavernsmeasurelessbyman Downbyasunlesssea. Ifthenocopyoptionisineffect,thecommand: s/[.,;?:]/*P&*/gp produces: Astatelypleasuredomedecree*P:* WhereAlph*P,*thesacredriver*P,*ran Downtoasunlesssea*P.* Finally,toillustratetheeffectofthegflag,thecommand: /X/s/an/AN/p produces(assumingnocopymode): InXANadudidKubhlaKhan andthecommand: /X/s/an/AN/gp produces: InXANadudidKubhlaKhAN 3.3.Input-outputFunctions (2)p--print Theprintfunctionwritestheaddressedlinestothestandardoutputfile.They arewrittenatthetimethepfunctionisencountered,regardlessofwhatsucceedingeditingcommandsmaydotothelines. (2)w<filename>--writeon<filename> Thewritefunctionwritestheaddressedlinestothefilenamedby<filename>.If thefilepreviouslyexisted,itisoverwritten;ifnot,itiscreated.Thelinesare writtenexactlyastheyexistwhenthewritefunctionisencounteredforeachline, regardlessofwhatsubsequenteditingcommandsmaydotothem. Exactlyonespacemustseparatethewand<filename>. Amaximumoftendifferentfilesmaybementionedinwritefunctionsandwflags aftersfunctions,combined. (1)r<filename>--readthecontentsofafile Thereadfunctionreadsthecontentsof<filename>,andappendsthemafterthe linematchedbytheaddress.Thefileisreadandappendedregardlessofwhat subsequenteditingcommandsdotothelinewhichmatcheditsaddress.Ifrand afunctionsareexecutedonthesameline,thetextfromtheafunctionsandther functionsiswrittentotheoutputintheorderthatthefunctionsareexecuted. Exactlyonespacemustseparatetherand<filename>.Ifafilementionedbyar functioncannotbeopened,itisconsideredanullfile,notanerror,andnodiagnosticisgiven. NOTE:Sincethereisalimittothenumberoffilesthatcanbeopenedsimultaneously,care shouldbetakenthatnomorethantenfilesbementionedinwfunctionsorflags;thatnumberis -8-reducedbyoneifanyrfunctionsarepresent.(Onlyonereadfileisopenatonetime.) Examples Assumethatthefile‘note1’hasthefollowingcontents: Note:KublaKhan(moreproperlyKublaiKhan;1216-1294)wasthegrandson andmosteminentsuccessorofGenghiz(Chingiz)Khan,andfounderoftheMongoldynastyinChina. Thenthefollowingcommand: /Kubla/rnote1 produces: InXanadudidKublaKhan Note:KublaKhan(moreproperlyKublaiKhan;1216-1294)wasthegrandson andmosteminentsuccessorofGenghiz(Chingiz)Khan,andfounderoftheMongoldynastyinChina. Astatelypleasuredomedecree: WhereAlph,thesacredriver,ran Throughcavernsmeasurelesstoman Downtoasunlesssea. 3.4.MultipleInput-lineFunctions Threefunctions,allspelledwithcapitalletters,dealspeciallywithpatternspacescontaining imbeddednewlines;theyareintendedprincipallytoprovidepatternmatchesacrosslinesinthe input. (2)N--Nextline Thenextinputlineisappendedtothecurrentlineinthepatternspace;thetwo inputlinesareseparatedbyanimbeddednewline.Patternmatchesmayextend acrosstheimbeddednewline(s). (2)D--Deletefirstpartofthepatternspace Deleteuptoandincludingthefirstnewlinecharacterinthecurrentpattern space.Ifthepatternspacebecomesempty(theonlynewlinewastheterminal newline),readanotherlinefromtheinput.Inanycase,beginthelistofediting commandsagainfromitsbeginning. (2)P--Printfirstpartofthepatternspace Printuptoandincludingthefirstnewlineinthepatternspace. ThePandDfunctionsareequivalenttotheirlower-casecounterpartsiftherearenoimbedded newlinesinthepatternspace. 3.5.HoldandGetFunctions Fourfunctionssaveandretrievepartoftheinputforpossiblelateruse. (2)h--holdpatternspace Thehfunctionscopiesthecontentsofthepatternspaceintoaholdarea(destroyingthepreviouscontentsoftheholdarea). (2)H--Holdpatternspace TheHfunctionappendsthecontentsofthepatternspacetothecontentsofthe holdarea;theformerandnewcontentsareseparatedbyanewline. (2)g--getcontentsofholdarea -9-Thegfunctioncopiesthecontentsoftheholdareaintothepatternspace (destroyingthepreviouscontentsofthepatternspace). (2)G--Getcontentsofholdarea TheGfunctionappendsthecontentsoftheholdareatothecontentsofthepatternspace;theformerandnewcontentsareseparatedbyanewline. (2)x--exchange Theexchangecommandinterchangesthecontentsofthepatternspaceandthe holdarea. Example Thecommands 1h 1s/did.*// 1x G s/\n/:/ appliedtoourstandardexample,produce: InXanadudidKublaKhan:InXanadu Astatelypleasuredomedecree::InXanadu WhereAlph,thesacredriver,ran:InXanadu Throughcavernsmeasurelesstoman:InXanadu Downtoasunlesssea.:InXanadu 3.6.Flow-of-ControlFunctions Thesefunctionsdonoeditingontheinputlines,butcontroltheapplicationoffunctionstothe linesselectedbytheaddresspart. (2)!--Don’t TheDon’tcommandcausesthenextcommand(writtenonthesameline),tobe appliedtoallandonlythoseinputlinesnotselectedbytheadresspart. (2){--Grouping Thegroupingcommand‘{’causesthenextsetofcommandstobeapplied(ornot applied)asablocktotheinputlinesselectedbytheaddressesofthegrouping command.Thefirstofthecommandsundercontrolofthegroupingmayappear onthesamelineasthe‘{’oronthenextline. Thegroupofcommandsisterminatedbyamatching‘}’standingonalinebyitself. Groupscanbenested. (0):<label>--placealabel Thelabelfunctionmarksaplaceinthelistofeditingcommandswhichmaybereferredtobyb andtfunctions.The<label>maybeanysequenceofeightorfewercharacters;iftwodifferent colonfunctionshaveidenticallabels,acompiletimediagnosticwillbegenerated,andnoexecutionattempted. (2)b<label>--branchtolabel Thebranchfunctioncausesthesequenceofeditingcommandsbeingappliedtothecurrentinput linetoberestartedimmediatelyaftertheplacewhereacolonfunctionwiththesame<label>was encountered.Ifnocolonfunctionwiththesamelabelcanbefoundafteralltheediting -10-commandshavebeencompiled,acompiletimediagnosticisproduced,andnoexecutionis attempted. Abfunctionwithno<label>istakentobeabranchtotheendofthelistofeditingcommands; whatevershouldbedonewiththecurrentinputlineisdone,andanotherinputlineisread;the listofeditingcommandsisrestartedfromthebeginningonthenewline. (2)t<label>--testsubstitutions Thetfunctiontestswhetheranysuccessfulsubstitutionshavebeenmadeonthecurrentinput line;ifso,itbranchesto<label>;ifnot,itdoesnothing.Theflagwhichindicatesthatasuccessfulsubstitutionhasbeenexecutedisresetby: 1)readinganewinputline,or 2)executingatfunction. 3.7.MiscellaneousFunctions (1)=--equals The=functionwritestothestandardoutputthelinenumberofthelinematched byitsaddress. (1)q--quit Theqfunctioncausesthecurrentlinetobewrittentotheoutput(ifitshould be),anyappendedorreadtexttobewritten,andexecutiontobeterminated. Reference [1]KenThompsonandDennisM.Ritchie,TheUNIXProgrammer’sManual.BellLaboratories,1978.