我有一些CSV文件,我需要删除所有包含日期大于指定日期的行。我将如何在PowerShell中执行此操作?
顺便说一句:这是日期格式:09/29/2011
示例:我想删除所有包含日期大于09/29/2011的行。
答案 0 :(得分:2)
foreach ($file in gci *.csv){
(gc $file) |
? {[datetime]$_.split('|')[1] -lt '09/29/2011'
} | set-content $file
}
假设这是一个以管道分隔的文件。
答案 1 :(得分:1)
好吧,似乎只有一件事看起来像那条线上的日期,所以我们可以只为此过滤:
Get-ChildItem *.csv | # adapt if necessary
ForEach-Object {
(Get-Content $_) | # the parentheses are important so the entire file is read at once
Where-Object { # now we process the file line by line
# find the date ↓ suppress the boolean output
$_ -match '\|(\d{2}/\d{2}/\d{4})\|' | Out-Null
# this only works if every line contains a date. Hopefully it does.
$date = [DateTime]($Matches[1])
# Finally the comparison we wanted in the first place
# This is the condition for all lines that are *retained* (hence less than)
$date -lt '09/29/2011'
} | Out-File $_ # use -Encoding ASCII/UTF8/Unicode depending on your needs.
# Maybe ASCII is enough
}
或更短:
gci *.csv | % {
(gc $_) |
? {
$null = $_ -match '\|(\d{2}/\d{2}/\d{4})\|'
[DateTime]$Matches[1] -lt '09/29/2011'
} |
Out-File $_
}
答案 2 :(得分:1)
我赞成清晰而不简洁:
param (
[parameter(Mandatory = $true)] [string] $csvFileName,
[parameter(Mandatory = $true)] [datetime] $date
)
try
{
$Error.Clear()
if (!(Test-Path $csvFileName))
{ throw "Could not find file $csvFileName" }
$newContent = Get-Content $csvFileName | ?{
([regex]::matches($_, "[0-9]{2}/[0-9]{2}/[0-9]{4}") | %{[DateTime] $_.value -lt $date})
}
$newContent | Set-Content $csvFileName
}
catch
{
Write-Host "$($MyInvocation.InvocationName): $_"
}
答案 3 :(得分:0)
您需要创建一个新的已清理的csv文件:
假设这是你csv:
col1,date,col3
aaaaa,05/05/2010,rwer
bdfdfg,06/29/2011,reewr
dsfsdf,08/05/2012,dsfsd
这样做:
import-csv .\myoriginal.csv -delimiter '|' | ? { [datetime]$_.date -ge [datetime]"09/29/2011"} | Export-Csv -NoTypeInformation -Path .\mycleaned.csv -delimiter '|'
然后你可以用
删除原始csvremove-item .\myoriginal.csv
答案 4 :(得分:-1)
我早上写了一个脚本,删除每行都有你指定的模式。 您应该运行脚本,例如:
myscruipt.sh YOURDATYE YOURCSVFILE
myscript.sh:
#!/bin/bash
declare -a num
num=`egrep -n "$1" yahoo_ab.csv |awk 'BEGIN{FS=":";}{for (i=0 ; i<NF ; i++) print $1; } '`
while true; do
for i in $num ; do
sed -i "$i d" $2 ;
done;
egrep $1 $2;
if [ $? = 1 ]; then break; fi;
done;