LinuxQuestions.org
Help answer threads with 0 replies.
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 04-14-2012, 07:10 AM   #1
putter1900
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Rep: Reputation: Disabled
Sorting an IP address list w/ CIDR notation


Greetings folks!

Not exactly new to Linux, but still a newbie in certain aspects of it.

I need to sort a list of IP's but in CIDR

Now to sort a "pure" IP list I'd use:

sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

BUT

the IP list I have looks like this

172.146.3.5/16
10.0.0.16/24
192.168.1.1/14
(etc)

for the life of me, I just can't figure out how to make sort essentially "ignore" the slash, together with anything to the right of it and just get on with sorting based upon the dotted quad notation.

Help and guidance would be greatly appreciated!

Regards & TIA
putter
 
Old 04-14-2012, 08:48 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Could you be more specific?

If I use the sort command mentioned by you, it does what you ask for. Although the /xx part isn't actually ignored the sort end result is what you want/need (a list of numerically sorted IP's).

Code:
$ cat input
1.2.3.100/16
1.2.3.4/4
10.20.30.40/16
1.2.3.4/2
10.20.30.40/4
1.2.3.10/2
1.2.3.4/16
10.20.30.40/2
1.2.3.1/4
$ sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 input
1.2.3.1/4
1.2.3.4/16
1.2.3.4/2
1.2.3.4/4
1.2.3.10/2
1.2.3.100/16
10.20.30.40/16
10.20.30.40/2
10.20.30.40/4
The bold parts are sorted correctly.
 
Old 04-14-2012, 09:10 AM   #3
putter1900
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Hi,

Could you be more specific?

If I use the sort command mentioned by you, it does what you ask for. Although the /xx part isn't actually ignored the sort end result is what you want/need (a list of numerically sorted IP's).

Code:
$ cat input
1.2.3.100/16
1.2.3.4/4
10.20.30.40/16
1.2.3.4/2
10.20.30.40/4
1.2.3.10/2
1.2.3.4/16
10.20.30.40/2
1.2.3.1/4
$ sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 input
1.2.3.1/4
1.2.3.4/16
1.2.3.4/2
1.2.3.4/4
1.2.3.10/2
1.2.3.100/16
10.20.30.40/16
10.20.30.40/2
10.20.30.40/4
The bold parts are sorted correctly.
That's SO odd, because when I tried the "classic" IP sort method (referenced above) on a list of IP's with comments annotated:

like this

192.168.1.18/12 -m comment --comment "albania 20120414"
10.0.0.20/16 -m comment --comment "georgia 20110515"
1.2.3.4/15 -m comment --comment "la la la 20090101"

on a (trial) list of around 150 lines, there were IP's out of sequence in the sorted list

After verifying it a couple of times, concluded that it just wasn't working. Hence the post....

Sort works on left side fields I think, but could text to the right of /xx be affecting the outcome perhaps?

Thanks so much for jumping in on this!!

Warm regards,
putter
 
Old 04-14-2012, 09:22 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by putter1900 View Post
That's SO odd, because when I tried the "classic" IP sort method (referenced above) on a list of IP's with comments annotated:

like this

192.168.1.18/12 -m comment --comment "albania 20120414"
10.0.0.20/16 -m comment --comment "georgia 20110515"
1.2.3.4/15 -m comment --comment "la la la 20090101"

on a (trial) list of around 150 lines, there were IP's out of sequence in the sorted list
It would be nice to have the original input you used.

Quote:
Sort works on left side fields I think, but could text to the right of /xx be affecting the outcome perhaps?
Without knowing what the original input looks like I cannot be 100% sure, but I don't think so.

The command you used sorts column 1 first, then if needed column 2, column 3 and lastly column 4 (all of them numerically). The columns itself are done left to right (as shown by my sample output).

I've played around with some variations of the input, but I cannot reproduce your claim.
 
Old 04-14-2012, 09:38 AM   #5
putter1900
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Hi,
It would be nice to have the original input you used.

Without knowing what the original input looks like I cannot be 100% sure, but I don't think so.

The command you used sorts column 1 first, then if needed column 2, column 3 and lastly column 4 (all of them numerically). The columns itself are done left to right (as shown by my sample output).

I've played around with some variations of the input, but I cannot reproduce your claim.
Hmm, well the file is called bigtestfile (I know, I'm light on imagination)

The test file contained entries absolutely identical to those earlier in the thread

so, this is what the input looked like

$ sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 bigtestfile

The output contained lines completely out of sequence. A bit like the attached "quick snip" from more or less the middle of the file. Clearly out of sequence.

Baffling

Ongoing kind thanks for your input and experimentation.

Warm regards,
putter
Attached Files
File Type: txt bigtestfile_snip.txt (1.6 KB, 6 views)
 
Old 04-14-2012, 09:47 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

The file you posted (and probably the original) are dos files and not linux/unix files.

Convert the original to a linux file format. There are many ways, dos2unix is just one way.

Hope this helps.

Have a look here as well: Convert text

Last edited by druuna; 04-14-2012 at 09:49 AM. Reason: Added url
 
Old 04-14-2012, 09:59 AM   #7
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
sort -V -t '/' -k 1 yourFile
 
Old 04-14-2012, 10:22 AM   #8
putter1900
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Cool Sorting an IP address list w/ CIDR notation [SOLVED]

Quote:
Originally Posted by druuna View Post
Hi,

The file you posted (and probably the original) are dos files and not linux/unix files.

Convert the original to a linux file format. There are many ways, dos2unix is just one way.

Hope this helps.

Have a look here as well: Convert text
Thanks much, yes indeed they were DOS files, but I thoughtI'd converted them within vim by doing

Code:
:set ff=unix
first before even beginning this thread

The attached file however, was indeed in DOS format, I copied/pasted into a new text file for the sake of speed

HOWEVER

thanks for the DOS2UNIX link, I installed it and banged all the files in the directory into unix format in one shot (for those reading this later on) this way;
Code:
dos2unix -o *
Reapplying the sort made no difference. Lines were still out of sequence.

BUT THEN

uhelp helpfully jumped in and made the following recommendation

Code:
sort -V -t '/' -k 1 yourFile
SO
I did;

Code:
sort -V -t '/' -k 1 bigtestfile > bigtestfile2
and it sorted in the exact sequence desired

I'm marking this thread "solved" with thanks to you both for spending time on helping me with this problem.

THANK YOU to:
druuna
and for the shortest solution I can ever remember seeing uhelp

Thank you both so very much!

Warm regards,
putter

Last edited by putter1900; 04-14-2012 at 10:23 AM.
 
Old 04-14-2012, 10:25 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
Old 04-14-2012, 10:29 AM   #10
putter1900
LQ Newbie
 
Registered: Apr 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
You're welcome

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
Gladly done, and again, many thanks for your patient help.

Warm regards,

putter
 
  


Reply


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
[SOLVED] Slash Notation for IP Address Ranges MathGuy Linux - Newbie 9 08-16-2011 05:55 PM
[SOLVED] list sorting in python aihaike Programming 4 05-12-2011 01:20 AM
script for sorting by exponential notation MALDATA Programming 5 12-02-2009 05:44 PM
sorting a list into comma separated list nixlearn Linux - Newbie 22 12-03-2008 07:21 AM
Understanding CIDR notation for iptables Madone_SL_5.5 Linux - Networking 5 12-07-2007 11:26 AM


All times are GMT -5. The time now is 10:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration