Skip to content

Instantly share code, notes, and snippets.

@neilkod
Created May 29, 2012 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilkod/2830385 to your computer and use it in GitHub Desktop.
Save neilkod/2830385 to your computer and use it in GitHub Desktop.
simple histogram in awk
nkodner@hadoop4 tmp$ cat coins.txt
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.1 1986 PRC Panda
silver 1 1986 USA Liberty dollar
gold 0.25 1986 USA Liberty 5-dollar piece
silver 0.5 1986 USA Liberty 50-cent piece
silver 1 1987 USA Constitution dollar
gold 0.25 1987 USA Constitution 5-dollar piece
gold 1 1988 Canada Maple Leaf
nkodner@hadoop4 tmp$ awk -f funcs.awk coins.txt
Number of records per country:
Austria-Hungary 1 *
USA 7 *******
Canada 1 *
Switzerland 1 *
PRC 1 *
RSA 2 **
nkodner@hadoop4 tmp$ cat funcs.awk
function hist(n)
{
ret = ""
for(i=1; i<=n; i++)
{
ret = ret "*"
}
return ret
}
{ countries[$4]+=1
} END {
print "Number of records per country:";
for (x in countries) printf " %-15s %-3d %s\n", x, countries[x],hist(countries[x])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment