Professional Documents
Culture Documents
Command
Description
awk'{printFNR"\t"$0}'filename
Printlinenumbersusingatabinsteadofaspace
awk'{print$NF}'filename
Printthelastfieldofeachlineofthefile
awk'END{printNR}'filename
Countthelinesinafile.Justlike"wcl".
awk'/Apache/{n++};END{printn+0}'filename
TotalthenumberoflinesthatcontainthenameApache
awk'{sum+=$6}END{printsum}'filename
Addupthenumbersof6thfield,printthetotal
awk'$4~/^[09]/{print$3}'filename
Ifthe3rdfieldstartswithanumber,printthatfield
awk'{sub(/[\t]+$/,"");print}'filename
Deletethetrailingwhitespacefromendofeachline
awk'{gsub(/^[\t]+|[\t]+$/,"");print}'filename
Deleteleadingandtrailingwhitespacefromeachline
awkF:'{print$1""$2}'filename
Printthefirst2fieldswithaspacebetweentheoutput
awk'{tmp=$1;$1=$2;$2=tmp;print}'filename
Swapthefirst2fields
awk'NR<=5'filename
Printthefirst5linesofafile
awk'END{print}'filename
Printthelastlineofafile
awk'{$1="";print}'filename
Removethesecondfieldineachlineandthenprintit
awk'{sub(/^/,"");print}'filename
Insert6blankspacesatbeginningofeachline
awk'{sub(/^[\t]+/,"");print}'filename
Deletetheleadingwhitespace(spacesortabs)fromfront
RegularExpressions
awk'/regex/'filename
Printthelinesmatchingtheregularexpression
awk'!/regex/'filename
Printthelinesthatdon'tmatchtheregularexpression
awk'/regex/{printx};{x=$0}'filename
Printthelinebeforetheregularexpressionmatch
awk'/regex/{getline;print}'filename
Printthelineaftertheregularexpressionmatch
awk'/Dog/,/Cat/'filename
Printlinesbetweenthematchesstartingoneandending
awk'!/pet/{gsub(/dog/,"cat")};{print}'filename
Substitute"dog"with"cat"onlinesthatdon'tcontainword"pet"
awk'length<50'filename
Printthelineslessthan50characters
awk'NR==20,NR==30'filename
Printthelines20through30
awk'NR==50{print;exit}'filename
Printtheline50
Substitution
awk'{gsub(/dog|cat|bird,"pet");print}'filename
Find/replacedog,catorbirdwithpetandprint
awk'{gsub("dog","cat",$0);print>FILENAME}'*.txt
Find/replacedogwithcatineveryfilewithextensiontxt
Counting
awk'/virtual/{n++};END{printn+0}'filename
Printthetotalnumberoflinesthathavethenamevirtual
psaux|awk'/program_name/&&!/awk/{print$2}'>kill
Findaprogrambynamefromprocesslistingthatisnotawkand
killit
awk'{print$7}'logfile|sort|uniqc|sortrn|grep"\.jpg"|head
Useawktopulltheseventhfieldofeachlineofthelogfile.Sort
thefieldsthenfindtheuniquefieldsandcountthem,doareverse
sortonnumericcount,filteroutanythingbutJPEGfilesandonly
givemethetop10ofthatlist.uniquehostnamesorurlsfroma
logfile
Numbering
awk'{printFNR"\t"$0}'files*
Precedeeachlinewithnumberfortheline