LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 08-24-2009, 05: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, 06: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, 06:36 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,677

Rep: Reputation: 426Reputation: 426Reputation: 426Reputation: 426Reputation: 426
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, 06:42 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,003

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Perl] Sort a file by column Kunsheng Programming 4 04-24-2009 09:09 AM
[SOLVED] Unable to sort by column in KDE 4.2.2 bassmadrigal Slackware 5 04-18-2009 05:22 AM
sort on a single column? baidym Programming 3 01-03-2009 08:46 AM
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 01:27 PM
Can't get "sort' to work on a particular column smkamene Programming 11 06-30-2006 10:37 AM


All times are GMT -5. The time now is 11:45 PM.

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