Using Awk to sum the list of output:

ps aux | awk '{sum +=$4}; END {print sum}'

Print Line that match a pattern:

awk ‘/string1/ > /string2/’  filename

Print specific fields :

awk ‘{print $1,$3,$5,$NF;}’ /etc/passwd

[ $NF means the last field ]

Initialization and Finalization:

BEGIN {actions}

{ACTION}#process everyline

END{ actions}

[#=comment for awk]

eg. ps aux |  grep -v grep | awk ‘{print $4}’ | awk ‘{total = total +$1}END{print total}

or ps aux | awk ‘{sum +=$4}; END {print sum}’

Find Value more than some value :

awk ‘$1 > 30’  somefilename

[$1 = first field ]

Print the pattern from a list :

awk ‘$3′ ~/string/’       filename

Count the number of matching pattern :

awk  ‘BEGIN  { count = 0;}

$3 ~ /string/ {count++;}

END { print “Number of Total Count : = “,count;}’   filename

[$3 = third field , ~ is for comparing with regular expressions ]

Examples :

awk -F ‘:’ ‘{ total += NF }; END { print total }’ /etc/paswd [count number of fields in a file ]

awk -F ‘:’ ‘$NF ~ //bin/bash/ { n++ }; END { print n }’ /etc/passwd  [ number of users using /bin/bash shell ]

awk -F ‘:’  ‘$3 > maxuid { maxuid=$3; maxline=$0 }; END { print maxuid, maxline }’ /etc/passwd  [ find user who has highest user id  ]

awk ‘NR % 2 == 0’ /etc/passwd  [ print even numbered lines ]

$awk -F ‘:’ ‘$3==$4’  /etc/passwd [ print user having same uid and gid ]

awk -F ‘:’ ‘$3>=500 && $NF ~ //bin/bash/’  /etc/passwd  [ user who has userid greater or equal to 500 and shell is /bin/bash ]

awk -F ‘:’ ‘$5 == “” ‘  /etc/passwd  [ user who doesnot have comment ]