LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2003, 10:40 PM   #1
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Rep: Reputation: 30
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?
 
Old 12-16-2003, 10:57 PM   #2
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
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])$/;
 
Old 12-16-2003, 11:00 PM   #3
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
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!
 
Old 12-16-2003, 11:12 PM   #4
AltF4
Member
 
Registered: Sep 2002
Location: .at
Distribution: SuSE, Knoppix
Posts: 532

Rep: Reputation: 31
> 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
 
Old 12-16-2003, 11:35 PM   #5
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with static arp tables and it`s filtering J4b0l Linux - Networking 0 11-18-2005 03:20 PM
grep problem michaelsanford Linux - Software 2 05-20-2005 08:23 PM
OpenOffice regular expression filtering problem JAB4ever Linux - Software 3 12-08-2004 05:42 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM
Sendmail Spam filtering and Virus filtering MrJoshua Linux - General 2 04-03-2003 11:12 AM


All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration