Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

letter frequency count

Status
Not open for further replies.

SerJel

Programmer
Joined
Nov 23, 2007
Messages
6
Location
EE
Ez, can anyboby help me to write a code in AWk, which will count the letter frequency in text. My code:
#!/usr/bin/awk -f
{
for (i = 1; i <= NF; i++)

freq[$i]++
}

END {
for (words in freq)
split(words,word,"");
for (s in word)
#if (word=word)
print word, freq[words]}
What can be wrong?
 
The following should work with gawk:

Code:
BEGIN { FS="" }
{
   for (i=1; i <= NF; i++) {
      if ($i != " ")
         letter[tolower($i)]++
   }
}
END {
     j = 1
     for (i in letter) {
         ind[j] = i
         j++
     }
     n = asort(ind)
     for (i = 1; i <= n; i++)
         printf "%s: %s\n", ind[i], letter[ind[i]]
}
 
thank you! it works :)
 
How i can now to decrypt some decrypted text uses letter frequency tables?
 
How i undesrtand i need to sort letter[ind] and the most bigger will be "e". But how to do that?
 
Ok, i have sorted the letters by frequency, but how can i to do that the most bigger is "e", and to put that "e" into text?
#!/usr/bin/awk -f

function swapa(arr,i,j, tmp) {
tmp=arr;
arr=arr[j];
arr[j]=tmp;
}
function _qsort(arr,keys,i,j, k,i2,j2) {
if(j-i<1)
return

i2=i; j2=j;
# pivot, later algorithm depends on i here,
# so be careful if going to change it
k=arr[keys];

while(i2<j2) {
# push i2 to the right until there is k greater element
while(i2<=j && arr[keys[i2]]<=k) i2++;
# push j2 to the left until there is k less/equal element
while(j2>=i && arr[keys[j2]]>k) j2--;

if(i2<j2)
swapa(keys, i2, j2);
}

# if i2>j then there were no elements greater than k
# this is special case where we assume k as greatest element
# and place it to the end of the list
if(i2>j) {
swapa(keys, i, j);
i2--; j2--;
}

# recurse for subarrays
_qsort(arr, keys, i, j2);
_qsort(arr, keys, i2, j);
}

function qsort(arr,keys, n,i,k) {
for(k in arr) {
keys[++i] = k
}
n=i;

_qsort(arr, keys, 1, n);

return n;
}

BEGIN { FS="" }
{
for (i=1; i <= NF; i++) {
if ($i != " ")
letter[tolower($i)]++
}
}

END {
j = 1
for (i in letter) {
ind[j] = i
j++}
n=qsort(letter,ind);
for(i=1; i<=n; i++) {
print ind,letter[ind];
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top