使用sed搜索并替换文件中的ip地址

时间:2022-06-01 19:17:15

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 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.

我试着让它工作一段时间,但并没有真正得到它。基本上,我有一个具有ip地址的文件,它每天都会或多或少地发生变化。这个文件只包含一个ip地址,这是我想用我的疯狂的grepping来替换的一个ip地址。

I have this

我有这个

#!/bin/sh

newip=$(ifconfig | grep 0xfff | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | grep -v 255)

echo $newip
sed 's/*\.*\.*\.*/"$newip"/g' log.txt > logmod.txt

but it's not matching and replacing. I'm not familiar with sed and I am a beginner with regexps too.

但它不是匹配和替换。我不熟悉sed,我也是regexp的初学者。

Any help would be awesome! Thanks :)

任何帮助都会很棒!谢谢:)

2 个解决方案

#1


11  

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选项),您可以做类似的事情(类似于您的grep语句)。另外,注意$newip在单引号之外,允许shell替换它。

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.

顺便说一句,这个解决方案仍然匹配不表示IP地址的字符串。有关更复杂的解决方案,请参见IP地址下面的站点。

#2


1  

IP=207.0.0.2; [[ x${IP}x =~ x"(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])"x ]] && echo ok || echo bad

this validates only four decimal octet representation so this one will fail 016.067.006.200 (even valid but not four decimal octet representation, but octal)

它只验证4个十进制八位元表示法,所以这个将失败016.067.006.200(即使有效但不是4个十进制八位元表示法,而是八进制)

016.067.006.200 =~ 14.55.6.200

#1


11  

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选项),您可以做类似的事情(类似于您的grep语句)。另外,注意$newip在单引号之外,允许shell替换它。

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.

顺便说一句,这个解决方案仍然匹配不表示IP地址的字符串。有关更复杂的解决方案,请参见IP地址下面的站点。

#2


1  

IP=207.0.0.2; [[ x${IP}x =~ x"(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(2([0-4][0-9])|2(5[0-5])|1[0-9][0-9]|[1-9][0-9]|[0-9])"x ]] && echo ok || echo bad

this validates only four decimal octet representation so this one will fail 016.067.006.200 (even valid but not four decimal octet representation, but octal)

它只验证4个十进制八位元表示法,所以这个将失败016.067.006.200(即使有效但不是4个十进制八位元表示法,而是八进制)

016.067.006.200 =~ 14.55.6.200