0

I read this topic First step which was usefull to begin with. I can add several lines into my hosts file.

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

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 :)

Community
  • 1
  • 1
user2381807
  • 15
  • 1
  • 6

2 Answers2

0

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

Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
0

example with sed for Windows:

sed -ri.bak "s/(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9]) website.com/NewIP website.com/ig" hosts

sed makes a backup copy hosts.bak.

Endoro
  • 37,015
  • 8
  • 50
  • 63