Exercise #8: Counting and Sums with awk

by mike on February 19, 2011

You can use an awk script to increment or count the total number of occurrences of a specific aspect. The  awk operators post helps explain some of the options you may consider. The example below starts with a BEGIN section that can use several different types of field separators. Then in the main body it searches for the text string “httpd” which is the Apache web server and counts those. Each time awk loops through the records it increments the count. Then in the END section the final Apache count can be provided.

awk file: count

BEGIN {FS="[ :\t]"}
/httpd/{count++}
END {print "Apache Servers Now Running: "count; print "Total Number of Records: " NR}

In this situation a live count can be created on a running system.

ps aux | awk -f count

Apache Servers Now Running: 9
Total Number of Records: 74

awk file: sum
In this example the sum of all memory use is taken from field number 4 and then used in the END section to print it with defining text.

BEGIN {print "#####################################" }
{sum += $4}
END {print "Total Memory Usage by Processes: " sum"%"}


awk -f sum processes

#####################################
Total Memory Usage by Processes: 6.2%

awk file: websum
This example collects all memory use by the web server (httpd) and sums it at the end.

BEGIN {print "#####################################" }
/httpd/ {sum += $4}
END {print "Total Memory Usage by the Web Server: " sum"%"}


awk -f websum processes
#####################################
Total Memory Usage by the Web Server: 1.7%

awk file: disksum
The goal of this awk file is to create a sum of the size of a specific type of file, in this example all “.conf” files in the /etc directory. The sum is then divided by 1024 in order to provide MB for easier understanding.

BEGIN {print "#####################################" }
{sum += $5}
END {print "Total File Size: " sum/1024/1024" MB"}


ls -lh /etc/*.conf
-rw-r–r– 1 root root 1.8K Jan 15 21:40 /etc/gpm-root.conf
-rw-r–r– 1 root root 17 Jan 15 21:40 /etc/host.conf
-rw-r–r– 1 root root 658 Jan 15 21:40 /etc/initlog.conf
-rw-r–r– 1 root root 31K Jan 15 21:40 /etc/jwhois.conf
-rw-r–r– 1 root root 608 Jan 15 21:40 /etc/krb5.conf
-rw-r–r– 1 root root 28 Jan 15 21:40 /etc/ld.so.conf
-rw-r—– 1 root root 191 Jan 15 21:40 /etc/libaudit.conf
-rw-r–r– 1 root root 2.5K Jan 15 21:40 /etc/libuser.conf
-rw-r–r– 1 root root 520 Jan 15 21:40 /etc/logrotate.conf
-rw-r–r– 1 root root 330 Jan 15 21:40 /etc/mke2fs.conf
-rw-r–r– 1 root root 2.7K Jan 15 21:40 /etc/multipath.conf
-rw-r—– 1 root root 1.3K Jan 15 21:40 /etc/named.caching-nameserver.conf
-rw-r–r– 1 root root 1.9K Jan 15 21:40 /etc/nscd.conf
-rw-r–r– 1 root root 1.7K Jan 15 21:40 /etc/nsswitch.conf
-rw-r–r– 1 root root 24 Feb 8 10:42 /etc/resolv.conf
-rw-r–r– 1 root root 84K Jan 15 21:40 /etc/sensors.conf
-rw-r–r– 1 root root 216 Jan 15 21:40 /etc/sestatus.conf
-rw-r–r– 1 root root 1010 Jan 15 21:40 /etc/sysctl.conf
-rw-r–r– 1 root root 694 Jan 15 21:40 /etc/syslog.conf
-rw-r–r– 1 root root 136 Jan 15 21:40 /etc/updatedb.conf
-rw-r–r– 1 root root 2.6K Jan 15 21:40 /etc/warnquota.conf
-rw-r–r– 1 root root 1001 Jan 15 21:40 /etc/xinetd.conf
-rw-r–r– 1 root root 346 Jan 15 21:40 /etc/yum.conf

ls -l /etc/*.conf | awk -f disksum
#####################################
Total File Size: 0.130379 MB

Comments on this entry are closed.

Previous post:

Next post: