LinuxQuestions.org
Help answer threads with 0 replies.
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 06-22-2010, 05:45 AM   #1
mohamad
LQ Newbie
 
Registered: Jun 2010
Posts: 11

Rep: Reputation: 0
Thumbs up sorting


hello
i wanna sort the 3rd column in a table numerically ( no actual borders...only tabs seperating the columns)
it should be something like this but i cant get it right

Sort -u +1 -3 results

the file is called results
 
Old 06-22-2010, 06:01 AM   #2
jcomeau_ictx
LQ Newbie
 
Registered: Aug 2004
Location: Petaluma, CA, US
Distribution: Debian GNU/Linux squeeze/sid
Posts: 29

Rep: Reputation: 17
Quote:
Originally Posted by mohamad View Post
hello
i wanna sort the 3rd column in a table numerically ( no actual borders...only tabs seperating the columns)
it should be something like this but i cant get it right

Sort -u +1 -3 results

the file is called results
sort -n -k 3 results
 
Old 06-22-2010, 06:36 AM   #3
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
thanks jcomeau_ictx
i learnt that if we have a file containing table such that columns are separated by tabs then
Code:
$sort -n -k 3 input
means
-n numeric sort
-k 3 the the sorting column is third
I think only one thing should be kept in mind that elements of column before sort column must not contain space or tab as that is what whitespace is & is used in counting.

Last edited by sumeet inani; 06-22-2010 at 06:39 AM.
 
Old 06-22-2010, 07:16 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by sumeet inani View Post
I think only one thing should be kept in mind that elements of column before sort column must not contain space or tab as that is what whitespace is & is used in counting.
The -t option could help in this case. Anyway, for a TAB delimited file it's necessary some trick, since sort -t does not accept the \t notation. For example:
Code:
sort -t $'\t' -k3n file
or by typing a literal TAB using the Ctrl-V + TAB sequence to skip the shell's TAB completion:
Code:
sort -t "<Ctrl-V><TAB>" -k3n file
 
Old 06-22-2010, 10:53 PM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
Thanks colucix
Thus we can make table whose columns are separated by tabs provided the content of cell (atleast those which occur before sort column in the row) does not contain tab only spaces.

Why do you use -k3n ?

From what I understand in man page
-k startField,endField
When we don't mention end field then if content of sort column for two rows match then next column decides & so on...

Last edited by sumeet inani; 06-22-2010 at 11:12 PM.
 
Old 06-22-2010, 11:08 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I have a hunch that "-k3n" is equivalent to "-k3 -n"

In general you can combine these "switches"--eg "ls -al" is the same as "ls -a -l" (And of course the ubiquitos "tar -xzvf"

Man pages plus experiments is the way to get your head around some of this.......
 
Old 06-23-2010, 12:45 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@sumeet inani: yes -k3n is a sort of combination of two options - as pixellany pointed out. However the start position and the end position can be more detailed, as explained in the man page when it talk about POS:
Code:
-k, --key=POS1[,POS2]
       start a key at POS1, end it at POS2 (origin 1)
<...>
POS is F[.C][OPTS], where F is the field number and C the character position  in  the  field;
both are origin 1.  If neither -t nor -b is in effect, characters in a field are counted from
the beginning of the preceding whitespace.   OPTS  is  one  or  more  single-letter  ordering
options,  which  override  global ordering options for that key.  If no key is given, use the
entire line as the key.
In -k3n we have F = 3, C = undef and OPTS = n.
 
Old 06-23-2010, 06:46 AM   #8
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
Understanding -k3n needs word by word reading of man page.
seems -k3n is similiar to -k 3 -n
Later is easier (though --key=3 --numeric-sort would be easiest but tedious) to understand & former fast to type.
Advanced users can write in cryptic ways.
I like simplicity.
Everyone has his way .

Last edited by sumeet inani; 06-23-2010 at 06:49 AM.
 
  


Reply

Tags
sort



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
JTable sorting manolakis Programming 1 07-30-2009 06:05 PM
Java sorting manolakis Programming 2 07-21-2008 11:28 AM
Sorting data. unreal128 Linux - Software 1 09-20-2005 06:31 PM
Which sorting algorithm? nodger Programming 6 01-28-2005 06:13 PM
Sorting Beppe83 Linux - Software 7 06-21-2004 09:10 AM

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

All times are GMT -5. The time now is 04:52 AM.

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