Powershell: how to replace first “:” in a string to be “,”?

不问归期 提交于 2019-12-12 03:38:19

问题


For example, I've got a text file indicating writers of powershell scripts, each line looks like:

Hello world.ps1:John Smith
Dowork.ps1:Blake Benn

I wish to find first ":" in each line and replace with "," so I can get a csv file,

Hello world.ps1,John Smith
Dowork.ps1,Blake Benn

I tried with "-replace", but failed:

"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

How to do this? Thanks!


回答1:


Your syntax for the -replace operator is not correct. Read the help for the operator carefully - help about_Comparison_Operators. You are missing a , between the find and replace strings.

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


来源:https://stackoverflow.com/questions/37914083/powershell-how-to-replace-first-in-a-string-to-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!