LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sort users begining with each letter of the alphabet from greatest to least occurence (https://www.linuxquestions.org/questions/linux-newbie-8/sort-users-begining-with-each-letter-of-the-alphabet-from-greatest-to-least-occurence-560727/)

nadeemr 06-10-2007 07:49 PM

sort users begining with each letter of the alphabet from greatest to least occurence
 
hi i was just wondering how to sort users beginning with each letter of the alphabet from greatest to least occurrence?

i have something like this :


sort -t, -k1,2n -k4,5rn /etc/passwd

but how would i specify how to determine which letter of the alphabet is being called?

my output should be something like :
c => 3
b => 2
d => 2
a => 1
...
z => 0

some help would be much appreciated

ghostdog74 06-10-2007 08:47 PM

Code:

awk -F ":" '
{
  fchar = substr($1,0,1)
  ++cnt[fchar]
}
END{
  for (i in cnt) {
    print i "=>" cnt[i] | "sort"
  }
}' /etc/passwd


nadeemr 06-10-2007 09:09 PM

thanx a lot ghostdog74 works perfectly!

nadeemr 06-11-2007 06:07 AM

question : code from ghostdog74 works fine but how do i sort it from greatest to least occurrence?

i tried :
awk -F ":" '
{
fchar = substr($1,0,1)
++cnt[fchar]
}
END{
for (i in cnt) {
print i "=>" cnt[i] | "sort"
}
}' /etc/passwd |grep -nr

but it just reverses the order not the sorting from greatest to least.
any idea?

nadeemr 06-12-2007 12:07 PM

Quote:

Originally Posted by nadeemr
question : code from ghostdog74 works fine but how do i sort it from greatest to least occurrence?

i tried :
awk -F ":" '
{
fchar = substr($1,0,1)
++cnt[fchar]
}
END{
for (i in cnt) {
print i "=>" cnt[i] | "sort"
}
}' /etc/passwd |grep -nr

but it just reverses the order not the sorting from greatest to least.
any idea?


please give me some hints on how to sort this code in order that it displays from greatest occurrence to least

druuna 06-13-2007 06:09 AM

Hi,

This should work:
Code:

nawk -F ":" '
{
  fchar = substr($1,0,1)
  ++cnt[fchar]
}
END{
  for (i in cnt) {
    print i " => " cnt[i] | "sort +2 -r"
  }
}' /etc/passwd

Output example run:
Code:

b => 5
s => 5
n => 4
d => 3
l => 2
m => 2
r => 2
a => 1
e => 1

ps: the sort used within awk is the normal sort program.

Hope this helps.


All times are GMT -5. The time now is 12:19 AM.