LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Perl Script to replace the number from text file (https://www.linuxquestions.org/questions/linux-newbie-8/perl-script-to-replace-the-number-from-text-file-815446/)

kishorebabue 06-21-2010 10:39 AM

Perl Script to replace the number from text file
 
Hi,
Can anyone help me out on creating a new perl script which replace IP address from the text file. eg. If in a file, we found any word like 11.222.333.44 then it has to be replaced to XX.XXX.333.44

centosboy 06-21-2010 10:50 AM

Quote:

Originally Posted by kishorebabue (Post 4010341)
Hi,
Can anyone help me out on creating a new perl script which replace IP address from the text file. eg. If in a file, we found any word like 11.222.333.44 then it has to be replaced to XX.XXX.333.44

you do not really need a perl script for this.
sed can do this quite easily. if you must use perl, a one liner is sufficient.

Code:

sed -i.bak -e's#a\.b#c\.d#  filename
--


Code:

perl -pi.bak -e's#a\.b#c\.d# filename

kishorebabue 06-24-2010 09:25 AM

Quote:

Originally Posted by centosboy (Post 4010360)
you do not really need a perl script for this.
sed can do this quite easily. if you must use perl, a one liner is sufficient.

Code:

sed -i.bak -e's#a\.b#c\.d#  filename
--


Code:

perl -pi.bak -e's#a\.b#c\.d# filename


Thanks for the command. But this doesnt work as per criteria.
The log file conatins different ip addresses and that to be replaced from nnn.nn.nn.nnn to XXX.XX.nn.nnn so this to be done in perl script using loop constraints.

centosboy 06-24-2010 10:00 AM

tell me exactly what needs doing and i'll write it if i get a chance - or give you pointers

kishorebabue 06-24-2010 02:27 PM

i explain with simple example as below,

Input file: test_log.txt

File COntents:
This is a test file ip addr 123.23.43.333 hello.All r welocme
and another ip 34.54.222.122:1234 with the new server and so onnnn.
This is a test file ip addr 999.323.43.222 hello.All r welocme
and another ip 191.22.33.182 with the new server and so onnnn.
This is a test file ip addr 145.66.77.191 hello.All r welocme
and another ip 34.54.222.122:1234 with the new server and so onnnn.
End of file

So i need a different output file as below,
This is a test file ip addr XXX.XX.43.333 hello.All r welocme
and another ip XX.XX.222.122:1234 with the new server and so onnnn.
This is a test file ip addr XXX.XXX.43.222 hello.All r welocme
and another ip XXX.XX.33.182 with the new server and so onnnn.
This is a test file ip addr XXX.XX.77.191 hello.All r welocme
and another ip XX.XX.222.122:1234 with the new server and so onnnn.
End of file


i.e whereever i find any ip addresses(like 145.22.12.333, 22.333.44.434,......) in the input file, the first 2 contents of the id address alone to be replaced(like XXX.XX.12.333, XX.XXX.44.434, .....)

grail 06-24-2010 07:09 PM

Well I am no perl guru but you can probably transpose the following sed into the perl example above:
Code:

sed -r 's/^([^0-9]*)[0-9]+\.[0-9]+/\1XXX\.XXX/' file
The difference here is that it is not replacing the numbers with the same number of 'X's, is this important?

kishorebabue 06-30-2010 03:32 PM

im using the command, 's/(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/XXX.XXX.$3.$4/g' to replace all ip address in a file.
but if i found any string like 222.333.444.555.444.333 also gets replaced as 222.333.XXX.XXX.444.333

222.333.444.555.444.333 = Not an valid ip address, shld not replace
222.333.444.555:3245 = ip address with port, must replaced as XXX.XXX.444.555:3245
ip/222.333.444.555 = valid ip address , must replaced as ip/XXX.XXX.444.555

How to overcome with this...

grail 06-30-2010 10:56 PM

Well assuming the format is the same you could just put a space in front of the first set of digits, this will get rid of an invalid grouping
ie not 4 sets of numbers

kishorebabue 07-02-2010 09:53 AM

but this also ignores the valid format like,
"192.22.33.232"
192.22.33.232

I just want to ignore when it is more than 4 octets, like 222.333.444.555.444.333


All times are GMT -5. The time now is 04:47 PM.