EffectiveawkProgrammingArnoldRobbins Tomyparents,fortheirlove,andforthewonderfulexampletheysetforme. TomywifeMiriam,formakingmecomplete.Thankyouforbuildingyourlifetogether withme. ToourchildrenChana,Rivka,Nachum,andMalka,forenricheningourlivesin innumerableways. ForewordtotheThirdEditionMichaelBrennan Authorofmawk ArnoldRobbinsandIaregoodfriends.Wewereintroducedin1990bycircumstances andourfavoriteprogramminglanguage,awk.Thecircumstancesstartedacoupleofyears earlier.IwasworkingatanewjobandnoticedanunpluggedUnixcomputersittinginthe corner.Nooneknewhowtouseit,andneitherdidI.However,acoupleofdayslater,it wasrunning,andIwasrootandtheone-and-onlyuser.Thatday,Ibeganthetransition fromstatisticiantoUnixprogrammer. OnoneofmanytripstothelibraryorbookstoreinsearchofbooksonUnix,Ifoundthe grayawkbook,a.k.a.AlfredV.Aho,BrianW.Kernighan,andPeterJ.Weinbergers TheAWKProgrammingLanguage(Addison-Wesley,1988).awkssimpleprogramming paradigmfindapatternintheinputandthenperformanactionoftenreduced complexortediousdatamanipulationstoafewlinesofcode.Iwasexcitedtotrymyhand atprogramminginawk. (Incidentally,itsnolongerdifficulttofindanewawk.gawkshipswithGNU/Linux,and youcandownloadbinariesorsourcecodeforalmostanysystem;mywifeusesgawkon herVMSbox.) MyUnixsystemstartedoutunpluggedfromthewall;itcertainlywasnotpluggedintoa network.So,oblivioustotheexistenceofgawkandtheUnixcommunityingeneral,and desiringanewawk,Iwrotemyown,calledmawk.BeforeIwasfinished,Iknewabout gawk,butitwastoolatetostop,soIeventuallypostedtoacomp.sourcesnewsgroup. (Incidentally,itsnolongerdifficulttofindanewawk.gawkshipswithGNU/Linux,and youcandownloadbinariesorsourcecodeforalmostanysystem;mywifeusesgawkon herVMSbox.) MyUnixsystemstartedoutunpluggedfromthewall;itcertainlywasnotpluggedintoa network.So,oblivioustotheexistenceofgawkandtheUnixcommunityingeneral,and desiringanewawk,Iwrotemyown,calledmawk.BeforeIwasfinished,Iknewabout gawk,butitwastoolatetostop,soIeventuallypostedtoacomp.sourcesnewsgroup.
Afewdaysaftermyposting,IgotafriendlyemailfromArnoldintroducinghimself.He suggestedwesharedesignandalgorithmsandattachedadraftofthePOSIXstandardso thatIcouldupdatemawktosupportlanguageextensionsaddedafterpublicationof TheAWKProgrammingLanguage. Frankly,ifourroleshadbeenreversed,Iwouldnothavebeensoopenandweprobably wouldhavenevermet.Imgladwedidmeet.Heisanawkexpertsawkexpertanda genuinelyniceperson.Arnoldcontributessignificantamountsofhisexpertiseandtimeto theFreeSoftwareFoundation. Thisbookisthegawkreferencemanual,butatitscoreitisabookaboutawkprogramming thatwillappealtoawideaudience.Itisadefinitivereferencetotheawklanguageas definedbythe1987BellLaboratoriesreleaseandcodifiedinthe1992POSIXUtilities standard. Ontheotherhand,thenoviceawkprogrammercanstudyawealthofpracticalprograms thatemphasizethepowerofawksbasicidioms:data-drivencontrolflow,patternmatching withregularexpressions,andassociativearrays.Thoselookingforsomethingnewcantry outgawksinterfacetonetworkprotocolsviaspecial/inetfiles. Theprogramsinthisbookmakeclearthatanawkprogramistypicallymuchsmallerand fastertodevelopthanacounterpartwritteninC.Consequently,thereisoftenapayoffto prototypinganalgorithmordesigninawktogetitrunningquicklyandexposeproblems early.Often,theinterpretedperformanceisadequateandtheawkprototypebecomesthe product. Thenewpgawk(profilinggawk)producesprogramexecutioncounts.Irecently experimentedwithanalgorithmthatfor nlinesofinputexhibited Cn2performance, whiletheorypredicted Cnlognbehavior.Afewminutesporingovertheawkprof.out profilepinpointedtheproblemtoasinglelineofcode.pgawkisawelcomeadditiontomy programmerstoolbox.
Arnoldhasdistilledoveradecadeofexperiencewritingandusingawkprograms,and developinggawk,intothisbook.Ifyouuseawkorwanttolearnhow,thenreadthisbook. ForewordtotheFourthEditionMichaelBrennan Authorofmawk Somethingsdontchange.ThirteenyearsagoIwrote:Ifyouuseawkorwanttolearn how,thenreadthisbook.Truethen,andstilltruetoday. Learningtouseaprogramminglanguageisaboutmorethanmasteringthesyntax.One needstoacquireanunderstandingofhowtousethefeaturesofthelanguagetosolve practicalprogrammingproblems.Afocusofthisbookismanyexamplesthatshowhowto useawk. Somethingsdochange.Ourcomputersaremuchfasterandhavemorememory. Consequently,speedandstorageinefficienciesofahigh-levellanguagematterless. PrototypinginawkandthenrewritinginCforperformancereasonshappensless,because moreoftentheprototypeisfastenough.
Ofcourse,therearecomputingoperationsthatarebestdoneinCorC++.Withgawk4.1 andlater,youdonothavetochoosebetweenwritingyourprograminawkorinC/C++. YoucanwritemostofyourprograminawkandtheaspectsthatrequireC/C++capabilities canbewritteninC/C++,andthenthepiecesgluedtogetherwhenthegawkmoduleloads theC/C++moduleasadynamicplug-in. hasallthedetails,and,asexpected, manyexamplestohelpyoulearntheinsandouts. Ienjoyprogramminginawkandhadfun(re)readingthisbook.Ithinkyouwill,too. PrefaceArnoldRobbins NofAyalon Israel Severalkindsoftasksoccurrepeatedlywhenworkingwithtextfiles.Youmightwantto extractcertainlinesanddiscardtherest.Oryoumayneedtomakechangeswherever certainpatternsappear,butleavetherestofthefilealone.Suchjobsareofteneasywith awk.Theawkutilityinterpretsaspecial-purposeprogramminglanguagethatmakesiteasy tohandlesimpledata-reformattingjobs. TheGNUimplementationofawkiscalledgawk;ifyouinvokeitwiththeproperoptionsor environmentvariables,itisfullycompatiblewiththePOSIX]specificationoftheawk languageandwiththeUnixversionofawkmaintainedbyBrianKernighan.Thismeans thatallproperlywrittenawkprogramsshouldworkwithgawk.Somostofthetime,we dontdistinguishbetweengawkandotherawkimplementations.
Usingawkyoucan: Managesmall,personaldatabases Generatereports Validatedata Produceindexesandperformotherdocument-preparationtasks Experimentwithalgorithmsthatyoucanadaptlatertoothercomputerlanguages Inaddition,gawkprovidesfacilitiesthatmakeiteasyto: Extractbitsandpiecesofdataforprocessing Sortdata Performsimplenetworkcommunications Profileanddebugawkprograms ExtendthelanguagewithfunctionswritteninCorC++ Thisbookteachesyouabouttheawklanguageandhowyoucanuseiteffectively.You shouldalreadybefamiliarwithbasicsystemcommands,suchascatandls, aswellas basicshellfacilities,suchasinput/output(I/O)redirectionandpipes. Implementationsoftheawklanguageareavailableformanydifferentcomputing environments.Thisbook,whiledescribingtheawklanguageingeneral,alsodescribesthe particularimplementationofawkcalledgawk(whichstandsforGNUawk).gawkrunson abroadrangeofUnixsystems,rangingfromIntel-architecturePC-basedcomputersup throughlarge-scalesystems.gawkhasalsobeenportedtoMacOSX,MicrosoftWindows (allversions),andOpenVMS.[] HistoryofawkandgawkRECIPEFORAPROGRAMMINGLANGUAGE 1partegrep 1partsnobol 2partsed 3partsC Blendallpartswellusinglexandyacc.Documentminimallyandrelease. Aftereightyears,addanotherpartegrepandtwomorepartsC.Documentverywellandrelease. Thenameawkcomesfromtheinitialsofitsdesigners:AlfredV.Aho,PeterJ.Weinberger, andBrianW.Kernighan.Theoriginalversionofawkwaswrittenin1977atAT&TBell Laboratories.In1985,anewversionmadetheprogramminglanguagemorepowerful, introducinguser-definedfunctions,multipleinputstreams,andcomputedregular expressions.ThisnewversionbecamewidelyavailablewithUnixSystemVRelease3.1 (1987).TheversioninSystemVRelease4(1989)addedsomenewfeaturesandcleaned upthebehaviorinsomeofthedarkcornersofthelanguage.Thespecificationforawkin thePOSIXCommandLanguageandUtilitiesstandardfurtherclarifiedthelanguage.Both thegawkdesignersandtheoriginalawkdesignersatBellLaboratoriesprovidedfeedback forthePOSIXspecification. PaulRubinwrotegawkin1986.JayFenlasoncompletedit,withadvicefromRichard Stallman.JohnWoodscontributedpartsofthecodeaswell.In1988and1989,David Trueman,withhelpfromme,thoroughlyreworkedgawkforcompatibilitywiththenewer awk.Circa1994,Ibecametheprimarymaintainer.Currentdevelopmentfocusesonbug fixes,performanceimprovements,standardscompliance,and,occasionally,newfeatures. InMay1997,JrgenKahrsfelttheneedfornetworkaccessfromawk,andwithalittle helpfromme,setaboutaddingfeaturestodothisforgawk.Atthattime,healsowrotethe bulkof TCP/IPInternetworkingwithgawk(aseparatedocument,availableaspartofthe gawkdistribution).Hiscodefinallybecamepartofthemaingawkdistributionwithgawk version3.1.
Next page