powershell - Swapping text in an HTML file -
i want update bit of text in html file. text in html file:
<html><body>normal</body></html>
i want change normal
rollout
, vice versa. code @ first was
(get-content c:\temp\a.html) | foreach-object {$_ -replace "normal", "rollout"} | set-content c:\temp\a.html
this worked great, except want run same script change rollout normal. trying if else if's didn't work either. below 1 of many attempts using if..elseif
:
$a = get-content c:\temp\a.html (get-content c:\temp\a.html) | if($a -match "normal") { foreach-object {$_ -replace "normal", "rollout"} } | elseif($a -match "rollout") { foreach-object {$_ -replace "rollout", "normal"} } | set-content c:\temp\b.html
how can approach differently?
using [regex]::replace
, script block delegate , substitution hash:
$replace = @{ normal = 'rollout' rollout = 'normal' } $x = @( '<html><body>normal</body></html>' '<html><body>rollout</body></html>' ) $x | foreach { [regex]::replace( $_,'(normal|rollout)',{$replace[$args[0].value]} ) } <html><body>rollout</body></html> <html><body>normal</body></html>
that toggle between "normal" , "rollout"
Comments
Post a Comment