我有几天的handle.exe
运行输出的众多.txt文件。我需要重新组织数据以将其存入关系数据库。我需要做的第一件事就是重新格式化日期。在.txt文件中重新格式化多个日期(每个不同)
每个文件有超过800个日期,分布在整个文件不均匀。日期格式:
June 29, 2016 12:05:45 PM
和我需要06-29-16 12:05:45
。
我现在只是在处理单个文件,以便拨打电话。我试图用Get-Date
原位替换日期(使用原始日期的数组),并且无处可用。然后我尝试-replace
,那没有奏效。
我已经花了3到4天的时间,我想我已经伤了脑筋。我已经尝试了很多东西排列,我甚至不知道我现在在哪里。
我试过的最后一件事是在下面。尝试使用散列表,在表中使用旧日期和新日期。
##To set "|" as separator for arrays
$OFS = '|'
##To get original dates into array
$a = @(sls .\hp.txt -pattern '(june 29|june 30|july 1|july 2|july 3|july 4)' | select -ExpandProperty line)
##To get dates with corrected format into array
$b = @($a | foreach {$_ | Get-Date -Format "MM-dd-yy hh:mm:ss"})
##To get old and new dates into hash table
$dates = @{$a = $b}
##To bring in content from file
$file = (Get-Content C:\hp.txt)
##To replace "NAME" with "VALUE" from hash table into file
foreach ($d in $dates) {
$file = $file -replace $d.Name, $d.Value
}
##To save corrected file with new file name
Set-Content -Path C:\hpnew.txt -Value $file
的$a
数组包含(小部分):
June 29, 2016 12:04:51 PM June 29, 2016 12:05:58 PM June 29, 2016 12:07:00 PM [NOTE: LOTS MORE DATES HERE] June 30, 2016 12:01:17 AM June 30, 2016 12:02:19 AM June 30, 2016 12:04:22 AM [NOTE:CONTINUING TO END]
的$b
数组包含:
06-29-16 12:04:51 06-29-16 12:05:58 06-29-16 12:07:00 [NOTE: LOTS MORE DATES ] 06-30-16 12:01:17 06-30-16 12:02:19 06-30-16 12:04:22 [NOTE: CONTINUING TO END]
有可能是一个更简单,更优雅的解决方案。但任何帮助/方向都会很棒。
,如果你能安装PS v5你可以尝试 - Convert-FromString http://www.powershellmagazine.com/2014/09/09/using-the-convertfrom-string-cmdlet-to-parse-structured-text/ – Kiran
Kiran ...谢谢为小费。但在4.0中真的没有办法做到这一点?散列表是否不起作用?看起来好像我接近了,但我无法做出最后的调整来让它发挥我寻求的结果。 – Charlie