Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-03-2012, 07:16 PM   #1
LQ Newbie
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
sort by column with null value

I have a TAB delimited file like this (using \t here since the forum does not display TAB):


Values in each column could be numbers or strings and I need to sort it by columns 1, 2, and then 3. Since the 2nd column may have null value, "sort -u" does not give the correct result, i.e., it ignores the blank field and compare the value in the 3rd column with the value in the 2nd one. The result is as shown above. For some reason "sort -n" doesn't work either, although values (when there is one) the 2nd column is numeric. I figured this command works properly:

>sort -n -k1 -k2 -k3 filename

But is there a simpler way to do this instead of listing all the columns? I also tried "sort -n -k1,3" but it didn't work.

Thanks for your help.

Last edited by sophiesu; 01-03-2012 at 07:22 PM.
Old 01-04-2012, 01:34 PM   #2
Senior Member
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi sophiesu,

Welcome to LQ!!!

Well LQ!! does allow you to use tab you need to put the content inside [code] and then end it with /code witin brackets. code save the spaces in the content that you paste.

As far as your query is concerned below command should work for you

cat /filename | cut -d ":" -f 1-5 | sort -n -k1
In the above command I am passing the output of cat /filename command to cut command which will then delimit (-d) on the basis of colon ":" (you can specify your own delimiter. Field (-f) I am using columns 1-5 you can specify columns as per your requirement. Then I am sorting it in ascending order on the basis of column 1.


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
how to sort the 2nd column on the basis of first column without repeating the value ? zediok Linux - Newbie 15 12-20-2011 12:48 PM
Multiple column sort brownflamigo1 Programming 7 09-15-2011 10:05 PM
unix sort on column PMP Linux - Newbie 3 08-24-2009 07:42 AM
[Perl] Sort a file by column Kunsheng Programming 4 04-24-2009 10:09 AM
sort on a single column? baidym Programming 3 01-03-2009 09:46 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration