LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2007, 08:49 PM   #1
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Rep: Reputation: 0
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
 
Old 06-10-2007, 09:47 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -F ":" '
{ 
  fchar = substr($1,0,1)
  ++cnt[fchar]
}
END{
  for (i in cnt) {
     print i "=>" cnt[i] | "sort"
  }
}' /etc/passwd
 
Old 06-10-2007, 10:09 PM   #3
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
thanx a lot ghostdog74 works perfectly!
 
Old 06-11-2007, 07:07 AM   #4
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
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?
 
Old 06-12-2007, 01:07 PM   #5
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
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
 
Old 06-13-2007, 07:09 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort regular users and system users from /etc/passwd joeyBig Red Hat 9 05-29-2008 01:59 AM
[PHP] Get last occurence of x from a log file GavB Programming 5 06-11-2007 11:49 AM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 08:18 PM
vsftpd-fedora core 5-virtual users-Documentations sort of for me. sarina1975 Linux - General 2 08-25-2006 01:55 PM
LXer: Open Letter: How the FOSS Community May Help Disabled Users LXer Syndicated Linux News 0 04-22-2006 07:21 PM


All times are GMT -5. The time now is 04:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration