Using sed to search and replace an ip address in a file

前端 未结 4 1099

Been trying to get this working for a while and not really quite getting it. Basically, I have a file with an ip address that changes more or less on a daily basis. The file

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 17:08

    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.

    sed -r 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b'/"$newip"/
    

    BTW this solution still matches strings that do not represent IP addresses. See this site under IP Adresses for more complex solutions.

提交回复
热议问题