# Get the ipaddress and align based on the input file

**URL:** https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553
**Category:** Shell Programming and Scripting
**Created:** [August 2, 2012, 6:04am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553 "2012-08-02T06:04:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kamauv234](https://community.unix.com/letter_avatar/kamauv234/32/5_5575768a8748004e209b776fc1b2916d.png) [@kamauv234](https://community.unix.com/u/kamauv234)
#### Post date: [August 2, 2012, 6:04am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/1 "2012-08-02T06:04:46Z")

</div>

Hi All,

I have a file contains below contents, "interfacename/subnet: public (or) interfacename/subnet:cluster\_interconnect"

```auto
"en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public
"en0"/10.26.208.0:public,"en1"/192.168.3.0:cluster_interconnect

```

With the help of the contents i want to make the ouput like below

```auto
  Public IP Address : 169.181.146.79
  Private IP Address 1 : 10.185.81.1
  Private IP Address 2 : 10.185.81.2

```

sometime it may have 2 cluster\_interconnect & sometime it may be 1. Please provide the suggestions. Thanks !

Regards  
Kamal

---

<div class="post-metadata">

### Author: ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)
#### Post date: [August 2, 2012, 10:33am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/2 "2012-08-02T10:33:34Z")

</div>

Your output data doesn't really resemble your input. It seems to create .79, .1, .2 out of nowhere -- how does it decide these from .0? Why does it ignore 10.26.208.0? etc, etc.

You'll have to explain the algorithm you want better.

---

<div class="post-metadata">

### Author: ![kamauv234](https://community.unix.com/letter_avatar/kamauv234/32/5_5575768a8748004e209b776fc1b2916d.png) [@kamauv234](https://community.unix.com/u/kamauv234)
#### Post date: [August 2, 2012, 11:00am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/3 "2012-08-02T11:00:03Z")

</div>

Thanks for the reply, Let me rephrase my question. I need to get the interface name (example en2 -\> private en5 -\> private en6 -\> public)based on those below input file and get the ipaddress for each interface name..... like that i have to do for each line of input file... and get the output as below.

Input file  
---------

```auto
 
"en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public
"en0"/10.26.208.0:public,"en1"/192.168.3.0:cluster_interconnect

```

Expected Output  
-----------------

```auto
  Public IP Address : 169.181.146.79
  Private IP Address 1 : 10.185.81.1
  Private IP Address 2 : 10.185.81.2

```

Regards  
Kamal

---

<div class="post-metadata">

### Author: ![gull04](https://community.unix.com/user_avatar/community.unix.com/gull04/32/14972_2.png) [@gull04](https://community.unix.com/u/gull04)
#### Post date: [August 2, 2012, 11:04am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/4 "2012-08-02T11:04:25Z")

</div>

Hi,

You could probably get the information this way;

```nohighlight
# ifconfig -a

```

Regards

Dave

---

<div class="post-metadata">

### Author: ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)
#### Post date: [August 2, 2012, 11:13am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/5 "2012-08-02T11:13:07Z")

</div>

You have not explained anything about what I actually asked.

Where does the .79 come from?

Where does the .1 come from?

Where does the .2 come from?

Why does it ignore 10.26.208.0?

Why does it ignore 192.168.3.0?

What does the interface name have to do with any of it? It doesn't appear in the output at all.

---

<div class="post-metadata">

### Author: ![kamauv234](https://community.unix.com/letter_avatar/kamauv234/32/5_5575768a8748004e209b776fc1b2916d.png) [@kamauv234](https://community.unix.com/u/kamauv234)
#### Post date: [August 6, 2012, 3:54am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/6 "2012-08-06T03:54:55Z")

</div>

Input data (Only single line) is generated by oracle database itself its stored in config file in each and every Oracle RAC Server as below format

```auto
 
ex:
if we specify one public & private interface during installation of oracle RAC it will put one entry for public and private as below format in config file 
 
"<interfacename>"/<subnet>:public,"<interfacename>"/<subnet>:cluster_interconnect
 
if we specify one public & two private interface during installation of oracle RAC it will put one entry for public and two private as below format in config file 
 
"<interfacename>"/<subnet>:public,"<interfacename>"/<subnet>:cluster_interconnect,"<interfacename>"/<subnet>:cluster_interconnect

```

All i am doing now is based on this input stored in the config file i want to convert this to below output

```auto
 
EX : ifconfig en2 (Ipaddress will be derived from this command)
 
en6 -> public -> <Ipaddress>
en2 -> Private 1 -> <Ipaddress>
en5 -> Private 2 -> <Ipaddress>

```

Regards  
Kamal

---

<div class="post-metadata">

### Author: ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)
#### Post date: [August 6, 2012, 5:45am UTC](https://community.unix.com/t/get-the-ipaddress-and-align-based-on-the-input-file/314553/7 "2012-08-06T05:45:59Z")

</div>

Making the wild assumption (amongst others) that your `ifconfig` will produce sth like

```nohighlight
eth0 Link encap:Ethernet HWaddr 00:1f:c6:4c:d0:e2  
          inet addr:10.1.1.1  

```

this awk program (saved in cmdfile) might do the job:

```nohighlight
BEGIN {
         while (("ifconfig -a|awk '/Link/ {a=$1; getline; sub(\".*:\",\"\",$2);print a, $2}'"|getline) > 0)
         ip[$1]=$2
         RS="[,\n]"; FS="/"
        }

        {
         gsub("\"","");gsub("([0-9]+[.:])+","",$2); if($2!="public") $2="Private "++pn
        }
        {print $1, "->", $2, "->", ip[$1]}

```

Execute like `awk -f cmdfile inputfile`

What it does is in the BEGIN command it executes ifconfig to pipe all the interfaces (identified by "Link" in the same line) and their IP- addresses into array "ip". Then it reads your input file, records separated by ",", to extract the respective interface names, cleans field $2 from the network part, and, if the remainder is not "public", assigns the string "Private " plus a counter. Finally the fields are printed. Hope this fits your needs. You may need to adapt the ifconfig extraction.
