用.bat中的PowerShell更改两行代码
我一直在关注这篇帖子并且我使用了这段代码在 .bat 中:
powershell -Command "(gc -Raw test.py) -replace 'test = ''lol''`r`nlol = ''test''', 'test1 = ''lol1''`r`nlol2 = ''test2''' | Out-File test2.py"
我的问题是,在创建的文件中,我有反引号 r 反引号 n 而不是换行符。正常吗?我错过了什么吗?
我还尝试使用 \r\n
而不是反引号 r 反引号 n ,同样的情况发生(它被识别,但在输出文件中有 \r\n
而不是实际的换行符(我使用的是 Windows 11)。
I've been following this post and I've used this code in the .bat:
powershell -Command "(gc -Raw test.py) -replace 'test = ''lol''`r`nlol = ''test''', 'test1 = ''lol1''`r`nlol2 = ''test2''' | Out-File test2.py"
It does recognise backtick r backtick n because if replaces this file:
My problem is that in the created file, I have backtick r backtick n instead of linebreaks. Is it normal? Am I missing something?
I also tried using \r\n
instead of backticks r backticks n and the same happens (it's recognised but in the output file there is \r\n
and not an actual linebreak (I'm on Windows 11).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要对逃生序列使用双重报价,请参见 about_special_characters 。
您要测试的一个示例:
至于您的调用
powershell.exe
从批处理文件中,我相信这可以使用(您可以使用“^”“
进行逃逸) :调用
cmd
和所需的逃逸的所有信用均转到此答案。You need to use double-quotes for escape sequences, see about_Special_Characters.
An example for you to test:
As for your call to
powershell.exe
from the batch file, I believe this could work (you can use"^""
for escaping):All credits on the call to
cmd
and the required escaping goes to this answer.