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. ;) |
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.
|
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. |
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. |
Code:
cut -d' ' -f2 fileA | sed 's/\(.*\)\..*\..*/\1\.0\.0/' If 'fileA' contains: Code:
hostname 192.168.23.65 Code:
192.168.0.0 |
Code:
awk '{print gensub(/([0-9]+\.[0-9]+\.).*/,"\1.0.0","g",$2)}' file |
Quote:
|
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...
|
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 |
Wow! I have always thought awk was not worth the time required to learn but that last command sure changed my mind. Well done.
|
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. |
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 |
Quote:
|
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, |
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 Oops -- that doesn't avoid file<n> with the same content :redface: This does Code:
#!/bin/bash |
All times are GMT -5. The time now is 03:59 PM. |