Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Awk Hash Function.

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-16-2008
dinjo_jo dinjo_jo is offline
Registered User
 
Join Date: May 2008
Last Activity: 13 December 2011, 4:12 AM EST
Posts: 232
Thanks: 1
Thanked 6 Times in 5 Posts
Awk Hash Function.

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.
Sponsored Links
    #2  
Old Unix and Linux 07-16-2008
Tytalus's Unix or Linux Image
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 10 January 2014, 5:37 AM EST
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts

Code:
#  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

Sponsored Links
    #3  
Old Unix and Linux 07-16-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
 
Join Date: Dec 2007
Last Activity: 23 December 2008, 1:00 PM EST
Location: Virginia, USA.
Posts: 251
Thanks: 0
Thanked 1 Time in 1 Post
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.

Code:
 
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)
}

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl hash - using a range as a hash key. dsw Shell Programming and Scripting 3 08-31-2010 07:03 AM
Perl Hash:Can not keep hash data in the same order that it was inserted jgfcoimbra Shell Programming and Scripting 1 03-23-2010 11:35 AM
Hash Function Speed killerqb Programming 7 10-22-2009 05:46 PM
Print Entire hash list (hash of hashes) Alalush Shell Programming and Scripting 1 08-06-2008 08:40 AM



All times are GMT -4. The time now is 12:54 AM.