How to replace an IP address in SED shell?
If your version of sed supports extended regular expressions (the -r option), you could do something like this (which is similar to what you have in your grep statement). Also note $newip is outside the single quotes to allow the shell to replace it. BTW this solution still matches strings that do not represent IP addresses.
How to search and replace an IP address?
Basically, I have a file with an ip address that changes more or less on a daily basis. The file only contains one ip address and this is the one I’m trying to replace with my crazy grepping to find my current internal ip. but it’s not matching and replacing.
How to replace an IP address in regex?
Also note $newip is outside the single quotes to allow the shell to replace it. BTW this solution still matches strings that do not represent IP addresses. See this site under IP Adresses for more complex solutions.
How to print out a list of IP addresses?
The -n switch causes it to iterate line by line over the input file; it will then iterate over each occurrence of an IP address, and it’ll print out each of those IP addresses to a separate line. The Regexp::Common module will have a pattern which matches IP addresses, which is available in $RE {net} {IPv4}.
Is it safe to use sed on IP address?
To be 200% safe, you also need to make sure that the dots in your currip variable are escaped to prevent sed from treating them as wildcards. Last edited by Dark_Helmet; 01-05-2012 at 06:34 PM. Thank you for the advise… it is definitely better to be 100+% safe. What regex should I use if the IP address is not on a word boundary. For example:
When to use regex to replace IP address?
What regex should I use if the IP address is not on a word boundary. For example: I need to generate a sed command to replace “10.2.123.1” with “10.2.123.3” in lines 1 & 2 and NOT change “10.2.123.10” to “10.2.123.30” in line 1. Is it possible to use the same sed command to cover both lines in the Config File above?
How to match whole words in SED on Mac?
\\b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character): On Mac OS X, neither of these regex syntaxes work inside sed for matching whole words Hear me now and believe me later, this ugly syntax is what you need to use: