查找时间戳超过给定持续时间的相邻CSV行

问题描述 投票:-2回答:1

我有一个包含以下内容的文件:

"BatchID","BatchName","JobName","ScannerID","DateCreated","DailyCounter"
"12713","9001","fst100","09                  ","2/6/2020 6:21:06 AM","1"
"12714","9002","fst100","09                  ","2/6/2020 6:21:42 AM","2"
"12715","9003","fst100","09                  ","2/6/2020 6:21:58 AM","3"
"12716","9004","fst100","09                  ","2/6/2020 6:22:13 AM","4"
"12717","9005","fst100","09                  ","2/6/2020 6:22:30 AM","5"
"12718","9006","fst100","09                  ","2/6/2020 6:24:54 AM","6"
"12719","9007","fst100","09                  ","2/6/2020 6:25:12 AM","7"
"12720","9008","fst100","09                  ","2/6/2020 6:55:11 AM","8"
"12721","9009","fst100","09                  ","2/6/2020 6:55:27 AM","9"

我想比较创建日期的字段,并将相距15分钟或更长时间的行写入文件。

powershell date compare timespan
1个回答
0
投票

自:

  • 您的时间戳记字符串采用将其转换为[datetime]System.DateTime)时的原样格式(除非您的格式列出了day而不是月份的开头,

  • 您可以直接减去两个System.DateTime实例以获得一个[datetime]实例([timespan]),该实例的System.TimeSpan属性可以查询

解决方案非常简单:

System.TimeSpan

以上将匹配的行打印到屏幕上;删除.TotalMinutes以导出到(不同的)CSV文件。

注:照所写,您可以在输出中以duplicate行结尾,即,如果相邻行ABC中A和B以及B和C的间隔都超过15分钟,则输出ABBC。

文档链接:

  • $prevRow = $null Import-Csv in.csv | ForEach-Object { # Parse the CSV rows into objects and process each if ($prevRow -and ([datetime] $_.DateCreated - [datetime] $prevRow.DateCreated).TotalMinutes -gt 15) { # Output the previous row and the current row. $prevRow; $_ } $prevRow = $_ } # | Export-Csv -NoTypeInformation -Encoding Utf8 out.csv
  • #
  • Import-Csv
© www.soinside.com 2019 - 2024. All rights reserved.