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: Edit Log



saurabhsharma
Novice

Jan 19, 2012, 3:36 AM


Views: 1289
Replacing exact match of IP from list of files

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)


Edit Log:
Post edited by saurabhsharma (Novice) on Jan 19, 2012, 4:09 AM
Post edited by saurabhsharma (Novice) on Jan 19, 2012, 4:47 AM
Post edited by saurabhsharma (Novice) on Jan 19, 2012, 4:53 AM


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

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