The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Locality Sensitive Hash Function Based on Concomitant Rand Order Statistics iBot UNIX and Linux RSS News 0 07-07-2008 06:10 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 12:39 PM
Perl Hash Harikrishna Shell Programming and Scripting 1 06-03-2008 12:45 AM
MD5 hash calculation zedex High Level Programming 4 02-24-2007 02:37 AM
Hash (#) as a variable errolg UNIX for Dummies Questions & Answers 5 05-21-2002 04:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2008
dinjo_jo dinjo_jo is offline
Registered User
  
 

Join Date: May 2008
Posts: 73
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.
  #2 (permalink)  
Old 07-16-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
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
  #3 (permalink)  
Old 07-16-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
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)
}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0