Here are some command line examples, which i’ve used over time.

  1. Add 2nd & 10th values for matching pattern “Eg: eth0 in /proc/net/dev”

    TOT=$(awk ‘{ if ($1 ~ /eth0/) print $2 + $10 }’ < /proc/net/dev)

  2. Add all the values for a matching pattern “Eg:- proc3 in /proc/net/rpc/nfs”

      nfs_tot=$(awk ‘{ if ($1 ~ /^proc3/) {s = 0; for (i = 1; i <= NF; i++) s = s+$i; print s} }’  < /proc/net/rpc/nfs)

  3. Awk by custom file separator.

    cat /tmp/i | awk ‘BEGIN { FS=”Plugin by”; } { print $1; }’

  4. Sort IP. I have a DNS file,  with 4th field listed by IP. Need to sort by IP.

    grep -v ‘^;’ /var/named/hosts-cassens  | awk ‘{ print $4 }’ | grep ‘140.40’ | sort -u -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4| more

  5. Add the entire Nth COLUMN in a output.

    Print the total size of all the log file named “syslog*”
    #> find /var/log -name ‘syslog*’ -printf %k”\n” | awk ‘{  sum += $1 } END { print sum }’