如何使用 powershell 命令用新行替换字符串?从批处理文件中? -双引号问题

问题描述

所需的输出是将所有 5 个波浪号转换为换行符。

命令是

Powershell (Get-Content -Raw allactsconv.txt) "-replace '~~~~~',"[\r\n]" | Out-File -FilePath allactsconv.txt -Force -Encoding ascii"

它回来了

    At line:1 char:55
+ (Get-Content -Raw allactsconv.txt) -replace '~~~~~',[\r\n] | Out-Fil ...
+                                                       ~
Missing type name after '['.
At line:1 char:53
+ (Get-Content -Raw allactsconv.txt) -replace '~~~~~',[\r\n] | Out-Fil ...
+                                                     ~
Missing expression after ','.
At line:1 char:54
+ (Get-Content -Raw allactsconv.txt) -replace '~~~~~',[\r\n] | Out-Fil ...
+                                                      ~~~~~~
Unexpected token '[\r\n]' in expression or statement.
    + CategoryInfo          : ParserError: (:) [],ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingTypename

我认为问题可能出在方括号上,但没有它们,错误完全相同。

更好的解决方案是将整个批处理文件(它做了很多其他的事情)重写为一个 powershell 脚本,但不幸的是我现在需要解决这个问题。在某个时候,我会这样做。

看起来问题是双引号内的双引号,但我该如何解决?如果可以,我想使用 powershell 命令,因为我需要学习它。

解决方法

纯粹基于您表示要替换并希望替换的字符串,我将提供以下选项:

{json.data && <h1>{json.data.email}</h1>}

如果您更喜欢在 PowerShell 中使用单引号,(不是我的偏好),那么:

Powershell -NoProfile "(Get-Content -Raw \".\allactsconv.txt\") -Replace \"~~~~~\",\"[`r`n]\" | Out-File -FilePath \".\allactsconv.txt\" -Force -Encoding ASCII"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...