繁体   English   中英

awk根据日期范围提取行:

[英]awk to extract lines based on Date range:

如果日期范围是从第二个字段($2) 25-mar-2015 to 05-may-2015日之间,则希望提取订单项。 日期列未排序,每个文件包含数百万条记录。

Inputs.gz

Des,DateInfo,Amt,Loc,Des2
abc,02-dec-2014,10,def,xyz
abc,20-apr-2015,25,def,xyz
abc,14-apr-2015,40,def,xyz
abc,17-mar-2014,55,def,xyz
abc,24-nov-2011,70,def,xyz
abc,13-may-2015,85,def,xyz
abc,30-sep-2008,100,def,xyz
abc,20-jan-2014,115,def,xyz
abc,04-may-2015,130,def,xyz
abc,25-nov-2013,145,def,xyz
abc,29-mar-2015,55,def,xyz

我已经尝试过以下命令,但操作不完整:

function getDate(date) {
    split(date, a, "-");
    return mktime(a[3] " " sprintf("%02i",(index("janfebmaraprmayjunjulaugsepoctnovdec", a[2])+2)/3) " " a[1] " 00 00 00")
}

BEGIN {FS=","}

{ if ( getDate($2)>=getDate(25-mar-2015) && getDate($2)<=getDate(05-may-2015) ) print $0 }

预期产量:

abc,20-apr-2015,25,def,xyz
abc,14-apr-2015,40,def,xyz
abc,04-may-2015,130,def,xyz
abc,29-mar-2015,55,def,xyz

请建议...我没有perl和python访问权限。

$ cat tst.awk
function getDate(date,  a) {
    split(date, a, /-/)
    return mktime(a[3]" "(index("janfebmaraprmayjunjulaugsepoctnovdec",a[2])+2)/3" "a[1]" 0 0 0")
}

BEGIN {
    FS=","
    beg = getDate("25-mar-2015")
    end = getDate("05-may-2015")
}

{ cur = getDate($2) }

NR>1 && cur>=beg && cur<=end

$ awk -f tst.awk  file
abc,20-apr-2015,25,def,xyz
abc,14-apr-2015,40,def,xyz
abc,04-may-2015,130,def,xyz
abc,29-mar-2015,55,def,xyz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM