Powershell:如何将字符串中的第一个“:”替换为“,”?
Powershell: how to replace first ":" in a string to be ","?
例如,我有一个指示 powershell 脚本编写者的文本文件,每一行如下所示:
Hello world.ps1:John Smith
Dowork.ps1:Blake Benn
我希望在每行中找到第一个“:”并用“,”替换,这样我就可以获得一个 csv 文件,
Hello world.ps1,John Smith
Dowork.ps1,Blake Benn
我尝试使用“-replace”,但失败了:
"Hello World.ps1:John Smith" -replace ":" ","
At line:1 char:43
+ "Hello World.ps1:John Smith" -replace ":" ","
+ ~~~
Unexpected token '","' in expression or statement.
+ CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
如何做到这一点?谢谢!
您的 -replace
运算符语法不正确。仔细阅读操作员帮助 - help about_Comparison_Operators
。您在查找和替换字符串之间缺少 ,
。
PS C:\> "Hello World.ps1:John Smith" -replace ":",","
Hello World.ps1,John Smith
例如,我有一个指示 powershell 脚本编写者的文本文件,每一行如下所示:
Hello world.ps1:John Smith
Dowork.ps1:Blake Benn
我希望在每行中找到第一个“:”并用“,”替换,这样我就可以获得一个 csv 文件,
Hello world.ps1,John Smith
Dowork.ps1,Blake Benn
我尝试使用“-replace”,但失败了:
"Hello World.ps1:John Smith" -replace ":" ","
At line:1 char:43
+ "Hello World.ps1:John Smith" -replace ":" ","
+ ~~~
Unexpected token '","' in expression or statement.
+ CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
如何做到这一点?谢谢!
您的 -replace
运算符语法不正确。仔细阅读操作员帮助 - help about_Comparison_Operators
。您在查找和替换字符串之间缺少 ,
。
PS C:\> "Hello World.ps1:John Smith" -replace ":",","
Hello World.ps1,John Smith