How to find and replace ALL IP addresses with a new one

How to find and replace ALL IP addresses with a new one

To find and replace all IP addresses with a new one, you can use the sed command. The syntax is as follows:

sed -i 's/old-ip-address/new-ip-address/g' file
Source:‮‬www.lautturi.com

Replace old-ip-address with the IP address you want to replace, new-ip-address with the new IP address, and file with the name of the file you want to modify.

For example, to replace all occurrences of the IP address 192.168.0.1 with the IP address 192.168.1.1 in a file called config.txt, you would run the following command:

sed -i 's/192.168.0.1/192.168.1.1/g' config.txt

This will search the config.txt file for all occurrences of the IP address 192.168.0.1 and replace them with the IP address 192.168.1.1. The -i option tells sed to edit the file in place, and the g flag at the end of the substitution command tells it to perform the replacement globally, meaning it will replace all occurrences of the IP address rather than just the first one.

You can also use the find and xargs commands to search for and replace IP addresses in multiple files. The syntax is as follows:

find /path/to/search -type f -exec sed -i 's/old-ip-address/new-ip-address/g' {} +
Created Time:2017-10-16 14:38:46  Author:lautturi