[英]How do I replace this text in a file from the command line in Linux?
I have a proxy config file which has the following line: 我有一个代理配置文件,其中包含以下行:
Allow 212.21.3.44
I'd like to replace that IP address portion with my new IP address when it changes. 我希望在更改时用我的新IP地址替换该IP地址部分。 It would probably be easier to just use the line number when searching for it &ndash I don't think that config file will change at all other than that particular setting.
在搜索时可能更容易使用行号&ndash我认为除了特定设置之外,配置文件根本不会改变。
How can I do it from the command line with something like like Perl, sed, etc.? 如何从命令行执行类似Perl,sed等的操作?
如果要使用sed更改特定的行号,可以使用:
sed -i '<line number> s/Allow .*$/Allow <new ip>/g' <filename>
sed -i 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/my.new.ip.here/' file.txt
-i
标志将在适当的位置更改文件(取决于您的发行版/ * nix的风格 - 适用于我的Ubuntu)。
如果行号确实改变了
sed -i '/Allow/s/Allow .*$/Allow <new ip>/' <filename>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.