LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-14-2010, 12:47 PM   #1
flea89
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:

Code:
#!/bin/bash


echo "The script starts now."

echo "Hi, $USER!"


echo "Count of logins for each user:"
#!/bin/sh
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, 12:57 PM   #2
AlucardZero
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, 01:02 PM   #3
flea89
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, 06:00 PM   #4
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
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
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration