GC -replace command has problem with ” sign

Welcome Forums General PowerShell Q&A GC -replace command has problem with ” sign

Viewing 6 reply threads
  • Author
    Posts
    • #248968
      Participant
      Topics: 1
      Replies: 3
      Points: -20
      Rank: Member

      Hi,

      I’m new here and can’t say that I’m anywhere near to call myself an expert in Powershell. The commands I use I have found on Google 🙂 But usually I achieve what I want in the end.

      The problem I have now and can’t find a solution for is that I’m trying to replace a text-string in a bunch of files with this command
      powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List="licenseserver"' | Out-File -encoding ASCII file.txt"
      The problem is that the output is Server List= licenseserver”” rather than the desired Server List=”licenseserver”. Obviously it has a problem with the quote sign but I can’t find a way around it. Is it doable?

    • #249004
      Participant
      Topics: 0
      Replies: 7
      Points: 45
      Rank: Member
      Hi,
      I’m new here as well and just trying to help out.
      To use double-quotes (“) within a a section started with “, you need to add a second double-quote, ie “”. I have updated your code below to help.
      I hope this helps.
    • #249038
      Participant
      Topics: 1
      Replies: 3
      Points: -20
      Rank: Member

      Thanks, but the result is only that the space beteeen = and l is erased. So it get Server List=licenseserver””

    • #249041
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      If you’re not in powershell, you’d be better off there instead of calling powershell.exe. If you need to call powershell.exe from another shell/application, then I recommend still getting the syntax sorted out in powershell before taking and using it in this manner. Another important point, a lot of times when you copy code from the internet, the quotes or other characters can be converted into similar but different characters. I make it a habit to replace any single or double quotes at a minimum out of code retrieved from a web page. If you have the choice to download the source vs copying from a page – do that instead.

      Now to your issue posted, this command achieves your desired result in powershell. For the sake of others, I added sample code to create the file.

      [/crayon]

      New file contents:

      [/crayon]

      If you are indeed calling it from another environment, please add those details if you need assistance making this code work there.

    • #249092
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

      If you are calling this code from PowerShell, then you must escape inner like-quotes in PowerShell and CMD shells. Escaping prevents early closure of outer quotes. If you are calling from CMD only, then you only need to escape inner like-quotes in CMD.
      # From CMD</p><p>powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List=\"licenseserver\"' | Out-File -encoding ASCII file.txt"</p><p># From PowerShell</p><p>powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List=\""licenseserver\""' | Out-File -encoding ASCII file.txt"
      The reason for the double escape in PowerShell is PowerShell will parse the entire command string first. So each &quot;&quot; is parsed as &quot;. Then it is passed to CMD where the remaining \&quot; is parsed as &quot;.

      • This reply was modified 1 month, 1 week ago by grokkit.
    • #249095
      Participant
      Topics: 1
      Replies: 3
      Points: -20
      Rank: Member

      Simple as that! It worked, thanks!

      I’m using Powershell.exe.  I’ve been using the same script to replace other strings lots of times before without a problem. It was just this one there I needed to add ” in the text that caused me problem.

      So what you did was remove Powershell -command and the leading and trailing since it’s not needed from within Powershell and that automatically resolved the problem that RichGardener42 was on to?

      Now I’ve learned something new. Thanks!

       

    • #249851
      Participant
      Topics: 1
      Replies: 3
      Points: -20
      Rank: Member

      If you are calling this code from PowerShell, then you must escape inner like-quotes in PowerShell and CMD shells. Escaping prevents early closure of outer quotes. If you are calling from CMD only, then you only need to escape inner like-quotes in CMD.

      # From CMD

      <br />powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List=\"licenseserver\"' | Out-File -encoding ASCII file.txt"</p><p># From PowerShell</p><p> powershell -Command "(gc file.txt) -replace 'Server List=licenseserver', 'Server List=\""licenseserver\""' | Out-File -encoding ASCII file.txt"

      The reason for the double escape in PowerShell is PowerShell will parse the entire command string first. So each "" is parsed as ". Then it is passed to CMD where the remaining \" is parsed as ".

      Cool! Thanks, that did work too!

      Learned another new thing 🙂

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