【问题标题】:Need code to replace all words in a text file which ends with Characters KB需要代码来替换以字符 KB 结尾的文本文件中的所有单词
【发布时间】:2019-07-03 17:38:26
【问题描述】:

我有一个文本文件,其中包含某些字符串,例如 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

【问题讨论】:

  • 试试-replace '\b\d+KB\b', '10KB'
  • @Theo 可能会将其作为答案提交
  • @MathiasR.Jessen 你是对的。 cmets中回答的问题太多

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


【解决方案1】:

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

试试下面的正则表达式:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 2015-12-07
    • 1970-01-01
    • 2014-06-29
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多