Problem with regexp for IP-Adress Pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with regexp for IP-Adress Pattern
# 1  
Old 05-23-2008
Problem with regexp for IP-Adress Pattern

Hi all Unix Gurus!

Since hours (even days :-)) I'm trying to find the correct pattern to search for IP addesses in text files.
The pattern to find a IP address itself is not too difficult:
'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|2[0-5]{2})\.){3,}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|2[0-5]{2})'

BUT, of course the above pattern is also matching lines like
v.55.25.1.7v
1.3.6.1.4.1.897.4.6.1 = dce
394983|12.50.1.0.0|
--> which are not really IP addresses

The big question is now to avoid the matching of above lines. I thought the best is to NOT ALLOW A DOT [^.] BEFORE AND AFTER MY PATTERN:
'[^.](([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|2[0-5]{2})\.){3,}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|2[0-5]{2})[^.]'

BUT THIS IS SIMPLY NOT WORKING Smilie! It is still showing above example lines

I'm using nawk and egrep on Solaris 9.

Many, many thanks in advance for any hint on my problem.
# 2  
Old 05-23-2008
The masq I use is this one:
Code:
masq='^((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])$'
 # to test:

$ echo "ip" | egrep "$masq"

# 3  
Old 05-23-2008
Hi ripat,

thanks for your reply, but this is unforunately not working as you asume that the IP address is the only information in the line (^....$).

Here is my testfile:
Code:
1. Yes 123.25.67.8 first line
2. RIP1:171.27.38.166:spooler:spooler:/HERMES:bin
3. this is an IP128.26.18.7:
4. 81.1.0.0 valid
5. 123.114.45.6c valid
6. valid IP |12.50.1.0|
7. this is also not an IP4.0.1.1.C
8. v.55.25.1.7vNO IP
9. 20040908|394983|12.50.1.0.0|SZ/WIS/SEITE01||
10. 1.3.6.1.4.1.897.4.6.1   = dce

--> I expect line 1-6 to be found, but not 7-10
# 4  
Old 05-23-2008
As RFC is not my favorite reading, can you define what you would consider as valid IP? The regex I gave, covers bytes going from 0 to 255.

Edit:

The following pattern works on your sample file but I am sure this is getting too specific.
Code:
'[^.0-9]((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])[^\.]'


Last edited by ripat; 05-23-2008 at 06:58 AM..
# 5  
Old 05-23-2008
Hi ripat,

THANKS A LOT! Nearly there :-))

I consider the whole range of IPs as valid IP (0-255.0-255.0-255.0-255).

The only problem left (which I can see currently) is that your regexp is also ignoring following additional line in my testfile (which is valid IP):

Code:
11. 12.50.1.0

# 6  
Old 05-23-2008
Just add the start and end of string anchors at the begining and at the end of the pattern:

Code:
'(^|[^.0-9])((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])([^\.]|$)'


Last edited by ripat; 05-23-2008 at 09:30 AM.. Reason: typo
# 7  
Old 05-23-2008
Hi ripat,

BRILLIANT! Thanks so much!

Your pattern is working (I guess you missed the NOT^ at the beginning of [.0-9]: [^.0-9] instead of [.0-9]):
Code:
'(^|[^.0-9])((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])([^\.]|$)'


IT IS WORKING FINE, BUT I DO NOT UNDERSTAND WHY
SmilieSmilieSmilie

(^|[^.0-9]) = "beginning of the line" OR "not dot, not number"

((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])= valid IP (range 0-255.0-255.0-255.0-255)

([^\.]|$) = "not backslash, not dot" OR "end of the line"

Am I interpreting the regexp correctly?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

TCL script to delete a pattern(regexp)

Hi I am writing a TCL script to delete a certain in a file My Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj; output dk; (1 Reply)
Discussion started by: kshitij
1 Replies

2. Shell Programming and Scripting

awk regexp to print repetitive pattern

How to use regexp to print out repetitive pattern in awk? $ awk '{print $0, "-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-"}' output: - - - - - - - - - - - -I tried following which does not give what I want, of course. awk '{print $0, "-\t{11}-"}' output: - ... (10 Replies)
Discussion started by: yifangt
10 Replies

3. Shell Programming and Scripting

Problem on understanding the regexp command

Hi all, I'm not clear of this regexp command: regexp {(\S+)\/+$} $String match GetString From my observation and testing, if $String is abc/def/gh $GetString will be abc/def I don't understand how the /gh in $String got eliminated. Please help. Thanks (2 Replies)
Discussion started by: mar85
2 Replies

4. Shell Programming and Scripting

Trying to get an IP adress from a file

This is probably a real n00b question but i`m not able to figure it out. I have a folder of configuration files that contain IP-adresses. The line i`m interested in looks like this: IP_ADDRESS="123.123.123.1123" Some have muliple ip adresses, so the line will look like : ... (5 Replies)
Discussion started by: DaneV
5 Replies

5. Shell Programming and Scripting

Regexp and sed problem

Basically it should identify what ever is in between /*< >*/ (tags) and replace dbname ending with (.) with the words in between the tags i.e. DELETE FROM /*<workDB>*/epd_test./*<multi>*//*<version>*/epd_tbl1 ALL; into DELETE FROM... (4 Replies)
Discussion started by: sol_nov
4 Replies

6. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

7. Shell Programming and Scripting

Extract words before and after a pattern/regexp

Couldn't find much help on the kind of question I've here: There is this text file with text as: Line one has a bingo Line two does not have a bingo but it has a tango Bingo is on line three Line four has both tango and bingo Now I would want to search for the pattern "bingo" in this file... (3 Replies)
Discussion started by: manthasirisha
3 Replies

8. HP-UX

Change IP Adress

I want change my IP address and hostname in my machine by use the console. Can any one tell me how can I execute that by command ? Thanks & Regards (1 Reply)
Discussion started by: magasem
1 Replies

9. Solaris

IP-Adress

Hello together how can I find a ipadress from a login into remote system console? Thanks a lot Urs (1 Reply)
Discussion started by: MuellerUrs
1 Replies

10. UNIX for Dummies Questions & Answers

MAC-Adress

Hello I need to show my MAC-Adress on a Unix System, is there someone that know how? (2 Replies)
Discussion started by: nkochr
2 Replies
Login or Register to Ask a Question