[英]Powershell csv delete row
I'm trying to delete rows from csv base off certain conditions in two columns.我正在尝试根据两列中的某些条件从 csv 中删除行。 Status column if it has T and the second column is Term Date if the date is older then the past 5 days.状态列(如果它有 T),第二列是期限日期(如果日期早于过去 5 天)。
little info column STATUS has either "A" or "T".小信息列 STATUS 有“A”或“T”。
$path = "data.csv"
$maxAge = (Get-Date).AddDays(-5).Date
Import-Csv $path | Where-Object {$_.STATUS -eq "A" -or $_.TERMDATE -gt $maxAge} |
Export-Csv -Path temp.csv -NoTypeInformation
it works to a certain degree, but still shows dates from last year.它在一定程度上有效,但仍显示去年的日期。
You will need to cast the TERMDATE
value to a datetime
object in order to make an accurate comparison.您需要将TERMDATE
值转换为datetime
时间 object 以便进行准确的比较。
$path = "data.csv"
$maxAge = (Get-Date).AddDays(-5).Date
Import-Csv $path |
Where-Object {$_.STATUS -eq "A" -or [datetime]$_.TERMDATE -gt $maxAge} |
Export-Csv -Path temp.csv -NoTypeInformation
If the [datetime]
cast does not work, then you will need to parse the string and perform a conversion.如果[datetime]
转换不起作用,那么您将需要解析字符串并执行转换。 In order to provide a solution for that, we need to know the original date format of TERMDATE
.为了为此提供解决方案,我们需要知道TERMDATE
的原始日期格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.