如何使用 powershell 命令用换行符替换字符串?从批处理文件中? - 双引号问题
How can I make use a powershell command to replace a string witha new line? From within a batch file? -double quotes issue
所需的输出是将所有 5 个波浪号转换为换行符。
命令是
Powershell (Get-Content -Raw allactsconv.txt) "-replace '~~~~~', "[\r\n]" | Out-File -FilePath allactsconv.txt -Force -Encoding ascii"
它returns
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 命令,因为我需要学习它。
完全根据您表示要替换并希望用其替换的字符串,我提供以下选项:
Powershell -NoProfile "(Get-Content -Raw \".\allactsconv.txt\") -Replace \"~~~~~\", \"[`r`n]\" | Out-File -FilePath \".\allactsconv.txt\" -Force -Encoding ASCII"
如果您更喜欢在 PowerShell 中使用单引号,(我不喜欢),那么:
Powershell -NoProfile "(Get-Content -Raw '.\allactsconv.txt') -Replace '~~~~~', '[`r`n]' | Out-File -FilePath '.\allactsconv.txt' -Force -Encoding ASCII"
所需的输出是将所有 5 个波浪号转换为换行符。
命令是
Powershell (Get-Content -Raw allactsconv.txt) "-replace '~~~~~', "[\r\n]" | Out-File -FilePath allactsconv.txt -Force -Encoding ascii"
它returns
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 命令,因为我需要学习它。
完全根据您表示要替换并希望用其替换的字符串,我提供以下选项:
Powershell -NoProfile "(Get-Content -Raw \".\allactsconv.txt\") -Replace \"~~~~~\", \"[`r`n]\" | Out-File -FilePath \".\allactsconv.txt\" -Force -Encoding ASCII"
如果您更喜欢在 PowerShell 中使用单引号,(我不喜欢),那么:
Powershell -NoProfile "(Get-Content -Raw '.\allactsconv.txt') -Replace '~~~~~', '[`r`n]' | Out-File -FilePath '.\allactsconv.txt' -Force -Encoding ASCII"