ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
egrep '^ *(([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5]) *$' your_file.txt
# Taken (modified) from the O'Reilly book "Mastering regular expressions" .
This assumes there only an IP-address on each line, nothing else, except leading and trailing spaces.
Also note that it accepts zero-padding, i.e. 001.002.003.004 will be valid. but this actually is a valid IP-address.
thanks guys. the egrep example is the one i really needed.
im not using perl or python so i needed just a plain ole regex, but i will save all of the example becuase you never know. i will have to use python later on tho....have to learn it to teach it to my girlfriend because she has to take a class that uses it. i get to learn it for the sake of helping her out in the class (arent i a good boyfriend).
Last edited by Robert0380; 08-14-2003 at 06:42 PM.
does the python example work? wouldnt 123.123.123.123.123 pass? i cant see where it checks for the number of octets.
<edit>also, how does int() treat characters, if someone entered 46.abc.123.87, what would int(abc) evaluate to? hopefully not 0 because if it did then it would pass.
but i have to say, not knowing python/perl and not being very good at regex, the python example was the only one i could read, and i wouldnt want to have to maintain the others.
Originally posted by kev82 does the python example work? wouldnt 123.123.123.123.123 pass? i cant see where it checks for the number of octets.
Yeah, it would. You can add a check for that, of course.
Quote:
<edit>also, how does int() treat characters, if someone entered 46.abc.123.87, what would int(abc) evaluate to? hopefully not 0 because if it did then it would pass.
It would raise an exception, as it should.
Quote:
but i have to say, not knowing python/perl and not being very good at regex, the python example was the only one i could read, and i wouldnt want to have to maintain the others.
Here's the modified version to check for number of octets as well.
Code:
def isIP(s):
octets = s.split(".")
if len(octets) != 4: return False
for octet in octets:
if not (int(octet) >= 0 and int(octet) <= 255):
return False
return True
The answers above are all very well, but doesn't fully solve the problem
Quote:
Originally posted by Robert0380
i'm trying to parse a file of valid and invalid IP's and print the good ones, it's using egrep and there is 1 ip per line.
All fails to read in the file and to print out the result (Well, except maybe the egrep thing). Here's a solution in perl that includes a novel thing: indentation ;-) I wasn't sure if the ip-addresses were printed on the line without any noise except maybe whitespace. If this was the case, the 3rd line could be simply
@ipbits = split(/\./);
#!/usr/bin/perl -w
main: while(<>) {
@ipbits = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})?/; # pick out ip addresses
next unless (@ipbits); # verify pattern match
for (@ipbits) {
next main if ($_ <0 || $_>255); # verify ranges
}
print join('.',@ipbits)."\n"; # print valid numbers
}
The important things to know in order to read perl is:
1) Scalar variables are prefixed with $, arrays with @ and hashes with #
2) Many perl functions work implicitly on $_ if nothing else is stated.
3) one-line if are written in reverse. E.g.:
die if ($shot);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.