[英]Powershell replace with double quotes and new line
I wanna replace something with double quotes, but also need a new line in the middle.我想用双引号替换一些东西,但中间还需要一个新行。 How can I do this?我怎样才能做到这一点? I tried all combinations, but it seems to me that a new line needs double quotes, is there any other way?我尝试了所有组合,但在我看来,新行需要双引号,还有其他方法吗?
For example:例如:
(Get-Content '.\input.xml') -replace 'version="1.0">', 'version="1.0"> "`n `n" iNeedANewLineHere' | Set-Content -encoding UTF8 '.\output.xml'
This way it doesnt interpret the nl-commands as new lines, it prints everything as text, in cause of the single quotes... is there any other way to solve this problem?这样它就不会将 nl 命令解释为新行,而是将所有内容打印为文本,因为单引号......还有其他方法可以解决这个问题吗?
Use double quotes to enclose your string, and escape the inner double quotes as such:使用双引号将您的字符串括起来,并像这样转义内部双引号:
(Get-Content '.\input.xml') -replace 'version="1.0">', "version=`"1.0`"> `"`n `n`" iNeedANewLineHere" | Set-Content -encoding UTF8 '.\output.xml'
Or like this:或者像这样:
(Get-Content '.\input.xml') -replace 'version="1.0">', "version=""1.0""> ""`n `n"" iNeedANewLineHere" | Set-Content -encoding UTF8 '.\output.xml'
If you are executing this from CMD you will need to escape any internal double quotes that you are trying to pass to PowerShell in CMD as they are passed.如果您从 CMD 执行此操作,您将需要在传递时转义您尝试传递给 CMD 中 PowerShell 的任何内部双引号。 This should do that:这应该这样做:
powershell -Command "(Get-Content '.\input.xml') -replace 'version=^"1.0^">', ^"version=^"^"1.0^"^"> `n `n iNeedANewLineHere^" | Set-Content -encoding UTF8 '.\output.xml'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.