【问题标题】:How can i delete string in variable powershell?如何删除变量powershell中的字符串?
【发布时间】:2021-07-09 08:01:10
【问题描述】:

我有一个这样的日志文件:

[2021/04/13 18:21:57.577+02:00][VERBOSE] Finished: 0 file(s), 5.23 GB; Average Speed:17.26 MB/s.

我只想删除“,”和“/s”之间的所有字符串。我试了很多次都做不好。

有人可以帮我在 Powershell 上执行此操作吗?

【问题讨论】:

  • 有点不清楚.. 你的意思是foreach ($line in (Get-Content -Path 'TheFile.log')) { ($line -split ',')[0] } ??如果没有,请添加所需输出的示例。
  • 感谢您的回答,恰恰相反,所需的输出例如:5.23 GB;平均速度:17.26 MB/s。谢谢我把 1 放在 [0] 上,这就是我想要的。另一个问题我可以使用 5,23 GB 这样的大小并做一些数学运算来获得 % 吗?
  • 在这种情况下foreach ($line in (Get-Content -Path 'TheFile.log')) { ($line -split ',')[-1] }[-1] 指的是数组中的最后一项
  • 谢谢西奥!另一个问题我可以使用 5,23 GB 这样的大小并做一些数学运算来获得 % 吗?
  • 文件是否包含示例行中的小数 point 或 cmets 中的小数 comma

标签: string powershell


【解决方案1】:

如果您不仅需要从日志行中获取有趣的部分,还需要能够对示例中的数字(“5.23 GB”)进行数学运算,则需要进行更多拆分:

foreach ($line in (Get-Content -Path 'TheFile.log')) { 
    $interestingPart = ($line -split ',')[-1].Trim()
    $logSize, $logAverage = $interestingPart -split ';'
    $size, $unit = $logSize -split '\s+'
    # calculate the size from both the number ('5.23') and the unit ('GB')
    $size = [double]::Parse($size) * "1$unit"

    # now you have the number to do further math on
}

【讨论】:

  • 非常感谢!正是我需要的。祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 2012-03-01
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
相关资源
最近更新 更多