Ip address sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ip address sorting
# 1  
Old 10-09-2013
Ip address sorting

Hello folks,

I am sorting ip address file i have file having ips like

Code:
192.168.1.1
192.168.2.2
192.168.1.3
192.168.1.2
192.168.11.1
192.168.1.4
192.168.2.1

I have tried below code
Code:
i=1
while :
do
echo $i
x=$(cat file | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.$i\{1,3\}\.[0-9]\{1,3\}')
echo $x
let i++
done

expected output, i want to sort ips in such a way first it will complete 192.168.1.1 to 192.168.1.254 then search 192.168.2.1 to 192.168.2.254 and so on go to 192.168.33.1 to 192.168.33.254.

Code:
192.168.1.1
192.168.1.2
192.168.1.3
192.168.2.1
192.168.2.2
192.168.1.4
192.168.11.1

# 2  
Old 10-09-2013
Try this:
Code:
sort -t . -k 3,3n -k 4,4n file

# 3  
Old 10-09-2013
This is not working, showing

Code:
192.168.30.
192.168.15.
192.168.30.
192.168.37.
.......

192.168.6.
192.168.30.
192.168.37.

# 4  
Old 10-09-2013
What is your input data and which OS are you running? What you posted doesn't correspond with the input you posted earlier. The sort command doesn't change the data, only the order in which it's shown and your IP's seem to be missing some numbers.
# 5  
Old 10-09-2013
I am using CENTOS Linux, There are hundered of ips, i just take few ips from order, but order is not correct as mentioned, if ips are not mentioned then no issue it will search for different subnet and make it order as mentioned above, if not present then discard that let it print the ips in order.
# 6  
Old 10-09-2013
Did you try the sort command directly on your input file instead of implementing it in your while loop?

Code:

$ cat file
192.168.1.1
192.168.2.2
192.168.1.3
192.168.1.2
192.168.11.1
192.168.1.4
192.168.2.1
$ sort -t . -k 3,3n -k 4,4n file
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.2.1
192.168.2.2
192.168.11.1

# 7  
Old 10-09-2013
Yes i have tried directly without loop, result is not correct. One more thing hostname is also mention in the file. File is like that.

Code:
hostname  ip-address
hostname  ip-address
hostname  ip-address
hostname  ip-address
hostname  ip-address

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

MAC Address - Four Interfaces with the same MAC Address

four interfaces with ifconfig all interfaces have the same mac. If is not set for unique. but it still works. what difference does it make to have all macs the same or different? (4 Replies)
Discussion started by: rrodgers
4 Replies

2. UNIX for Advanced & Expert Users

C program to detect duplicate ip address if any after assigning ip address to ethernet interface

Hi , Could someone let me know how to detect duplicate ip address after assigning ip address to ethernet interface using c program (3 Replies)
Discussion started by: Gopi Krishna P
3 Replies

3. IP Networking

Tracing a MAC address to IP address: Solaris

Hi there I lost connectivity to one of our remote systems and when I checked the messages log I found the following: Aug 10 23:42:34 host xntpd: time reset (step) 1.681729 s Aug 16 13:20:51 host ip: WARNING: node "mac address" is using our IP address x.x.x.x on aggr1 Aug 16 13:20:51 host... (9 Replies)
Discussion started by: notreallyhere
9 Replies

4. UNIX for Dummies Questions & Answers

What would the physical address be for virtual address?

Hi guys, I got one problem which I definetily no idea. What would the physical address be for virtual address? 1) 2ABC 2) 3F4B Here is the page table:see attached Thank you sos sososososso much!! (0 Replies)
Discussion started by: lemon_06
0 Replies

5. Solaris

Get ip address from mac address

I have following message in my messages file on solaris 10 WARNING: e1000g3712000:3 has duplicate address 010.022.196.011 (in use by 00:50:56:85:25:ef); disabled Now is there any way i can find which server has 00:50:56:85:25:ef mac address either IP or Hostname ? (6 Replies)
Discussion started by: fugitive
6 Replies

6. UNIX for Dummies Questions & Answers

Panic kernal-mode address fault on user address 0x14

:) Firstly Hi all!!, im NEW!! and on here hoping that someone might be able to offer me some help... i have a server that keeps crashing every few days with the error message: PANIC KERNAL-MODE ADDRESS FAULT ON USER ADDRESS 0X14 KERNAL PAGE FAULT FROM (CS:EIP)=(100:EF71B5BD) EAX=EF822000... (10 Replies)
Discussion started by: Twix
10 Replies

7. Shell Programming and Scripting

ksh - how to list all ip address between 2 ip address

Trying to do a ksh script that needs to list all ip address between ip address a and b .. ie. Ip address A=192.168.1.200 Ip address B=192.168.2.15 So the subnet changes from 1 to 2 but I want to list all possible ip addresses between the 2.. Which would be: 192.168.1.200... (4 Replies)
Discussion started by: frustrated1
4 Replies

8. Shell Programming and Scripting

Sorting an address string

I'm in an introduction to Unix class and well I'm kind of stuck on one part of the lab for this week or shell scripts. Basically we're given a file named address.data and we're supposed to create a script to sort it according to zip code, last name, and first name (not at the same time of course).... (0 Replies)
Discussion started by: Minimum
0 Replies

9. IP Networking

How to Achive IP address through MAC(Ethernet) address

Hi sir, i want to make such programe which takes MAC(Ethernet) address of any host & give me its IP address....... but i'm nt getting that how i can pass the MAC address to Frame........ Please give me an idea for making such program... Thanks & regards Krishna (3 Replies)
Discussion started by: krishnacins
3 Replies

10. UNIX for Dummies Questions & Answers

network address and broadcast address?

say I have a IP address which is 10.0.0.12, and subnet mask is 255.255.255.240, what is the network address and what is the broadcast address which host lives on? And could you explain how to get the answer? thanx in advance! (7 Replies)
Discussion started by: pnxi
7 Replies
Login or Register to Ask a Question