LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   output IP into a file (https://www.linuxquestions.org/questions/programming-9/output-ip-into-a-file-806382/)

NetRock 05-06-2010 04:45 PM

output IP into a file
 
Hi ALL ;)

have a file (called it A) contains;

hostname 192.168.23.65
hostname 10.18.13.253
hostname 10.18.16.253
hostname 177.23.56.58


how can i write a script to get only ip addresses & send it to 3 files with the following info. only:

file1:
192.168.0.0


file2:
10.18.0.0


file3:
177.23.0.0


Thank you in advance. ;)

acid_kewpie 05-06-2010 04:47 PM

What does that output mean?? why 3 files? What does it mean to be in file1 instead of file2?? you need to explain this a lot more to give anyone a chance of providing what you're after.

pixellany 05-06-2010 04:59 PM

the way I read it, you want to put every IP into a separate file, but with the 3rd and 4th stanzas of the IP masked out. Also (just guessing), it looks the individula files are assigned based on the 1st 2 stanzas.

If I'm correct, then file two will have two identical entries.

As acid* says, you need to give more detail as to exactly what the logic needs to be.

NetRock 05-06-2010 10:18 PM

Thanks for your replies. Need to have different files for each set of ip addr. so later on, i will used them to build my subnet, gw. by knowing the first two digits of each hostname. please do not ask way because that's what i get to start with to create the subnetmask...etc.
i use grep, awk & cut ...after echo to a file. but my problem is the space between the hostname & the 1st digit. try different ways the best i got was: ex.;
hostname 192.168.0.0 for file1. But i need ONLY 192.168.0.0. i did something like:
grep fileA | awk -d. |

any help appreciated!!

Thanks.

SuperJediWombat! 05-06-2010 10:47 PM

Code:

cut -d' ' -f2 fileA | sed 's/\(.*\)\..*\..*/\1\.0\.0/'
I am sure someone can think of a cleaner way to do that, but it works :)

If 'fileA' contains:
Code:

hostname 192.168.23.65
hostname 10.18.13.253
hostname 10.18.16.253
hostname 177.23.56.58

The output will be this:
Code:

192.168.0.0
10.18.0.0
10.18.0.0
177.23.0.0

Note that is assumes you are allways using a /16 subnet (seems strange)

grail 05-07-2010 02:06 AM

Code:

awk '{print gensub(/([0-9]+\.[0-9]+\.).*/,"\1.0.0","g",$2)}' file

SuperJediWombat! 05-07-2010 02:55 AM

Quote:

Originally Posted by grail (Post 3960071)
I'll match your sed and raise you one awk.

Thanks, Grail.

acid_kewpie 05-07-2010 04:13 AM

meh, death by awk!... I'd initially cut everything to shreds, just my ugly quick style... But there's still no detail at all about what each "set" or ip addresses is... SuperThingyWotsit has assumed /16's, which seems fair enough based on the output, but at the same why would you want /16's in a real life scenario? No idea.. maybe if you actually told us...

grail 05-07-2010 04:40 AM

So I made some typos, so I thought I would answer all of the question (ie individual files:

Code:

awk '{x=gensub(/([0-9]+\.[0-9]+\.).*/,"\\10.0","g",$2);if(!_[x]++)print x > "file"(++i)}' input_file

SuperJediWombat! 05-07-2010 06:17 AM

Wow! I have always thought awk was not worth the time required to learn but that last command sure changed my mind. Well done.

NetRock 05-07-2010 07:19 AM

grail, superjedi
Thank you so much for the code, I will try it out & let you know.
i am not plan'g to use 16 bits. I need these files to test my system. for sure, i have to cover all the subnet mask. this is the start to a big project. keep you posted. Your help appreciated very much.

NetRock 05-07-2010 09:08 AM

Alright tested & grail's code works just GREAT....thanks.
just need to know the code itself how it works, was wondering if you can explain as i am a newbie.
thanks.

Superjedi thanks for your reply. tried yours but only leaves out the hostname & i get all 3 ips as they are in the fileA

pixellany 05-07-2010 09:12 AM

Quote:

Originally Posted by NetRock (Post 3960425)
Alright tested & grail's code works just GREAT....thanks.
just need to know the code itself how it works....

http://www.grymoire.com/Unix/Awk.html

grail 05-07-2010 09:31 AM

Most of this is in the pages pointed to by pixellany's link, but happy to explain:

Code:

x=gensub(/([0-9]+\.[0-9]+\.).*/,"\\10.0","g",$2) - so this is a standard back reference scenario. gensub returns your changes to the target without altering it,
                                                  like gsub or sub and assigns to x. So all between () is what we are saving (ie NNN.NNN.). \\1 equals this
                                                  then we add the string 0.0 "g" means global and $2 second field as separator is space

if(!_[x]++)print x > "file"(++i)} - arrays in awk can have any string as an index, so ours is the IP we just created. Default value for variables is zero, hence
                                    the use of not (!). So first time it see the array the value is zero (false) which becomes true with not so perform task
                                    after "if". This makes it so it will only print unique values. Then we print the value to a file named "file" concated with
                                    the value of i, which we increment prior to it being used as all values, as above, start at zero

Viola!!

catkin 05-07-2010 09:33 AM

Not as pretty as grail's awk but just to show it can be done (and it should run a lot faster than using awk)
Code:

#!/bin/bash

i=0
IFS=.
while read name IP
do
    octets=( $IP )  # parse IP octets into array elements
    echo ${octets[0]}.${octets[1]}.0.0 > file$((++i))
done < fileA

EDIT:

Oops -- that doesn't avoid file<n> with the same content :redface: This does
Code:

#!/bin/bash

i=0
done=
IFS=.
while read name IP
do
    octets=( $IP )  # parse IP octets into array elements
    network=${octets[0]}.${octets[1]}.0.0
    case $done in
        *"$network"* )
            ;;
        * )
            echo "$network" > file$((++i))
            done="$done $network"
    esac
done < fileA



All times are GMT -5. The time now is 03:59 PM.