# Awk Hash Function.

**URL:** <https://community.unix.com/t/awk-hash-function/205049>\
**Category:** Shell Programming and Scripting\
**Created:** [July 16, 2008, 5:01am UTC](https://community.unix.com/t/awk-hash-function/205049 "2008-07-16T05:01:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinjo\_jo](https://community.unix.com/letter_avatar/dinjo_jo/32/6_449c533f586939b6e5f246632a46421f.png) [@dinjo\_jo](https://community.unix.com/u/dinjo_jo)\
**Post date:** [July 16, 2008, 5:01am UTC](https://community.unix.com/t/awk-hash-function/205049/1 "2008-07-16T05:01:27Z")

</div>

I have a file with a format of

A,2  
B,2  
G,3  
A,2  
A,3  
A,2  
D,7  
A,2  
E,2  
A,2

I need to create a sum of each alphabet with the numbers assigned to it using awk.

---

<div class="post-metadata">

**Author:** ![Tytalus](https://community.unix.com/user_avatar/community.unix.com/tytalus/32/87_2.png) [@Tytalus](https://community.unix.com/u/Tytalus)\
**Post date:** [July 16, 2008, 5:44am UTC](https://community.unix.com/t/awk-hash-function/205049/2 "2008-07-16T05:44:08Z")

</div>

```nohighlight
# awk -F"," '{cnt[$1]+=$2}END{for (x in cnt){print x,cnt[x]}}' infile
A 13
B 2
D 7
E 2
G 3

```

---

<div class="post-metadata">

**Author:** ![ramen\_noodle](https://community.unix.com/letter_avatar/ramen_noodle/32/6_449c533f586939b6e5f246632a46421f.png) [@ramen\_noodle](https://community.unix.com/u/ramen_noodle)\
**Post date:** [July 16, 2008, 6:30pm UTC](https://community.unix.com/t/awk-hash-function/205049/3 "2008-07-16T18:30:23Z")

</div>

Did you want a pseudo hash algorithm and checksum based on line content or a simple  
occurrence sum (already provided above)?  
Here's the former.

```nohighlight
 
function makesimplehash(data,seedval, n,x,hashval,store,p) {

                 for (n = 1 ; data[n] != 0 ; n++) {
                     x = sprintf("%d",data[n])
                     if (x == 0) {
                                 gencharsarr(store)
                                 for (p=0 ; p < 10 ; p++) {
                                     if (store[p] == data[n]) {x = p + 1; break;}
                                 }
                     }
                     #printf "Derived value %d from %d\n",x,data[n]
                     hashval += int(x + seedval % (n + 1))
                 }
                 return hashval
}         

function zeroarr(data,nv, x) {
                while (x < nv) {data[x] = 0; x++}
}

function gencharsarr(data, n) {
str="abcdefghijk"
                    for (n=0 ; n < 10 ; n++) {data[n]=substr(str,n,1)}
}

function createhashable(szl, store,str,vn) {
store[0]=""
                        gencharsarr(store)
                        for (vn=0 ; vn < szl + 1; vn++) {
                            if (vn == 0) {str = store[int(1 + rand() * 9)]; continue}
                            str = str "," int(1 + rand() * 10)
                        }
                        return str
} 
                           

BEGIN {
arra[0]=""
cnt=0;sz=0
       if (ARGC == 2 && ARGV[1] == "-generate") {
          srand()
          while (cnt < 20) {
                sz = int(1 + rand() * 7)
                print createhashable(sz)
                cnt++
          }
          exit
        }
}

{ 
FS=","
         
         if ( (num = split($0,arra)) == 0) {printf "At record number %d, malformed field content.\n",NR ; next}
         arra[num + 1] = 0;
         #for (a = 1 ; a <= num ; a++) {print a,arra[a]}
         printf "Hash value for %s = %d.\n",$0,makesimplehash(arra,15003)
         zeroarr(arra,num)
}

```
