使用SED在文件中搜索并替换IP地址 [英] Using sed to search and replace an ip address in a file

查看:5589
本文介绍了使用SED在文件中搜索并替换IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直试图得到这个工作了一段时间,而不是真的很得到它。
基本上,我有改变或多或少每天的基础上的IP地址的文件。该文件只包含一个IP地址,这是一个我想用我的疯狂grepping替换找到我目前的内部IP。

我有这种

 #!/ bin / sh的newip = $(使用ifconfig | grep的0xFFF的| grep的-eo([0-9] {1,3} \\){3} [0-9] {1,3}'| grep的-v 255)回声$ newip
SED的/*\\.*\\.*\\.*/\"$ newip/ G'的log.txt> logmod.txt

但它不匹配和替换。
我不熟悉sed和我用正则表达式初学者了。

任何帮助将是真棒!
感谢:)


解决方案

如果你的版本 SED 支持扩展正前pressions(即 -r 选项),你可以做这样的事情(这类似于你在你的的grep 语句是什么)。还要注意 $ newip 是单引号外面让Shell来替换它。

 的sed -r的/(\\ B〔0-9] {1,3} \\。){3} [0-9] {1,3} \\ B' / $ newip /

BTW这个解决方案仍然匹配不重present IP地址字符串。请参见 IP不会忽略作为更复杂的解决方案。 p>

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.

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.

Any help would be awesome! Thanks :)

解决方案

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.

这篇关于使用SED在文件中搜索并替换IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆