cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Cloud



Etc


Reference














.

os:linux:command:awk.html



Linux Commands Cheat Sheet

awk - How to use awk Command in Linux

man awk

NAME
       gawk - pattern scanning and processing language


Example

Operation Command
1st column awk '{ print $1 }' FILE
1,4 column awk '{ print $1 $4 }' FILE
2 column , “:” is filed sepalater awk -F: '{ print $2 }'

#echo "1 2 3 4 5" | awk '{ print $1,$3 }'
1 3
#

#echo "1 2 3 4 5" | awk '{ print $1 $3 }'
13
#

#echo "1 2 3 4 5" | awk '{ print $1 "," $3 }'
1,3
#

#echo "1 2 3 4 5" | awk '{ print $NF }'
5
#

#echo "1 2 3 4 5 6 7 8" | awk '{ {for(i=2;i<NF;i++)printf("%s ",$i) }print($NF) }'
2 3 4 5 6 7 8
#

#echo "1 2 3 4 5" | awk '{print $1 "        " $4 }'
1        4
#

#echo "1 2 3 4 5" | awk '{print $1 "\t" $4 }'
1       4
#

#echo "1 2 : 3 4 : 5" | awk -F: '{ print $2 }'
 3 4
#


TIPS

How to avoid single quotes

How to escape
awk '{print " '\'' " }'
How to enclose in double quotes
awk "{print \"'\" }"

#You need to escape the "$"
How to specify by ASCII code
awk '{print "\x27"}'
awk '{print "\047"}'


Totalization

counttime (){
   #test
   #cat $1 | awk -v min=$2 -v max=$3 'BEGIN{i=0,FS=",";OFS=","} {if($4 >= min && $4 < max) print $4}'
   cat $1 | awk -v min=$2 -v max=$3 'BEGIN{i=0,FS=",";OFS=","} {if($4 >= min && $4 < max) i++} END {print min"ms",max"ms",i "count";}' >> $OUTPUT
}

countnum () {
   counttime $2 0 100
   counttime $2 100 200
   counttime $2 200 300
   counttime $2 300 400
   counttime $2 400 500
   counttime $2 500 10000
}


NULL

awk -F, '{ if($9=NULL) print $6 "," $9 }' 

awk -F, '{ if($9!=NULL) print $6 "," $9 }' 


ETC

cat httpd.log | awk -F'\t' '$14=="JP" {print $3}' 
vmstat 1 86400 | awk '{print strftime("%Y/%m/%d %H:%M:%S"), $0} { system(":") }' >> vmstat.`date +%Y%m%d`








Linux Commands Cheat Sheet




os/linux/command/awk.html.txt · Last modified: 2022/03/09 by admin

Page Tools