问题
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