Home > database >  In these Powershell -replace commands from the cmd prompt, why does the first escape double quotes s
In these Powershell -replace commands from the cmd prompt, why does the first escape double quotes s

Time:09-28

C:\>Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace '\u0020\u0020\u0020\u0020\u007D\u002C\u000d\u000A\u0020\u0020\u0020\u0020\u0020\u0020\u0022\u0073\u0074\u0075\u0064\u0065\u006E\u0074\u0054\u006F\u0074\u0061\u006C\u0022\u003A\u0020', \"      `\"studentTotal`\": \" | Out-File -FilePath allacts.txt -Force -Encoding ASCII -nonewline"

This works correctly and doesn't return any errors. As you can see, I need to replace a string with a double quote.

C:\>Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace '\u0022\u000d\u000A\u0020\u0020\u0020\u0020\u0020\u0020', \"    `\"   \" | Out-File -FilePath allacts.txt -Force -Encoding ASCII -nonewline"
'Out-File' is not recognized as an internal or external command,
operable program or batch file.

The above does not work. I don't understand why - it's escaped in exactly the same way as the first command.

I have to use these commands from within a batch file as I can't run Powershell scripts on this system. I've searched through a lot of questions on SO about escaping quotes. ^ does not work inside double quotes. If the presence of | is the problem, as suggested in this answer., then it shouldn't work for either of these examples.

You can see that I used unicode for the "find" portion of the command. I would use it in the "replace" portion but it is interpreted literally, whether I use single or double quotes. That is it will not insert the " character but the six characters /u0022 - if you know how to make that work the way I need it to, please tell.

Edit:

Powershell -NoProfile "(Get-Content -Raw .\allacts.txt) -replace '\u0022\u000d\u000A\u0020\u0020\u0020\u0020\u0020\u0020', \"    `\"   `\"   \" | Out-File -FilePath allacts.txt -Force -Encoding ASCII -nonewline"

works but this means I get two double quotes when I only want one.

CodePudding user response:

The second line has an unbalanced single quote and unbalanced double quote in the replace-with part. Correct this and the script will work the same as the first line.

  • Related