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 |
Quote:
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 |
Quote:
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. |
tell me exactly what needs doing and i'll write it if i get a chance - or give you pointers
|
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, .....) |
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 |
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... |
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 |
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. |