Replace multiple strings in multiple rows in html file

Welcome Forums General PowerShell Q&A Replace multiple strings in multiple rows in html file

Viewing 8 reply threads
  • Author
    Posts
    • #226894
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      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
      • This topic was modified 3 weeks, 3 days ago by sasaz12.
      • This topic was modified 3 weeks, 3 days ago by sasaz12.
      • This topic was modified 3 weeks, 3 days ago by sasaz12.
      • This topic was modified 3 weeks, 3 days ago by sasaz12.
      • This topic was modified 3 weeks, 3 days ago by sasaz12. Reason: Had an issue with pasting html code
    • #226915
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      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.

    • #226921
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      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?

    • #226936
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP
      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.

    • #226942
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      I have no issue with finding string with select-string but to replace row with !dataset1! + next row with my variable.

    • #226954
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      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'
    • #226987
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      Very creative. You accomplished that without -replace command.

      I haven’t seen/used LineNumber property before.

      Thank you very much!

    • #227032
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      I haven’t seen/used LineNumber property before.

      I recommended that in my first answer. :-/

    • #227041
      Participant
      Topics: 2
      Replies: 7
      Points: 65
      Rank: Member

      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 🙂

Viewing 8 reply threads
  • You must be logged in to reply to this topic.