Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-14-2010, 01:47 PM   #1
LQ Newbie
Registered: Mar 2010
Posts: 15

Rep: Reputation: 0
Sort in descending order according to a column of an array

Hello ! I have the following bash script:


echo "The script starts now."

echo "Hi, $USER!"

echo "Count of logins for each user:"
last > logs.txt
awk '{count[$1]++}END{for(j in count) print j, "("count[j]"logins)"}' logs.txt
What it does is count logins to my network and print on screen the total
of logins each user has.
How can I have my results in descending order according to the loggins of each user?
For example the result should be like:
ampaltas (80logins)
amaxilat (61logins)
altanis (70logins)
alkalai (70logins)
alexopo (20logins)
alexiou (18logins)
alexandz (17logins)
alavernt (11logins)
akribopo (11logins)
akretsis (10logins)
akis (2logins)
akatis (1logins)
Old 03-14-2010, 01:57 PM   #2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
sort -r -n -k2

see: man sort
Old 03-14-2010, 02:02 PM   #3
LQ Newbie
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
oh thanks ! i had already read man sort but i hadnt understood how i would choose the column via k2... thanks a lot
i suppose this will go after awk, like awk '{...}' | sort -r -n -k2 ?
Old 03-14-2010, 07:00 PM   #4
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos
Posts: 282

Rep: Reputation: 55
This will work (you can ignore the first line of the output)

last | awk '{count[$1]++}END{for(j in count) print count[j] " logins" , j}' | sort -g


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 list folder size in descending order ? Winanjaya Linux - Server 2 02-09-2010 01:32 AM
Can I use GNU sort to sort one field in order, another in reverse? zombieite Linux - Newbie 4 04-27-2009 01:23 AM
php sort help - sort numerical descending then by alphabetical? RavenLX Programming 3 03-11-2009 09:35 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 11:12 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 08:44 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:10 PM.

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