Welcome › Forums › General PowerShell Q&A › Replace multiple strings in multiple rows in html file
- This topic has 8 replies, 2 voices, and was last updated 8 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
May 9, 2020 at 6:51 pm #226894
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.PowerShell1234$mydata = @'some datasome data new row'@Table !dataset1!
Data
Table !dataset2!
Data-
This topic was modified 8 months, 3 weeks ago by
sasaz12.
-
This topic was modified 8 months, 3 weeks ago by
sasaz12.
-
This topic was modified 8 months, 3 weeks ago by
sasaz12.
-
This topic was modified 8 months, 3 weeks ago by
sasaz12.
-
This topic was modified 8 months, 3 weeks ago by
sasaz12. Reason: Had an issue with pasting html code
-
This topic was modified 8 months, 3 weeks ago by
-
May 9, 2020 at 7:37 pm #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.
-
May 9, 2020 at 8:27 pm #226921
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.
Tried without success in this case. Which code you would use?
-
May 9, 2020 at 9:30 pm #226936PowerShell1Select-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.
-
May 9, 2020 at 9:57 pm #226942
I have no issue with finding string with select-string but to replace row with !dataset1! + next row with my variable.
-
May 9, 2020 at 11:28 pm #226954
When you know the line numbers you just skip these lines with Select-Object.
PowerShell12345[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' -
May 10, 2020 at 5:49 am #226987
Very creative. You accomplished that without -replace command.
I haven’t seen/used LineNumber property before.
Thank you very much!
-
May 10, 2020 at 11:40 am #227032
I haven’t seen/used LineNumber property before.
I recommended that in my first answer. :-/
-
May 10, 2020 at 12:46 pm #227041
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 🙂
-
-
AuthorPosts
- The topic ‘Replace multiple strings in multiple rows in html file’ is closed to new replies.