Powershell:如何将字符串中的第一个“:”替换为“,”?

时间:2016-06-20 03:02:34

标签: string powershell replace comma colon

例如,我有一个文本文件,指示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

怎么做?谢谢!

1 个答案:

答案 0 :(得分:1)

-replace运算符的语法不正确。仔细阅读操作员的帮助 - help about_Comparison_Operators。您在查找和替换字符串之间缺少,

PS C:\> "Hello World.ps1:John Smith" -replace ":",","
Hello World.ps1,John Smith