CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Replacing exact match of IP from list of files

 



saurabhsharma
Novice

Jan 19, 2012, 3:36 AM

Post #1 of 8 (1343 views)
Replacing exact match of IP from list of files Can't Post

Hi,

I have number of files containing IP address list.

I want to replace exact match of IP with some other value.

e.g.

One of IP list file contains

1. 10.23.23.241 url=jdbc:oracle:thin:@//110.235.345.21//10.235.345.21.abc.com

2. 10.23.23.241 10build 10build23.abc.com
3. 10.23.23.241 3s10build4 s10build2.iabc.com
4. 10.23.23.241 s10build s10build2.xyz.com
5. 10.23.23.241 10.235.345.214 10.235.345.21:1521build.www.com
6. VIPADDRESS=10.235.345.21 10.235.345.21:1521build.


i ONLY want to change exact match of (10.235.345.21) to (10.10.10.10) means first line in the file

10.23.23.241 url=jdbc:oracle:thin:@//110.235.345.21//10.235.345.21.abc.com MUST be replaces as

10.23.23.241 url=jdbc:oracle:thin:@//110.235.345.21//10.10.10.10.abc.com

In otherwords value "110.235.345.21" value must NOT be changed as this is not the exact match as it contains digit "1" before "10.235.345.21"

likewise line number 5

5. 10.23.23.241 10.235.345.214 10.235.345.21:1521build.www.com must be changed to

10.23.23.241 10.235.345.214 10.10.10.10:1521build.www.com , again value in this line "10.235.345.214" must NOT be changed as it contains digit "4" in at the last position.

So the idea is to replace the exact match of value 10.235.345.21 provided there should be no digit preceeding it or present as the last character.

I have written shell script ehich is using perl command as

OLD_VAL= 10.235.345.21

NEW_VAL= 10.10.10.10

/usr/bin/perl -p -i -e "s/([^\d])$OLD_VAL([^\d])/\1$NEW_VAL\2/" $FILENAME

where FILENAME is the name of the file ro be modified as i don't want to open the file and process each line individually (even though the command is doing so internally)

The error i get is that it replaces the values which starts with "H", means it replaces the file into

1. 10.23.23.241 url=jdbc:oracle:thin:@//110.235.345.21/H.10.10.10.abc.com

2. 10.23.23.241 10build 10build23.abc.com
3. 10.23.23.241 3s10build4 s10build2.iabc.com
4. 10.23.23.241 s10build s10build2.xyz.com
5. 10.23.23.241 10.235.345.214H.10.10.10:1521build.www.com
6. VIPADDRESSH.10.10.10H10.10.10:1521build.




Like wise if the replacing IP starts with 11.X.X.X it replaces the first character with “I” and eats up first character.

please help

Saurabh


(This post was edited by saurabhsharma on Jan 19, 2012, 4:53 AM)


rovf
Veteran

Jan 19, 2012, 5:58 AM

Post #2 of 8 (1337 views)
Re: [saurabhsharma] Replacing exact match of IP from list of files [In reply to] Can't Post

What characters in front of the first digit do "delimit" those IP addresses you want to replace? Anything non-numeric? Any special character? Only a certain set of special characters? You need to define this, before you can solve this problem, because you want to search for a pattern, which are NOT preceded by another pattern.

After having done this specification task, look at

perldoc perlre

You will find there, for example:

\D ... matches a non-digit
\b ... matches a word boundary
[^.....] ... matches any character which is not in the set
\W ... matches a non-word character
(?<=pattern) ... zero-width look behind

and many more.


saurabhsharma
Novice

Jan 20, 2012, 12:10 AM

Post #3 of 8 (1333 views)
Re: [rovf] Replacing exact match of IP from list of files [In reply to] Can't Post

Hi rovf,

The pattern i am looking for is :

if preceded or end by any numeric digit the string should not be replaced that is why i have used

([^\d])$OLD([^\d]) in the search pattern,

but what it does is that once it finds the pattern it is replacing it with first character as Alphabetical character and garbling the replaced pattern as i quoted in the question.

10.23.23.241 url=jdbc:oracle:thin:@//110.235.345.21/H.10.10.10.abc.com



I am unable to figure out why, but when i replace “to be replaced” string

10.10.10.10 to start with any character it is working fine

i.e if i change 10.10.10.10 to let’s say A10.10.10.10 the replacement is working fine and it would replace the above mentioned string to

10.23.23.241 url=jdbc:oracle:thin:@//110.235.345.21//A10.10.10.10.abc.com



Can you please explain why ?


saurabhsharma
Novice

Jan 20, 2012, 12:56 AM

Post #4 of 8 (1332 views)
Re: [saurabhsharma] Replacing exact match of IP from list of files [In reply to] Can't Post

i rovf,

I figured out the problem, it was the problem with the perl command i was giving, i must have replace \1 and \2 in my substitution command to \${1} and \${2}

old command was

/usr/bin/perl -p -i -e "s/([^\d])$OLD_VAL([^\d])/\1$NEW_VAL\2/" $FILENAME

the correct one is

/usr/bin/perl -p -i -e "s/([^\d])$OLD_VAL([^\d])/\${1}$NEW_VAL\${2}/" $FILENAME

Now its working like charm Smile

Cheers,

Saurabh Smile


rovf
Veteran

Jan 20, 2012, 2:11 AM

Post #5 of 8 (1328 views)
Re: [saurabhsharma] Replacing exact match of IP from list of files [In reply to] Can't Post

BTW, you can write [^\d] in a more compact way as \D.


saurabhsharma
Novice

Jan 22, 2012, 9:05 PM

Post #6 of 8 (1318 views)
Re: [rovf] Replacing exact match of IP from list of files [In reply to] Can't Post

Yes

But using ([\D]*) is better Smile


rovf
Veteran

Jan 23, 2012, 1:52 AM

Post #7 of 8 (1308 views)
Re: [saurabhsharma] Replacing exact match of IP from list of files [In reply to] Can't Post

In your case, no. Aside from the fact that the brackets are unnecessary ([\D] can be simply written as \D), the asterisk doesn't make sense. \D* means: "zero or no non-digits". Hence it matches ANY zero-length part of your string - certainly not what you have in mind.

You could write \D+, but since you don't collect the non-digits, this doesn't bring any advantage over just writing \D.

You should, however, conisder the case, whether the pattern you are looking for, could also appear at the *start* of the string. In this case, you would need something like (^|\D).


saurabhsharma
Novice

Jan 23, 2012, 5:33 AM

Post #8 of 8 (1303 views)
Re: [rovf] Replacing exact match of IP from list of files [In reply to] Can't Post

Yes i had used ([^\d]|^) instead, (^|\D) works the same way and look cleaner too.

Thanks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives