简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM