Data Replacement Method Needed

Welcome Forums General PowerShell Q&A Data Replacement Method Needed

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
1 year, 8 months ago.

  • Author
    Posts
  • #64114

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 10
    Rank: Member

    Show your code.

  • #64120

    Participant
    Points: 10
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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

      Participant
      Points: 10
      Rank: Member

      😉 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

      Participant
      Points: 0
      Rank: Member

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

The topic ‘Data Replacement Method Needed’ is closed to new replies.