gpt4 book ai didi

powershell - 从文件计算时间

转载 作者:行者123 更新时间:2023-12-03 01:09:49 25 4
gpt4 key购买 nike

基于my previous question(感谢@MartinBrandl),我也想获取时间值来计算时间。

示例数据:

07:30 - 07:45PMTS09526052 | Sev9 | Location| | Due: 12/23/2016
NON PC HARDWARE - TROUBLESHOOT SW
Complete this Job

马丁·布兰德尔(Martin Brandl)提出了这个方案,以获得我需要的其他数据:
Select-String $WLDir -pattern '(PMT[S|T]\d{8})' -Context 0,2 | ForEach-Object {
[PSCustomObject]@{
Time = $_.Matches.Groups[1].Value
Topic = $_.Context.PostContext[0]
Status = $_.Context.PostContext[1]
}
} | ConvertTo-Csv -NoTypeInformation

我想从 07:30 - 07:45部分的前面抓取 PMT位。我打算用这个来获取时间:
$StartTime = [However I get the `7:30` here]
$EndTime = [However I get the `7:45` here]
$ElapsedTime = (NEW-TIMESPAN –Start $StartTime –End $EndTime).TotalHours

然后将第四个字段添加到CSV:
Elapsed = $ElapsedTime

但是我不确定如何抓紧两次并将它们隔离为变量。有人可以协助吗?

最佳答案

在您的第一个问题中,时间跨度不在Csv中
正则表达式没有引用它。现在是7:30是$ matches.groups [1] .value等

$WLDir = ".\File.txt" 
$Pattern='(\d{2}:\d{2}) - (\d{2}:\d{2})(PMT[S|T]\d{8})'
Select-String $WLDir -pattern $Pattern -Context 0,2 |
ForEach-Object {
$StartTime=[datetime]::ParseExact($_.Matches.Groups[1].Value,"HH:mm",$null)
$EndTime=[datetime]::ParseExact($_.Matches.Groups[2].Value,"HH:mm",$null)
$ElapsedTime = (NEW-TIMESPAN –Start $StartTime –End $EndTime).TotalHours
[PSCustomObject]@{
Time = $_.Matches.Groups[3].Value
Topic = $_.Context.PostContext[0]
Status = $_.Context.PostContext[1]
ElapsedHrs = $ElapsedTime
}
} | ConvertTo-Csv -NoTypeInformation

提供此输出(我在PMTT中添加了第二个测试项)
"Time","Topic","Status","ElapsedHrs"
"PMTS09526052","NON PC HARDWARE - TROUBLESHOOT SW","Complete this Job","0,25"
"PMTT10952605","NON PC HARDWARE - TROUBLESHOOT SW","Complete this Job","0,25"

时间列仍然不包含时间
(我的语言环境有一个十进制逗号)

关于powershell - 从文件计算时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com