简体   繁体   English

Powershell 用双引号和新行替换

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

相关问题 如何使用 powershell 命令用新行替换字符串? 从批处理文件中? - 双引号问题 - How can I make use a powershell command to replace a string witha new line? From within a batch file? -double quotes issue Powershell用字符串中的单个引号替换双引号 - Powershell replace double quotes with single in a string PowerShell替换具有双引号的字符串 - PowerShell to replace string having double quotes PowerShell - escaping 花式单引号和双引号,用于正则表达式和字符串替换 - PowerShell - escaping fancy single and double quotes for regex and string replace PowerShell脚本将双引号内的逗号替换为空 - PowerShell script to replace commas within double quotes with nothing Powershell 脚本仅替换双引号括起来的子字符串中的制表符 - Powershell script to replace only the tab characters in substrings enclosed in double quotes Powershell curl 双引号 - Powershell curl double quotes Powershell-如何用双引号将数组对象中的特定行括起来? - Powershell- How to wrap a specific line in an array object in double quotes? PowerShell 从命令行中去除双引号 arguments - PowerShell stripping double quotes from command line arguments powershell 就地删除文本文件中的双引号,其中行以双引号+一些其他文本开头 - powershell in-place remove double quotes in text file where line starts with double quotes + some other text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM