LinuxQuestions.org
Latest LQ Deal: Linux Power User 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 08-24-2009, 06:56 AM   #1
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
unix sort on column


I have a file having the following lines
Code:
     7     1   10
     7     1   12
     7     1   14
     7     1   16
     7     1    2
     7     1    4
     7     1    6
     7     1    8
     7     2   10
     7     2   11
     9     2    9
     7     2   13
     7     2   15
     7     2   17
     7     2    1
     7     2    2
     7     2    3
     7     2    4
     7     2    5
     7     2    6
     7     2    7
     7     2    8
     8     2    9
I want to get it sorted by column1,column,column3
I have used
sort -nk1,3 <filename>

but i get the output as
Code:
     7     1   10
     7     1   12
     7     1   14
     7     1   16
     7     1    2
     7     1    4
     7     1    6
     7     1    8
     7     2    1
     7     2   10
     7     2   11
     7     2   13
     7     2   15
     7     2   17
     7     2    2
     7     2    3
     7     2    4
     7     2    5
     7     2    6
     7     2    7
     7     2    8
     8     2    9
     9     2    9
 
Old 08-24-2009, 07:05 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
Well Got it working with
sort -nk1 -nk2 -nk3 <filename>
Am I doing right?
 
Old 08-24-2009, 07:36 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
As the saying goes: "If it works, it's OK."

I missed what was wrong with your first version---Ah---it sorted alphabetically!!
 
Old 08-24-2009, 07:42 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by PMP View Post
Well Got it working with
sort -nk1 -nk2 -nk3 <filename>
Am I doing right?
Result is ok, but you can avoid to specify -n multiple times:
Code:
sort -n -k1 -k3 <filename>
 
  


Reply

Tags
linux, sort


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
[Perl] Sort a file by column Kunsheng Programming 4 04-24-2009 10:09 AM
[SOLVED] Unable to sort by column in KDE 4.2.2 bassmadrigal Slackware 5 04-18-2009 06:22 AM
sort on a single column? baidym Programming 3 01-03-2009 09:46 AM
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 02:27 PM
Can't get "sort' to work on a particular column smkamene Programming 11 06-30-2006 11:37 AM

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

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