[英]PowerShell modify date column in csv
I have a series of csv statements from my bank that I would like to combine and pull out the relevant rows.我有一系列来自银行的 csv 报表,我想合并并提取相关行。 I have managed to get the rows out, but for the date column I would like to reformat the date from DD/MM/YYYY HH:MM to YYYY-MM-DD and I can't Google up the concept of how I should do this?我已经设法将行取出来,但是对于日期列,我想将日期从 DD/MM/YYYY HH:MM 重新格式化为 YYYY-MM-DD,但我无法谷歌搜索我应该怎么做的概念这个? I'm a PS noob.我是PS菜鸟。
Can anyone steer me in the right direction please?任何人都可以引导我朝着正确的方向前进吗?
Here's what I have so far:这是我到目前为止所拥有的:
$files = Get-ChildItem -Filter *.csv
$output = ForEach ( $file in $files ) {
Import-CSV $file | Where-Object{$_.Description -eq "Funding Bacs" -or $_.Description -eq "Lender Withdrawal Request" } | Select-Object Date,Description,"Paid In", "Paid Out"
# foreach of the date fields, take the date object? string? remove the time, and reformat the date to YYYY-MM-DD
}
$output | Export-Csv Newtest.csv -NoTypeInformation -encoding "unicode"
Current Output:电流输出:
Date Description Paid In Paid Out
---- ----------- ------- --------
03/04/2012 09:15 Funding Bacs 50.0 0.0
06/04/2013 17:32 Lender Withdrawal Request 0.0 234.5
01/04/2014 05:31 Funding Bacs 125.0 0.0
01/04/2014 05:31 Funding Bacs 10.0 0.0
Desired Output:期望输出:
Date Description Paid In Paid Out
---- ----------- ------- --------
2012-04-03 Funding Bacs 50.0 0.0
2013-04-06 Lender Withdrawal Request 0.0 234.5
2014-04-01 Funding Bacs 125.0 0.0
2014-04-01 Funding Bacs 10.0 0.0
Update: thanks to Martin I'm now getting the below... need to figure out why I'm getting the missed date values.更新:感谢 Martin,我现在得到了以下信息……需要弄清楚为什么我得到了错过的日期值。
Date Description Paid In Paid Out
---- ----------- ------- --------
2014-03-11 Funding Bacs 10.0 0.0
2014-03-11 Funding Bacs 125.0 0.0
Lender Withdrawal Request 0.0 521.05
2016-07-11 Lender Withdrawal Request 0.0 188.93
Lender Withdrawal Request 0.0 185.57
2012-03-10 Lender Withdrawal Request 0.0 148.72
2013-01-10 Funding Bacs 460.0 0.0
You could use a calculated property where you format the date.您可以使用计算属性来格式化日期。 Just change the Select-Object
to:只需将Select-Object
更改为:
Select-Object @{l="Date"; e={(Get-Date $_.Date).ToString('yyyy-MM-dd')}},Description,"Paid In", "Paid Out"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.