LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2011, 05:11 AM   #1
BarataPT
LQ Newbie
 
Registered: Mar 2011
Posts: 12

Rep: Reputation: 1
Problems with sort


Hi,

I have a file with the following contents:
Quote:
20100228062520 10.134.53.113
20100228062520 10.134.53.114
20100228062520 10.134.53.114
20100228062520 99.247.216.111
20100228062521 10.134.53.111
20100228062521 10.134.53.114
20100228062522 201.230.227.197
20100228062524 99.247.216.111
20100228062525 10.134.53.114
20100228062525 99.247.216.110
The columns are separated by a TAB.

I want to sort by the IP address and i do this:
Code:
cat file2.txt | sort -n -k2,2 -t "    " | less
But the output isn't quite correct. It seems that it sorts only by the 1st IP number (before the first point).

Quote:
20100228062520 10.134.53.113
20100228062520 10.134.53.114
20100228062520 10.134.53.114
20100228062521 10.134.53.111
20100228062521 10.134.53.114
20100228062525 10.134.53.114
20100228062520 99.247.216.111
20100228062524 99.247.216.111
20100228062525 99.247.216.110
20100228062522 201.230.227.197
If i cut only the IPs i get the correct result

Code:
cat file2.txt | cut -d "    " -f2,2 | sort -n

Quote:
10.134.53.111
10.134.53.113
10.134.53.114
10.134.53.114
10.134.53.114
10.134.53.114
99.247.216.110
99.247.216.111
99.247.216.111
201.230.227.197
Can someone explain to me why this happen?
 
Old 03-31-2011, 06:04 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
An IP address isn't a number.

You have two types of delimiters on the same line. tabs and dots.

You could use sed or tr to convert the tab to a dot. Sort the results, and convert the dot back again.
Code:
tr '\t' '\.' <test | sort -t'.' -k2 -k3 -k4 -k5 | sed 's/\./\t/'
 
Old 03-31-2011, 06:13 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -ane 'BEGIN{a=[];b=[]} ;b<<[$F[0].to_i,$F[1]];END{b.sort_by{|x| [ x[1].split(".")[0].to_i , x[1].split(".")[-1].to_i] }.each{|x|puts x.join(" ")}}'  file
20100228062521 10.134.53.111
20100228062520 10.134.53.113
20100228062520 10.134.53.114
20100228062525 10.134.53.114
20100228062520 10.134.53.114
20100228062521 10.134.53.114
20100228062525 99.247.216.110
20100228062524 99.247.216.111
20100228062520 99.247.216.111
20100228062522 201.230.227.197


---------- Post added 03-31-11 at 06:13 AM ----------

Quote:
Originally Posted by jschiwal View Post
An IP address isn't a number.

You have two types of delimiters on the same line. tabs and dots.

You could use sed or tr to convert the tab to a dot. Sort the results, and convert the dot back again.
Code:
tr '\t' '\.' <test | sort -t'.' -k2 -k3 -k4 -k5 | sed 's/\./\t/'
this does not give correct output.
 
Old 03-31-2011, 08:19 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
How about using the version sort instead ... not quite what it was intended for, but it seems to work:
Code:
sort -V -k2,2 file
 
1 members found this post helpful.
Old 03-31-2011, 09:05 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Correction. I left out the -n option for numeric sort.
Code:
tr '\t' '\.' <test | sort -n -t'.' -k2 -k3 -k4 -k5 | sed 's/\./\t/'
20100228062521  10.134.53.111
20100228062520  10.134.53.113
20100228062520  10.134.53.114
20100228062520  10.134.53.114
20100228062521  10.134.53.114
20100228062525  10.134.53.114
20100228062525  99.247.216.110
20100228062520  99.247.216.111
20100228062524  99.247.216.111
20100228062522  201.230.227.197
Nice catch about using a Version sort. I didn't think about that. The format of an IP address matches version numbers, so it would work.

Last edited by jschiwal; 03-31-2011 at 09:07 AM.
 
Old 03-31-2011, 02:19 PM   #6
BarataPT
LQ Newbie
 
Registered: Mar 2011
Posts: 12

Original Poster
Rep: Reputation: 1
Thanks guys
 
  


Reply



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
Is there a sort of 2nd SSH daemon server, sort of JAILKIT, in the debian repositories frenchn00b Debian 5 06-20-2010 04:03 AM
php sort help - sort numerical descending then by alphabetical? RavenLX Programming 3 03-11-2009 08:35 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
Java - problems with selection sort gomez6211 Programming 1 09-22-2006 11:54 PM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 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