Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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.
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:
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.
@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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.