需要代码替换文本文件中所有以Characters KB结尾的单词

时间:2019-03-25 09:37:00

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

我有一个文本文件,其中包含某些字符串,例如12344KB,1231232KB等。 我必须使用powershell将这些以KB结尾的单词更改为10KB。 我刚刚开始学习powershell,如果有人可以帮助我提供脚本,那将非常有帮助。

我尝试了以下代码。

((Get-Content -path C:\test.txt) -replace '*KB','10KB') | Set-Content -Path C:\test.txt

实际:值分别为12344KB,1231232KB

预期:值分别为10KB,10KB

1 个答案:

答案 0 :(得分:1)

要发布我的评论作为答案(因此问题不再是“未回答”)

尝试以下正则表达式:

((Get-Content -path C:\test.txt) -replace '\b\d+KB\b', '10KB') | Set-Content -Path C:\test.txt