Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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, .....)
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.