Replace multiple strings in multiple rows in html file

Hi All. I have html file and need to find row that contains string !dataset1! (easy part) and replace that row + next row with data stored in variable @mydata. I tried various things but it didn’t work.
!dataset1! is the only constant value I set in this html to find specific table.

$mydata = @'
some data
some data new row
'@
Table !dataset1! Data Table !dataset2! Data

To find a string in a text you could use Select-String. This would give you the line of text the string was found in. This you could use to replace it.

[quote quote=226915]To find a string in a text you could use Select-String. This would give you the line of text the string was found in. This you could use to replace it.

[/quote]
Tried without success in this case. Which code you would use?

Select-String -Path 'Complete Path To Your HTML File' -Pattern '!dataset1!' -SimpleMatch | Select-Object LineNumber

You should always read the complete help including the examples for the cmdlets you’re about to use to learn how to use them. And it helps usually to play a little bit with it and to try this and to try that.

[quote quote=226936][/quote]
I have no issue with finding string with select-string but to replace row with !dataset1! + next row with my variable.

When you know the line numbers you just skip these lines with Select-Object.

[INT]$MatchedLine = Select-String -Path 'Complete Path To Your HTML File' -Pattern '!dataset1!' -SimpleMatch | Select-Object -ExpandProperty LineNumber
$HTMLContent = Get-Content -Path 'HTML File'
$BeforeMatchedLine = $HTMLContent | Select-Object -First ($MatchedLine - 1)
$AfterMatchedLine = $HTMLContent | Select-Object -Skip ($MatchedLine + 1)
$BeforeMatchedLine, $mydata, $AfterMatchedLine | Out-File -FilePath 'HTML File'

[quote quote=226954][/quote]
Very creative. You accomplished that without -replace command.

I haven’t seen/used LineNumber property before.

Thank you very much!

I recommended that in my first answer. :-/

Yeah, but your example didn’t makes sense to me at the moment :slight_smile: