Back to the main page

awk example - show hostname per state

This awk program will show list of hostnames per States and sort States by their name.

This example can give you idea for similar tasks or to be included as part of a bigger script.

Say we have file with info about: hostname, OS, machine type and location, like some very simple hardware database.

hostname-1, OS Windows 2K, HP Prolient, California
hostname-2, OS Windows XP, HP Prolient, Florida
hostname-3, OS Debian Linux, no name PC, New York
hostname-4, OS Solaris, SunFire V240, California
hostname-5, OS RedHat, HP Prolient, Massachusetts
hostname-6, OS Windows 2K, Toshiba laptop, Kansas
hostname-7, OS Suse Linux, Asus PC, Alabama
hostname-8, OS Windows 2K, HP Prolient, Hawaii
hostname-9, OS Windows 2K, no name PC, Hawaii
hostname-10, OS Windows 2K, HP Prolient, Oregon
hostname-11, OS FreeBSD, no name server, Iowa
hostname-12, OS Windows 2K, HP Prolient, Maine
hostname-13, OS Windows 2K, HP Prolient, Hawaii
hostname-14, OS Windows 2K, HP Prolient, Oregon
hostname-15, OS Windows 2K, HP Prolient, Kansas
1. First, add state name on the front, since we want to determine hostname per state. Also sort output.

# cat hardware.txt | awk -F, '{print $4 ", " $0}' | sort
 Alabama, hostname-7, OS Suse Linux, Asus PC, Alabama
 California, hostname-1, OS Windows 2K, HP Prolient, California
 California, hostname-4, OS Solaris, SunFire V240, California
 Florida, hostname-2, OS Windows XP, HP Prolient, Florida
 Hawaii, hostname-13, OS Windows 2K, HP Prolient, Hawaii
 Hawaii, hostname-8, OS Windows 2K, HP Prolient, Hawaii
 Hawaii, hostname-9, OS Windows 2K, no name PC, Hawaii
 Iowa, hostname-11, OS FreeBSD, no name server, Iowa
 Kansas, hostname-15, OS Windows 2K, HP Prolient, Kansas
 Kansas, hostname-6, OS Windows 2K, Toshiba laptop, Kansas
 Maine, hostname-12, OS Windows 2K, HP Prolient, Maine
 Massachusetts, hostname-5, OS RedHat, HP Prolient, Massachusetts
 New York, hostname-3, OS Debian Linux, no name PC, New York
 Oregon, hostname-10, OS Windows 2K, HP Prolient, Oregon
 Oregon, hostname-14, OS Windows 2K, HP Prolient, Oregon
2. Okay, now after sort, assign variable 'last' to first state (in first line) and print its hostname.

Program goes to next line. If state is same as variable 'last', program just prints new hostname.
If state is different, program prints state and hostname. There is 'tab' between them.

So whole (short) program looks like:

# cat hardware.txt | awk -F, '{print $4 ", " $0}' | sort | awk -F, '$1 == last { print "\t" $2 } $1 != last { last = $1; print $1; print "\t" $2 }'

And result is:
 Alabama
         hostname-7
 California
         hostname-1
         hostname-4
 Florida
         hostname-2
 Hawaii
         hostname-13
         hostname-8
         hostname-9
 Iowa
         hostname-11
 Kansas
         hostname-15
         hostname-6
 Maine
         hostname-12
 Massachusetts
         hostname-5
 New York
         hostname-3
 Oregon
         hostname-10
         hostname-14

Back to the main page