简体   繁体   中英

Powershell replace double quotes with single in a string

I just can't get it right

The below statement throws an exception and I cannot get the correct format

$appendedQry = $appendedQry -replace "\"","'"

What would be the correct syntax?

它应该是

$appendedQry = $appendedQry -replace '"',''''

this because the escaping character is `

in the following a working example

$appendedQry = "`"asd"
echo $appendedQry
$appendedQry = $appendedQry -replace "`"", "'"
echo $appendedQry

I was replacing all occurances of the string in a file, using command

powershell -Command "(gc c:\input.txt) -replace 'aaa', 'bbb' | Out-File c:\output.txt"

To replace double quotes I need to do some trick - use the variable:

$ToReplace = "\" + """"
$command = "(gc c:\input.txt) -replace '" + $ToReplace + "', 'bbb' | Out-File c:\output.txt"
powershell -Command $command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM