# Replace multiple strings in multiple rows in html file

**URL:** <https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355>\
**Category:** PowerShell Help\
**Created:** [May 9, 2020, 1:21pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355 "2020-05-09T13:21:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sasaz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sasaz/32/1821_2.png) [@sasaz](https://forums.powershell.org/u/sasaz)\
**Post date:** [May 9, 2020, 1:21pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/1 "2020-05-09T13:21:34Z")

</div>

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 |

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 9, 2020, 2:07pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/2 "2020-05-09T14:07:48Z")

</div>

To find a string in a text you could use [Select-String](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7). This would give you the line of text the string was found in. This you could use to replace it.

---

<div class="post-metadata">

**Author:** ![sasaz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sasaz/32/1821_2.png) [@sasaz](https://forums.powershell.org/u/sasaz)\
**Post date:** [May 9, 2020, 2:57pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/3 "2020-05-09T14:57:14Z")

</div>

[quote quote=226915]To find a string in a text you could use [Select-String](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7). 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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 9, 2020, 4:00pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/4 "2020-05-09T16:00:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sasaz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sasaz/32/1821_2.png) [@sasaz](https://forums.powershell.org/u/sasaz)\
**Post date:** [May 9, 2020, 4:27pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/5 "2020-05-09T16:27:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 9, 2020, 5:58pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/6 "2020-05-09T17:58:13Z")

</div>

When you know the line numbers you just skip these lines with [Select-Object](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7).

```
[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'
```

---

<div class="post-metadata">

**Author:** ![sasaz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sasaz/32/1821_2.png) [@sasaz](https://forums.powershell.org/u/sasaz)\
**Post date:** [May 10, 2020, 12:19am UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/7 "2020-05-10T00:19:01Z")

</div>

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

I haven’t seen/used LineNumber property before.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 10, 2020, 6:10am UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/8 "2020-05-10T06:10:15Z")

</div>

> [@](#):
>
> I haven’t seen/used LineNumber property before.

I recommended that in my first answer. :-/

---

<div class="post-metadata">

**Author:** ![sasaz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sasaz/32/1821_2.png) [@sasaz](https://forums.powershell.org/u/sasaz)\
**Post date:** [May 10, 2020, 7:16am UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/9 "2020-05-10T07:16:35Z")

</div>

> [@](#):
>
> > [sasaz12 wrote:](https://powershell.org/forums/topic/replace-multiple-strings-in-multiple-rows-in-html-file/#post-226987)
> > I haven’t seen/used LineNumber property before.
> 
> I recommended that in my first answer. :-/

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/replace-multiple-strings-in-multiple-rows-in-html-file/14355/10 "2024-05-16T20:30:08Z")

</div>

Closed on May 16, 2024, 8:30pm UTC


