You are on page 1of 15

10/14/11 Awk Introduction Tutorial 7 Awk Print Examples

1/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

Home
About
FreeeBook
Archives
BestoftheBlog
Contact
AwkIntroductionTutorial7AwkPrint
Examples
bySasikalaonJanuary6,2010
StumbleUpon
Thisisthefirstarticleonthenewawktutorialseries.Wellbepostingseveralarticlesonawkinthe
upcomingweeksthatwillcoverallfeaturesofawkwithpracticalexamples.
Inthisarticle,letusreviewthefundamentalawkworkingmethodologyalongwith7practicalawkprint
examples.
Note:MakesureyoureviewourearlierSedTutorialSeries.
AwkIntroductionandPrintingOperations
Awkisaprogramminglanguagewhichallowseasymanipulationofstructureddataandthegenerationof
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
2/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
formattedreports.AwkstandsforthenamesofitsauthorsAho,Weinberger,andKernighan
TheAwkismostlyusedforpatternscanningandprocessing.Itsearchesoneormorefilestoseeifthey
containlinesthatmatcheswiththespecifiedpatternsandthenperformassociatedactions.
SomeofthekeyfeaturesofAwkare:
Awkviewsatextfileasrecordsandfields.
Likecommonprogramminglanguage,Awkhasvariables,conditionalsandloops
Awkhasarithmeticandstringoperators.
Awkcangenerateformattedreports
Awkreadsfromafileorfromitsstandardinput,andoutputstoitsstandardoutput.Awkdoesnotgetalong
withnontextfiles.
Syntax:
awk'/searchpattern1/{Actions
/searchpattern2/{Actions'file
Intheaboveawksyntax:
searchpatternisaregularexpression.
Actionsstatement(s)tobeperformed.
severalpatternsandactionsarepossibleinAwk.
fileInputfile.
Singlequotesaroundprogramistoavoidshellnottointerpretanyofitsspecialcharacters.
AwkWorkingMethodolog
1. Awkreadstheinputfilesonelineatatime.
2. Foreachline,itmatcheswithgivenpatterninthegivenorder,ifmatchesperformsthecorresponding
action.
3. Ifnopatternmatches,noactionwillbeperformed.
4. Intheabovesyntax,eithersearchpatternoractionareoptional,Butnotboth.
5. Ifthesearchpatternisnotgiven,thenAwkperformsthegivenactionsforeachlineoftheinput.
6. Iftheactionisnotgiven,printallthatlinesthatmatcheswiththegivenpatternswhichisthedefault
action.
7. Emptybraceswithoutanyactiondoesnothing.Itwontperformdefaultprintingoperation.
8. EachstatementinActionsshouldbedelimitedbysemicolon.
Letuscreateemployee.txtfilewhichhasthefollowingcontent,whichwillbeusedinthe
examplesmentionedbelow.
$catemployee.txt
100ThomasManagerSales$5,000
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
3/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
200JasonDeveloperTechnology$5,500
300SanjaySysadminTechnology$7,000
400NishaManagerMarketing$9,500
500RandyDBATechnology$6,000
AkEample1.DefalbehaioofAk
BydefaultAwkprintseverylinefromthefile.
$awk'{print'employee.txt
100ThomasManagerSales$5,000
200JasonDeveloperTechnology$5,500
300SanjaySysadminTechnology$7,000
400NishaManagerMarketing$9,500
500RandyDBATechnology$6,000
Intheaboveexamplepatternisnotgiven.Sotheactionsareapplicabletoallthelines.
Actionprintwithoutanyargumentprintsthewholelinebydefault.Soitprintsallthe
linesofthefilewithoutfail.Actionshastobeenclosedwithinthebraces.
AkEample2.Pinhelinehichmacheihhepaen.
$awk'/Thomas/
>/Nisha/'employee.txt
100ThomasManagerSales$5,000
400NishaManagerMarketing$9,500
IntheaboveexampleitprintsallthelinewhichmatcheswiththeThomasorNisha.Ithastwopatterns.
Awkacceptsanynumberofpatterns,buteachset(patternsanditscorrespondingactions)hastobe
separatedbynewline.
AkEample3.Pinonlpecificfield.
Awkhasnumberofbuiltinvariables.Foreachrecordi.eline,itsplitstherecorddelimitedbywhitespace
characterbydefaultandstoresitinthe$nvariables.Ifthelinehas4words,itwillbestoredin$1,$2,$3
and$4.$0representswholeline.NFisabuiltinvariablewhichrepresentstotalnumberoffieldsina
record.
$awk'{print$2,$5'employee.txt
Thomas$5,000
Jason$5,500
Sanjay$7,000
Nisha$9,500
Randy$6,000
$awk'{print$2,$NF'employee.txt
Thomas$5,000
Jason$5,500
Sanjay$7,000
Nisha$9,500
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
4/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
Randy$6,000
Intheaboveexample$2and$5representsNameandSalaryrespectively.WecangettheSalaryusing
$NFalso,where$NFrepresentslastfield.Intheprintstatement,isaconcatenator.
AkEample4.InitialiationandFinalAction
AwkhastwoimportantpatternswhicharespecifiedbythekeywordcalledBEGINandEND.
Syntax:
BEGIN{Actions
{ACTION#Actionforeverylineinafile
END{Actions
#isforcommentsinAwk
ActionsspecifiedintheBEGINsectionwillbeexecutedbeforestartsreadingthelinesfromtheinput.
ENDactionswillbeperformedaftercompletingthereadingandprocessingthelinesfromtheinput.
$awk'BEGIN{print"Name\tDesignation\tDepartment\tSalary"
>{print$2,"\t",$3,"\t",$4,"\t",$NF
>END{print"ReportGenerated\n"
>'employee.txt
Name Designation Department Salary
ThomasManager Sales$5,000
Jason Developer Technology $5,500
SanjaySysadmin Technology $7,000
Nisha Manager Marketing $9,500
Randy DBA Technology $6,000
ReportGenerated

Intheaboveexample,itprintsheadlineandlastfileforthereports.
AkEample5.Findtheemploeeshohasemploeeidgreaterthan200
$awk'$1>200'employee.txt
300SanjaySysadminTechnology$7,000
400NishaManagerMarketing$9,500
500RandyDBATechnology$6,000
Intheaboveexample,firstfield($1)isemployeeid.Soif$1isgreaterthan200,thenjustdothedefault
printactiontoprintthewholeline.
AkEample6.PrintthelistofemploeesinTechnologdepartment
Nowdepartmentnameisavailableasafourthfield,soneedtocheckif$4matcheswiththestring
Technology,ifyesprinttheline.
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
5/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
$awk'$4/Technology/'employee.txt
200JasonDeveloperTechnology$5,500
300SanjaySysadminTechnology$7,000
500RandyDBATechnology$6,000
Operator~isforcomparingwiththeregularexpressions.Ifitmatchesthedefaultactioni.eprintwholeline
willbeperformed.
AkEample7.PrintnumberofemploeesinTechnologdepartment
Thebelowexample,checksifthedepartmentisTechnology,ifitisyes,intheAction,justincrementthe
countvariable,whichwasinitializedwithzerointheBEGINsection.
$awk'BEGIN{count=0
$4/Technology/{count++
END{print"NumberofemployeesinTechnologyDept=",count'employee.txt
NumberofemployeesinTehcnologyDept=3
Thenattheendoftheprocess,justprintthevalueofcountwhichgivesyouthenumberofemployeesin
Technologydepartment.
RecommendedReading
SedandAk101Hacks,bRameshNatarajan.Ispendseveralhoursadayon
UNIX/Linuxenvironmentdealingwithtextfiles(data,config,andlogfiles).IuseSedandAwkforall
mymytextmanipulationwork.BasedonmySedandAwkexperience,IvewrittenSedandAwk101
HackseBookthatcontains101practicalexamplesonvariousadvancedfeaturesofSedandAwkthatwill
enhanceyourUNIX/Linuxlife.EvenifyouvebeenusingSedandAwkforseveralyearsandhavenot
readthisbook,pleasedoyourselfafavorandreadthisbook.YoullbeamazedwiththecapabilitiesofSed
andAwkutilities.
AdditionalAkArticles
AwkUserdefinedVariableswith3PracticalExamples
8PowerfulAwkBuiltinVariablesFS,OFS,RS,ORS,NR,NF,FILENAME,FNR
7PowerfulAwkOperatorsExamples(Unary,Binary,Arithmetic,String,Assignment,Conditional,
RegExAwkOperators)
4AwkIfStatementExamples(if,ifelse,ifelseif,:?)
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
6/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

StumbleUpon
Share
CaughtIntheLoop?AwkWhile,DoWhile,ForLoop,Break,Continue,ExitExamples
Comment
Ifouenjoedthisarticle,oumightalsolike..
1. 50LinuxSysadminTutorials
2. 50MostFrequentlyUsedLinuxCommands
(WithExamples)
3. Mommy,Ifoundit!15PracticalLinux
FindCommandExamples
4. TurbochargePuTTYwith12PowerfulAdd
Ons
5. 15AwesomeGoogleSearchTipsandTricks
AwkIntroductionTutorial7AwkPrint
Examples
SedTutorial:AdvancedSedSubstitution
Examples
8EssentialVimEditorNavigation
Fundamentals
25MostFrequentlyUsedLinuxIPTables
RulesExamples
AdvancedRegularExpressionsinGrep
Commandwith10Examples
Tags:AwkACTIONBlock,AwkBEGINBlock,AwkBeginEnd,AwkENDBlock,AwkTutorial
Examples,LinuxAwkExamples,LinuxAwkTutorial,UnixAwkExamples,UnixAwkTutorial
{27commentsreadthembeloworaddone}
1SteveMillsJanuary6,2010at3:38am
Ihaveonlyjuststartedreadingthesearticles.SofarIthinktheyarewellwrittenandtheexplanations
areclearlydonewithanawarenessastohowtheymightpossiblybemisunderstoodandhenceextra
layersofdetailarepresentedwherethatmighthappen.Forexample,pointingoutthatthetilde(~)is
usedtocomparewithregularexpressionswhenthereadermighthaveotherwiseexpectedanequals
signwithouttheexplanationthereadermighthavedecidedthatthetilderepresentedthesamething
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
7/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
asanequalssign.
Ishallbereadingmore.
Thanksforpostingthesearticles.
KindRegards
Steve
2DanielReimannJanuary6,2010at6:02am
Thankyoufortheposthereonawk.Iuseitfrequently,butitisalwaysgoodtohavesomeupdates
andreminders.HappyNewYear.
3LawrenceJanuary7,2010at4:34am
awkisawesome!thanksforyoursharing.
BestRegards,
Lawrence
4KnusperJanuary9,2010at5:15pm
HiGoodarticlenowIknowwhatarkis,andwhatIcoulduseitforwellwritten.Ifollow
younowontwitter!
5HarshJanuary10,2010at10:08pm
Thanksforpostingatutorialonawkwithillustratedexamples.
IWillbeexpectingotherarticlesonawk
6RameshNatarajanJanuary14,2010at9:23pm
@Steve,
Yeah.~canbelittleconfusinginthiscontext,ifnotexplainedproperly.Thanksforyoucomment.
@Daniel,
Yeah.Mostotherreadersoftheblogareinsimilarpositionlikeyou.So,weareheretogiveconstant
updatedandremaindersofthefunctionalitythattheyalreadyknow.
@Lawrence,Harsh,
Thanksfortheverynicecomment.Imgladyoulikedthisarticle.
@Knusper,
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
8/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
Thanksforfollowingusontwitter.
7thalafanMarch21,2010at10:24am
NandrakaUlladhu!!!
Iguesstheexample2canbedonewithoutanewlinelikebelow?Patternasregex.
~/bin$awk/Jason|Randy/employee
200JasonDeveloperTechnology$5,500
500RandyDBATechnology$6,000
Andalsowhatdoesthestandsfor?EndofPattern?
8AndreiaAmaralApril7,2010at5:14am
Hi,
Iwanttouseanifelsestatementlikethis:
if$10>10print$0>filename1
elseprint$0>filename2
butitsnotworkingitisnotcreatingfilename1orfilename2,howcanIdothis?
thanks?
9AshuAgrawalAugust6,2010at10:31am
Grtpost.Thanksformakingmeunderstandtheawkworking
10avinashOctober1,2010at7:30am
hi,thisisavinash.
supposeuhaveaemptablelikethis:
idnamedesignationsalary
1avimanager1000
2ashmanager1500
3nashmanager2000
4varmatrainee500
5chowtrainee600
6hemanthtrainee800
usingawkcommand,ihavtoprintmanagertotalsalaryandtraineetotalsalary.
ineedaprogram..cananyoneplzpostit
11vikasOctober13,2010at1:34am
Hi..@Avinash..
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
9/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
ucantrythisone.
awkBEGIN{man_sal=0trainee_sal=0}
$3~/manager/{man_sal+=$NF}
/trainee/{trainee_sal+=$NF}
END{printTotalsalaryofmanagersandtraineesare=man_sal,trainee_sal}in_file.name
12sivaOctober15,2010at1:44am
Helloforummembers,
ThanksforAWKtutorials,itwasveryhelpfultome.
13avinashOctober19,2010at5:12am
@vikas
thanksyou
14wishOctober21,2010at3:36am
hiall,
ifihaveaissuefilelike:
101addopenvishiuo
ifexitandloginagainishouldgettheincrementofthefirstfieldlike
102addopenvishiuo
15mounikaOctober27,2010at10:40pm
itssimplysuperbtounderstand
itsisveryusefulforthebeginninglearnersanditsisveryhelpinexamstimealso
soguysreadandenjoywdtheunix
16LyndaZhangNovember17,2010at12:36pm
Thisisveryhelp.HowaboutifIwanttoprintthesalaryseperatedbycommas,e.g.2,000insteadof
2000
17IkemDecember30,2010at8:22pm
Youvemadealittletypo:
>Numberofemployeesin_Tehcnology_Dept=3
18sudhaFebruary2,2011at12:38pm
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
10/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
varyvaryhealpultoeveryone
19ebenMarch27,2011at11:28pm
Itsveryusefulforbeginerslikeme
20kalandarApril7,2011at8:58am
Hi,
Ifoundthisarticletobeveryuseful.Anybodywhowantstoknowwhatanawkis,thiswillgivea
fairidea.Lookingforwardtosimilararticlesonothertopicsofunix.
Thanks
21BhagyarajApril24,2011at1:38am
Hi,
Good,
PleasetryteachinYoutubetotrydifferntly.
Itwillbemoresuccess.
Keepitup,
Ineedtotakeanexamonawk,letmeseehowmuchIcansuccessed.
22kernelkidJune10,2011at6:48am
verysimpleandeasytounderstand,thanksalot,ithelpmealot
23lijuJune14,2011at3:05am
goodsimplearticle
24MarijaJune30,2011at9:03am
Ihavereadfewgeekstuffarticlesuntilnow,explanationsprovidedarethebestIhaveeverseenso
far!Greatjob Thanksalot
25MuslimJuly19,2011at12:02pm
hi,
ihavethequestionthathowtousingprintcommandawktosortortransposethisdatafrommany
coloumsto2columnsonly
#inputfile
NAMEJANFEBMARCHAPRILMAYJUNEJULY
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
11/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

BEN5,0006,0007,0008,0006,5007,5009,000
YONG4,0005,5006,0005,8007,0008,0008,5000
#outputshouldbeasbelow.
BEN5,000
BEN6,000
BEN7,000
BEN8,000
BEN6,500
BEN7,500
BEN9,000
YONG4,000
YONG5,500
YONG6,000
YONG5,800
YONG7,000
YONG8,000
YONG8,5000
Anyonecanhelp.thanks
@muss
26nailscarmodyAugust9,2011at1:08pm
Iknowitslate,but
#!/bin/bash
awk{
#skipthefirsttwolines
if(NR==1||NR==2)
continue
for(i=2i<=NFi++)
printf("%s%s\n",$1,$i)
}'datafile.txt
Nicesite!IlearnedsomenewthingsaboutsedIdidn'tknow.
27SudhanshuAugust29,2011at1:00am
Thearticleissimplyawesome!!!
LeaveaComment
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
12/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
Name
Email
Webie
Noifmeoffollopcommeniaemail
Submit
Peiopo:CanYoTopThi?15PacicalLinTopCommandEample
Nepo:6SepfoMinimalUbnInallaionUingdebooap
Signpfoofeeemailnelee ou@address.com SignU
RSS Tie Facebook
Search
EBOOK

10/14/11 Awk Introduction Tutorial 7 Awk Print Examples


13/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

POPULAR POSTS
12AmazingandEssentialLinuxBooksToEnrichYourBrainandLibrary
50UNIX/LinuxSysadminTutorials
50MostFrequentlyUsedUNIX/LinuxCommands(WithExamples)
HowToBeProductiveandGetThingsDoneUsingGTD
30ThingsToDoWhenyouareBoredandhaveaComputer
LinuxDirectoryStructure(FileSystemStructure)ExplainedwithExamples
LinuxCrontab:15AwesomeCronJobExamples
GetaGripontheGrep!15PracticalGrepCommandExamples
UnixLSCommand:15PracticalExamples
15ExamplesToMasterLinuxCommandLineHistory
Top10OpenSourceBugTrackingSystem
ViandVimMacroTutorial:HowToRecordandPlay
Mommy,Ifoundit!15PracticalLinuxFindCommandExamples
15AwesomeGmailTipsandTricks
15AwesomeGoogleSearchTipsandTricks
RAID0,RAID1,RAID5,RAID10ExplainedwithDiagrams
CanYouTopThis?15PracticalLinuxTopCommandExamples
Top5BestSystemMonitoringTools
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
14/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
Top5BestLinuxOSDistributions
HowToMonitorRemoteLinuxHostusingNagios3.0
AwkIntroductionTutorial7AwkPrintExamples
HowtoBackupLinux?15rsyncCommandExamples
TheUltimateWgetDownloadGuideWith15AwesomeExamples
Top5BestLinuxTextEditors
PacketAnalyzer:15TCPDUMPCommandExamples
TheUltimateBashArrayTutorialwith15Examples
3StepstoPerformSSHLoginWithoutPasswordUsingsshkeygen&sshcopyid
UnixSedTutorial:AdvancedSedSubstitutionExamples
UNIX/Linux:10NetstatCommandExamples
TheUltimateGuideforCreatingStrongPasswords
6StepstoSecureYourHomeWirelessNetwork
TurbochargePuTTYwith12PowerfulAddOns
About The Geek Stuff
MynameisRamesh Natarajan.Iwillbepostinginstructionguides,howto,
troubleshootingtipsandtricksonLinux,database,hardware,securityandweb.Myfocusistowrite
articlesthatwilleitherteachyouorhelpyouresolveaproblem.ReadmoreaboutRameshNatarajan
andtheblog.
Support Us
Supportthisblogbypurchasingoneofmyebooks.
Bash101HackseBook
SedandAwk101HackseBook
Vim101HackseBook
NagiosCore3eBook
Contact Us
10/14/11 Awk Introduction Tutorial 7 Awk Print Examples
15/15 www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
EaiMe:UsethisContactFormtogetintouchmewithyourcomments,questionsorsuggestions
aboutthissite.Youcanalsosimplydropmealinetosayhello!.
FollowusonTwitter
BecomeafanonFacebook
Copyright20082011RameshNatarajan.Allrightsreserved|TermsofService|Advertise

You might also like