Data Replacement Method Needed

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Danielle Boyd Danielle Boyd 1 month, 1 week ago.

  • Author
    Posts
  • #64114
    Profile photo of Danielle Boyd
    Danielle Boyd
    Participant

    I have 5 *.xml files in a directory. Within each file, there is a line: Content-ID:

    The literal content id data is 3-4 delimited pieces. I need to replace the 1st **** with $date ( $date = Get-Date -format "yyyyMMdd" )

    Your suggestions are greatly appreciated. Thank you!

  • #64117
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Show your code.

  • #64120
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    On a high level you could do something like this:

    Read all xml files you want to modify and store this list in a variable. Create a foreach loop over this variable. Inside this loop read the xml nodes of the file, select the node you want to change and use the -replace operator with a sofisticated regex pattern. Then you just have to save the changed xml data either to the original file or to a new one.

    • #64201
      Profile photo of Danielle Boyd
      Danielle Boyd
      Participant

      Thank you so much, Olaf. Here's my biggest struggle: I don't know what the text is that's needing to be replaced.

      Content-ID: sbteev510b2bsbt-rn20-preamble

      This is a line in the xml files where I need to replace sbteev510b2bsbt. "sbteev510b2bsbt" will be different every time.

      What if you don't know what you are replacing but know where it is: I know the line begins with Content-ID and I want to replace sbteev510b2bsbt

      I have been scouring and reading but not finding the answer. Thank you!

    • #64204
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      😉 That's the moment where the sophisticated regex pattern appears on the stage ....

      $date = Get-Date -format "yyyyMMdd"
      'Content-ID: sbteev510b2bsbt-rn20-preamble' -replace '(?< =Content-ID:\s+).+?(?=-rn20-preamble)',$date

      You just need to know the "structure" of the ID or the possible format. If it's always something between a white space and a "minus" ... it will fit.

    • #64207
      Profile photo of Danielle Boyd
      Danielle Boyd
      Participant

      Wow; you made that SO EASY!!!! Thank you for teaching!

You must be logged in to reply to this topic.