LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Filtering Problem (using grep) (https://www.linuxquestions.org/questions/linux-newbie-8/filtering-problem-using-grep-126496/)

gauge73 12-16-2003 09:40 PM

Filtering Problem (using grep)
 
I'm writing a perl script and I'm trying to check a variable to see if it is a valid IP address. I've split the octets into an array and am using a foreach loop to check them one by one. I'm trying to use grep to check their validity (0-255), but I can't seem to get it to work.

My first attempt was to simpy use the following:

if(`echo $var | grep [0-255]` eq $var)

But that didn't work. So my next try was to do the following:

if(`echo $var | grep [[0-1]?[0-9]?[0-9], 2[0-4][0-9], 25[0-5]]` eq $var)

I wasn't sure that having classes within classes like that was valid, so when this didn't work, I wasn't surprised. I then tried to spread out the outer class by doing this:

if(`echo $var | grep -e [0-1]?[0-9]?[0-9] -e 2[0-4][0-9] -e 25[0-5]` eq $var)

Yet, that did not work either. After a while it occurred to me that the back-ticked command had to be chopped to get rid of the return character. I've implimented all three of the above commands with the chop(), and still to no avail.

Can anyone help me with this? :confused:

AltF4 12-16-2003 09:57 PM

http://iis1.cps.unizar.es/Oreilly/pe...ok/ch06_24.htm

IP address

m/^([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\.
([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])$/;

gauge73 12-16-2003 10:00 PM

Should I use that expression with sed or what? I'm really quite new at the whole filter thing. I've never seen grep commands that looked like that.

And is there any chance you could explain to me why what I tried didn't make sense? I feel like I'd learn more if I saw where my error was. :)

Thanks a bunch for the help! :)

AltF4 12-16-2003 10:12 PM

> I'm writing a perl script ..
made me think you wanted a perl solution :-)
i'm afraid grep won't handle it

m #match
/^ # start of line
( # group start
[01]?\d\d # "nothing" or 0 or 1, followed by 2 digits
| # OR
2[0-4]\d # 2 followed by 0,1,2,3 or 4 followed by 1 digit
| # OR
25[0-5] # or 25 followed by 0,1,2,3,4 or 5
) # group end
\. # dot
([01]?\d\d|2[0-4]\d|25[0-5])\. # 2nd + .
([01]?\d\d|2[0-4]\d|25[0-5])\. # 3rd + .
([01]?\d\d|2[0-4]\d|25[0-5]) # 4th
$/; # end of line

gauge73 12-16-2003 10:35 PM

I really appreciate the help... I have two more questions, though. I hate to bug you, but I gotz to learn this stuff. ;)

1) How do I phrase the if statement using what you found? Would it be something like this?

if(m/^([01]?\d\d|2[0-4]\d|25[0-5])/ $octet) {...}

Keep in mind I have the IP divided up into octets already, so I don't need to check all four at once, and I don't need to look for the .'s just the 0-255, basically. I just don't know how to use the regular expression in the if statment without using the default variable ($_).

2) Why won't grep handle it? I know that it can do the three different cases...

a) nothing followed by 0-9 or nothing followed by 0-9
b) 2 followed by 0-4 followed by 0-9
c) 25 followed by 0-5

I got each of these to work individually, but I couldn't find a way to combine them. Is there no way to combine expressions like this?


Thanks again!


All times are GMT -5. The time now is 02:19 PM.