本文介绍了Powershell:如何替换第一个“:"字符串中是否为“,"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个文本文件指示powershell脚本的编写者,每行看起来像:

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

我希望在每一行中找到第一个:"并替换为,",以便获得一个csv文件,

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

我尝试使用"-replace",但失败了:

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!

推荐答案

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

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

这篇关于Powershell:如何替换第一个“:"字符串中是否为“,"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:11