Windows批处理在主机文件中添加/替换条目 [英] Windows batch to add/replace entry in hosts file
问题描述
我阅读了此主题第一步,这对开始很有用和.我可以在主机文件中添加几行.
I read this topic First step which was usefull to begin with. I can add several lines into my hosts file.
我现在正在尝试用正则表达式搜索和替换字符串,但没有成功.例如,如果我的主机文件包含"SOMEIP website.com",我想修改此行以获得"NEWIP website.com".
I am now trying, without success, to search and replace a string with regular expression. For exemple, if my host file contains "SOMEIP website.com" I would like to modify this line to obtain "NEWIP website.com".
我可以找到一行是否包含"website.com",我目前不知道(如果可能的话)如何获取该行并通过正则表达式模式替换IP地址.
I can find if a line contains "website.com", I currently don't know (if possible) how to get that line and replace the IP address via a regular expression pattern.
%windir%\system32\FIND /C /I "website.com" %hostpath% >nul
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^%server% website.com>>%hostpath%
我迷路了,谢谢您的回答:)
I'm lost so thank you for your answers :)
推荐答案
请参见 https://stackoverflow.com/a/16735079/dbenham的repl.bat的891976 ,它支持正则表达式搜索和替换.
See https://stackoverflow.com/a/16735079/891976 for repl.bat by dbenham which supports regular expression search and replace.
type "HOSTS" | repl "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b website.com" "NEWIP website.com" m >"HOSTS.new"
move /y HOSTS.new HOSTS
Repl.bat: http://www.dostips .com/forum/viewtopic.php?f = 3& t = 3855
Repl.bat: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
这篇关于Windows批处理在主机文件中添加/替换条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!