Loading...
 

AWK

Name

Synopsis

Special Commands, characters and variables

Variables

OFS = Output Field Separator
NR = number of records (i.e. if you grep for something, this will be the count of how many were found)

Options

-v allows you to pass bash variables into an awk variable. I.e. awk -v d=$DIR ‘{print “Directory = “d}’

Examples

specify a field separator

$ grep forSomething fromSomeFile| awk 'BEGIN{OFS=" - ";}{print $1,$4,$6}'
Field1 - Field4 - Field6

How to print the the whole line based on the value of column 5

awk '{ if ($5 > 0) print $0}' myFilename

work out average from grepped results

awk 'BEGIN{s=0;}{s=s+$1;}END{if (NR=="0") print 0; else print int(s/NR + .5);}')
awk -F\/ 'match($14, /[[:alpha:]].*/) {print substr($14, RSTART, RLENGTH)}'

calculate column averages from REGEX criteria

awk 'BEGIN{s=0;}{if (match($4, /[[:digit:]].*/)) {s=s+$4;}}END{if (NR=="0") print 0; else print s/NR;}' tmp/ioBefore
1.34825

Split a string by a defined divider

port becomes an array of variable after splitting the string by :

awk '{split($4, port, ":")}{print port[2]}'