Vous êtes sur la page 1sur 11

AnIntroductiontoawk

SimonStanford
BirminghamLUGUtilityfest
February2007

Introduction

Whatisawk?
PatternAction,PatternAction
Amorecomplexexample
Whattolookatnext
Q&A

Whatisawk?

Awkisapatternscanningandprocessing
language.

Itworksonatextfilealineatatime
Ifalinematchesapatterntherelatedactionis
executed

Createdby

AlfredAho
PeterWeinberger
BrianKernighan

(YesthatKernighan)

Henceawk.Itisnotshortforawkward.

ADisclaimer

Alloftheexamplesshowninthistalkcould
beachievedequallywell(ifnotbetter)using
avarietyofotherutilitiesorlanguagessuch
assed,perl,python,c,cobol,algolor
assembler.
Mostofthemcouldprobablybedone
differentlyinawkaswell.
There'sMoreThanOneWaytoDoIt
(TMTOWTDI)

PatternAction,PatternAction
/Logout/{printTheuserloggedoutat$1}

Anawkprogramconsistsofoneormore
patternactionpairs.
Patternsareenclosedin//,actionsin{}
Eachlineoftextistestedagainsteach
pattern,ifitmatchesthentheassociated
actionisperformed.
Thelineissplitintofields$1,$2,$3etc.for
useintheactionstatements.
$0isthewholeline.

AnExample

Processtheftp.logfiletocreateacsvfileof
userlogonsandfiletransfers.

Feb600:01:29logftp01ftpd[12500]:FTPLOGINFROM10.225.1.211[10.225.1.211],tnttrans
Feb600:01:29logftp01ftpd[12501]:TYPEImage
Feb600:01:29logftp01ftpd[12501]:PORT
Feb600:01:29logftp01ftpd[12501]:RETR/ftphome/general/niceday.list
Feb600:01:30logftp01ftpd[12501]:QUIT
Feb600:01:29logftp01ftpd[12500]:TYPEImage
Feb600:01:30logftp01ftpd[12500]:PORT
Feb600:01:30logftp01ftpd[12500]:STOR/ftphome/BDS/tntnewsbds.list
Feb600:01:30logftp01ftpd[12500]:QUIT
Feb600:01:30logftp01ftpd[12501]:FTPsessionclosed
Feb600:01:30logftp01ftpd[12500]:FTPsessionclosed
Feb600:01:30logftp01ftpd[12502]:USERsca

TheAwkProgram
/FTPLOGINFROM/{print$1"_"$2"_"$3","$7","$11"_"$9}
/STOR|RETR/{print$1"_"$2"_"$3","$6","$7}

Feb600:01:29logftp01ftpd[12500]:FTPLOGINFROM10.225.1.211[10.225.1.211],tnttrans

$1$2$3$4$5$6$7$8$9$10$11

Feb600:01:29logftp01ftpd[12501]:RETR/ftphome/general/niceday.list
Excuteprogramoninputfile:
awkfawk1local5ftp.log>userlog.csv

AMoreComplexExample

Addaheadertothecsvfileandattheend
printoutthenumberoflogons,thenumberof
filetransactionsandtheaveragefilesper
login.
Date,Action,Detail
Feb 6 00:01:29,LOGIN,tnttrans 10.225.1.211
Feb 6 00:01:29,RETR,/ftphome/SUDS/tntnews.list
Feb 6 00:01:29,LOGIN,tnttrans 10.225.1.211
Feb 6 00:01:29,LOGIN,tnttrans 10.225.1.211
Feb 7 00:00:37,LOGIN,csvmail 10.225.1.205
Feb 7 00:00:38,LOGIN,csvmail 10.225.1.205
Feb 7 00:00:51,LOGIN,tesco 172.16.1.65
Number of Logins=45146 Number of file operations=15622
Avg files per login =0.346033

TheAwkProgram
BEGIN{#setcountvariables
num_logins=0
num_files=0
#printtheheader
print"Date,Action,Detail"
}
/FTPLOGINFROM/{print$1""$2""$3","$7","$11""$9
#addonetologinscount
num_logins++
}
/STOR|RETR/{print$1""$2""$3","$6","$7
#addonetofilescount
num_files++
}
END{print"NumberofLogins="num_logins"Numberoffileoperations="num_files
#doanaveragebutjustcheckwedon'tdivbyzero
if(num_logins>0)print"Avgfilesperlogin="num_files/num_logins
}

Whattolookatnext

formattingwithprintfstatements

Otherdelimiters

awkF:fawk3/etc/passwd

Builtinvariables

printf(Files=%s,Avg=%4.2f\n,$1$3)

NF,FILENAME,NR

Morecomplexcontrolanddatastructures
andfunctions

for(;;),getline(),arrays

Quickies

Stripoutaspecificfieldfromawhitespace
delimitedinput.
E.g.Producealistofalltheprocessidsof
runningprocesses.
psef|awk'{print$2}'

Printthelinebeforeaparticularpattern

BEGIN
{previous=}
/FTPLOGINFROM/ {printprevious}
{previous=$0}

Vous aimerez peut-être aussi