LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 04-21-2013, 10:59 PM   #1
castor0troy
LQ Newbie
 
Registered: Apr 2013
Posts: 27

Rep: Reputation: Disabled
using the sort command


need some help on sort command.

keyword file looks like
cat 2
ball 55
bat 4

i want to sort top 30 keywords only, ascending to desceding based on numeric value of the keyword.
will this work?
sort -k 2 -g -r file >output.txt

output.txt will be
ball 55
bat 4
cat 2
 
Old 04-21-2013, 11:35 PM   #2
jdkaye
Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,120

Rep: Reputation: Disabled
Hi castor,
Is this homework? If so, please read the guidelines concerning posting homework questions on LQ.

Quote:
will this work?
sort -k 2 -g -r file >output.txt
Why not try it out and see?
jdk
 
Old 04-21-2013, 11:36 PM   #3
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,706

Rep: Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271Reputation: 1271
Hi,

looks ok, but if you only want the first 30, you should probably pipe it through "head -30".

Evo2.
 
Old 04-22-2013, 12:03 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Are you stuck somewhere? Did you try:
Code:
~$ sort -nr -k2 | head -30
Check: http://linux.die.net/man/1/sort
 
Old 04-22-2013, 12:21 AM   #5
castor0troy
LQ Newbie
 
Registered: Apr 2013
Posts: 27

Original Poster
Rep: Reputation: Disabled
Guys.
no this wasnt a homework.
actually i was working on this command yesterday but didnt have server access to test it.
i just tried the head option and it worked fine.

thanks people
 
Old 04-22-2013, 06:16 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Great. You can Mark the thread as solved (option is under Thread Tools on top menu), if you think it has so.
 
Old 04-23-2013, 10:01 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
Quote:
Originally Posted by castor0troy View Post
i want to sort top 30 keywords only, ascending to desceding based on numeric value of the keyword.
This needs clarification. Do you want to take the first 30 entries and then sort only them? Or do you want to sort all the entries and then take the top 30? the order of the commands is different for each.

Code:
head -n 30 input.txt | sort -k2nr >output.txt
Code:
sort -k2nr input.txt | head -n 30 >output.txt
 
  


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
[SOLVED] sort command help anu_1 Linux - Newbie 4 01-29-2010 04:34 AM
sort command fallloveuni Programming 3 01-24-2010 08:31 PM
Sort command djfog Linux - Newbie 17 07-03-2008 03:05 AM
Sort command arya6000 Linux - Newbie 2 11-27-2007 07:50 PM
The SORT command Rezon Programming 2 10-30-2003 04:14 PM


All times are GMT -5. The time now is 08:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration