简体   繁体   English

Powershell用字符串中的单个引号替换双引号

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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