Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org sort users begining with each letter of the alphabet from greatest to least occurence
 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

 06-10-2007, 07:49 PM #1 nadeemr LQ Newbie   Registered: May 2007 Posts: 18 Rep: 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
 06-10-2007, 08:47 PM #2 ghostdog74 Senior Member   Registered: Aug 2006 Posts: 2,697 Blog Entries: 5 Rep: Code: awk -F ":" ' { fchar = substr(\$1,0,1) ++cnt[fchar] } END{ for (i in cnt) { print i "=>" cnt[i] | "sort" } }' /etc/passwd
 06-10-2007, 09:09 PM #3 nadeemr LQ Newbie   Registered: May 2007 Posts: 18 Original Poster Rep: thanx a lot ghostdog74 works perfectly!
 06-11-2007, 06:07 AM #4 nadeemr LQ Newbie   Registered: May 2007 Posts: 18 Original Poster Rep: 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?
06-12-2007, 12:07 PM   #5
LQ Newbie

Registered: May 2007
Posts: 18

Original Poster
Rep:
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

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

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post joeyBig Red Hat 9 05-29-2008 12:59 AM GavB Programming 5 06-11-2007 10:49 AM tworkemon Linux - Newbie 8 01-30-2007 07:18 PM sarina1975 Linux - General 2 08-25-2006 12:55 PM LXer Syndicated Linux News 0 04-22-2006 06:21 PM

LinuxQuestions.org

All times are GMT -5. The time now is 02:28 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -