Looping through variable and replace text

Welcome Forums General PowerShell Q&A Looping through variable and replace text

Viewing 3 reply threads
  • Author
    Posts
    • #194606
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      Hi,

      I’m trying to loop through a variable, split each line by | (giving 4 tokens), then search for “a|b|c” in a file and replace with “a|b|d”

      Essentially i have a csv file that contains a load of parts and prices and while it’s not ideal to be in csv many people use it so we need to continue using it like this, i want to check each part every day to see if any prices in the csv sheet have changed compared to an API feed.

      If there is a change in price i want to update the file with the new price, so…

      The variable data looks like this

      Token 3 being the old price and 4 being the new price

      I run the following and because price $updatesPending evaluates to True it runs the IF statement

      The issue is the output in the file looks like this so i’m clearing doing very wrong, possibly the entirely wrong command?

       

      Any help will be appreciated.

       

      Regards,

      Jamie

    • #194627
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      The -replace statement uses regex for the search pattern and in regex the “|” represents a logical “or” – that’s why you fail at the moment. 😉 If you have a valid csv file why don’t you treat it as such and use Import-CSV instead of get content?

    • #194636
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Here’s a bit of a different approach. Powershell is strongest when you are leveraging object. While you can do text manipulation, the other file is in a CSV format, so it will be more predictable leveraging it as an object. This approach would take an update file and consider it autho:

      The output can be Sorted by PartNumber if you want, but basically if you un-remark the lines under $results you would overwrite the file. You can also make backups of everything along the way.

      Output:

    • #194675
      Participant
      Topics: 6
      Replies: 10
      Points: 103
      Rank: Participant

      The -replace statement uses regex for the search pattern and in regex the “|” represents a logical “or” – that’s why you fail at the moment. 😉 If you have a valid csv file why don’t you treat it as such and use Import-CSV instead of get content?

      Hi Olaf,

      Thank you for highlighting that -replace uses regex, i should have picked up on this but it explains the issue i was having.
      With regards to why not using Import-Csv… I wasn’t actually aware of it, I’m new to Powershell, I have always used batch but trying to break that habit (finally).

      After tinkering with Import-Csv I have now managed to get the values to update as intended so thank you very much for that pointer.

      Being able to reference specific rows / columns using an array is awesome, having used batch files for so long i have been super limited and feel like an idiot for not jumping ship years ago :).

      Here’s a bit of a different approach. Powershell is strongest when you are leveraging object. While you can do text manipulation, the other file is in a CSV format, so it will be more predictable leveraging it as an object. This approach would take an update file and consider it autho:

      Hi Rob,

      Thank you for your input again, I hadn’t actually seen your reply until now sorry but will certainly play with this approach too.

       

      Thank you both.

Viewing 3 reply threads
  • The topic ‘Looping through variable and replace text’ is closed to new replies.