2015-05-22 64 views
1

想抽取行項目,如果日期範圍在25-mar-2015 to 05-may-2015之間,則從第二個字段($2)開始。 日期列未排序,每個文件包含數百萬條記錄。awk根據日期範圍提取行數:

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訪問。

+2

表達'GETDATE(25-MAR-2015)'提供-1990到'getDate',因爲你沒有一個變量'mar'所以它的計算結果爲0。括在引號:'GETDATE( 「2015年3月25日」),你可能會有機會。 –

回答

5
$ 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 
+0

非常感謝,我已經接受了答案,並投票贊成! – VNA