Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IP octect matching - AWK

Status
Not open for further replies.

SenFan

Programmer
Jun 5, 2001
2
CA
Hello,

I have a text file containing IP information, the IP address is in the first column of the file. What I want to do is match a range in the first octet, I'm new to scripting so I'm a little confused. Heres what I have tried.

awk '$1 ~ /[192 - 196].168.0.1/' samplelog

so basically I want to see all the IP address that start with 192.168.0.1 to 196.168.0.1.

This is part of a larger script and I have hard coded the last 3 (168.0.1) parts of the address just so I could illistrate my problem. Their are 6 colums in this log file so I don't really want to change my field separator (to '.')and then check the values that way, but it's starting to to look like I might have to.

I would be very grateful for any ideas you might have,

Mark Moesker.
Ottawa, Canada
 
Hi, SenFan!

Please, try this regular expression:

/19[2-6].168.0.1/

[2-6] matches any number between 2 and 6.

Regular expressions are powerful tool of awk scripting language. If you want to learn regExp, I suggest you this site:


Excellent book about regular expressions is "Mastering Regular Expressions," by Jeffrey E. F. Friedl; you can see this site:


There is 4th chapter from Mr. Friedl's book on this site:


RegExp is very powerful tool.  I use it with awk for searching the Bible in ASCII format and for data analysis.

Bye!

KP.
 
SenFan-

You may have to escape the periods as shown.

Otherwise, it is the same as Krunek's example.


awk '$1 ~ /19[2-6]\.168\.0\.1/' samplelog


Hope this helps you!


flogrr
flogr@yahoo.com

 
SenFan:

Here is a script I use to look up IP addresses:
It works okay for me.

awk --re-interval ' /[0-9]/ {
if ($0 ~ /{1,3}/) {
print "IPaddresses:", $0
} else {
printf "%s", $0
}
}' addressfile



 
There are at least 3 or 4 pages [pp123-125]in Freidl's book
["Mastering Regular Expressions"] on the topic of IPaddress
recognition/validation. As the experiense shows, you can
take this[recognition/validation] to the ultimate and _still_
not be certain that you've covered ALL the cases.

For examples [from Friedl's book]:

^([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])
$

Quoting Friedl: "Was the trouble worth it? You have to decide yourself based upon your own needs".

vlad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top