Skip to content

Instantly share code, notes, and snippets.

@neilkod
Created May 29, 2012 20:03
Show Gist options
  • Select an option

  • Save neilkod/2830385 to your computer and use it in GitHub Desktop.

Select an option

Save neilkod/2830385 to your computer and use it in GitHub Desktop.

Revisions

  1. neilkod created this gist May 29, 2012.
    41 changes: 41 additions & 0 deletions funcs.awk
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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])
    }