从长字符串中提取日期和时间

时间:2018-12-20 23:16:09

标签: powershell

我收到了一份格式很糟糕的报告,应该是CSV,但实际上不是,因此我必须将其导入并进行转换。我已经对其进行了转换,但是我有可用的数据,但是在最后一列上,我有太多无用的数据,并且想要从字符串的开头和结尾提取日期和时间。

我尝试了几种不同的方法,但似乎无法获得想要的东西。

这是我正在运行的代码:

$csvPath = 'C:\Powershell\Test5.csv'
$csvData = Get-Content -Path $csvPath |
           Select-Object -Skip 1 |
           Out-String |
           ConvertFrom-Csv
$AuditTrail = $csvData | Select-Object "Approval History"

$AuditTrail | foreach {
    $entry = $_ -split 'Approved'
    $entry[-2]
}

输出为:

12/11/2018 10:20 AM  Submitted for approval by Joseph Star  12/11/2018 12:00 PM  
12/11/2018 05:16 PM  Submitted for approval by Joseph Star  12/12/2018 07:46 AM  
12/13/2018 09:41 AM  Submitted for approval by Joseph Star  12/13/2018 10:28 AM  
12/13/2018 05:16 PM  Submitted for approval by Joseph Star  12/14/2018 07:49 AM  
12/14/2018 05:22 PM  Submitted for approval by Joseph Star  12/18/2018 10:14 AM  
12/11/2018 10:06 AM  Submitted for approval by Jimmy Newton  12/14/2018 11:36 AM  
12/11/2018 06:33 PM  Submitted for approval by Jimmy Newton  12/14/2018 11:37 AM  
12/17/2018 10:28 AM  Submitted for approval by Jimmy Newton  12/18/2018 04:33 PM  
12/13/2018 03:31 PM  Submitted for approval by Jimmy Newton  12/14/2018 11:37 AM  
12/17/2018 10:33 AM  Submitted for approval by Jimmy Newton  12/18/2018 04:33 PM  
12/10/2018 05:59 PM  Submitted for approval by Stan Henry  12/11/2018 09:10 AM  
12/12/2018 10:59 AM  Submitted for approval by Stan Henry  12/14/2018 02:35 PM  
12/12/2018 05:01 PM  Submitted for approval by Stan Henry  12/14/2018 02:15 PM  
12/14/2018 10:12 AM  Submitted for approval by Stan Henry  12/14/2018 02:27 PM  
12/16/2018 11:24 PM  Submitted for approval by Stan Henry  12/19/2018 01:12 PM  
12/16/2018 11:25 PM  Submitted for approval by Stan Henry  12/18/2018 11:12 AM  

从该输出中,我想获取前19个字符(完整的日期和时间)并将其存储到变量中。然后,我想取最后19个字符(完整的日期和时间)并将其存储到另一个变量中。

3 个答案:

答案 0 :(得分:5)

最简洁的解决方案是将每一行用分隔两个空格,因为它们Ansgar Wiechers指出,将日期的开头和结尾都分开了:

$line = '12/11/2018 10:20 AM  Submitted for approval by Joseph Star  12/11/2018 12:00 PM'
$date1, $date2 = [datetime[]] ($line -split '  ')[0,-1]

对于类似的基于 regex 的简洁解决方案,其中基于 pattern matching 提取日期,请参见seedie's answer,可以将其简化如下:< / p>

$line = '12/11/2018 10:20 AM  Submitted for approval by Joseph Star  12/11/2018 12:00 PM'
[regex] $regex = '\b\d{2}/\d{2}/\d{4} \d{2}:\d{2} (?:AM|PM)\b'
$date1, $date2 = [datetime[]] $regex.Matches($line).Value

答案 1 :(得分:1)

如果您不能依赖将日期分隔的两个空格,则可以使用正则表达式

$line = '12/11/2018 10:20 AM  Submitted for approval by Joseph Star  12/11/2018 12:00 PM'
[regex]$regex = '((0?[1-9]|1[012])[\/](0?[1-9]|[12][0-9]|3[01])[\/\-]\d{4} [01][0-9][\:][0-5][0-9] [AP]M)'
$date1, $date2 = [datetime[]] $regex.Matches($line).Value

基于mklement0的答案

答案 2 :(得分:0)

有几种方法可以实现,这是我能想到的最简单的方法,但是有些“快速而肮脏的”方法是:

fileprivate

本质上,只需$string = "12/11/2018 10:20 AM Submitted for approval by Joseph Star 12/11/2018 12:00 PM " [datetime]$date1 = ($string -split " ")[0..2] -join " " [datetime]$date2 = ($string -split " ")[-4..-1] -join " " 使用空格-split进入数组,从该数组中获取前三项(" "),然后[0..2]将它们一起返回在强制转换为-join之前的字符串。同样,对于第二个日期,但要从数组中获取最后3个项目([datetime])。

如果需要,您可以将其压缩为1行:

[-4..-1]

哪个输出:

[datetime]$date1,[datetime]$date2 = (($string -split " ")[0..2] -join " "),(($string -split " ")[-4..-1] -join " ")

如果字符串发生变化,或者日期不总是连续的前3组或后3组连续字符,则此方法将无效。