根据天数计算老化桶

时间:2014-12-05 12:00:57

标签: unix awk

想生成报告,其中计算天数,物料在仓库中。 天数是材料进入的日期($3 field)之间的差异 生成报告的日期(05-NOV-2014)以查看广告资源。 现在,我该如何解决这个问题。而且,一旦我得到天数, 这些必须归类为

CAT-1 for the days 0-30Days,CAT-2 for days 31-60Days,CAT-3 for days 61-90Days,CAT-4 for days 91-180Days,
CAT-5 for days 181-365Days,CAT-6 for days 365-730Days,CAT-7 for days Greaterthan>730Days 

反对Product($4) and Material($5)明智的组合。

Input.csv

Des11,Des22,DateIn,Product,Material,Loc11,Loc22

10,a,15-SEP-14.11:09:06,ab,pq,yyy,zzz
20,a,23-SEP-14.08:09:35,ab,pq,yyy,zzz
10,a,15-MAY-14.08:09:26,ab,pq,yyy,zzz
62,a,17-FEB-12.03:09:23,ab,pq,yyy,zzz
10,a,04-NOV-14.11:09:06,cd,rs,yyy,zzz
20,a,12-AUG-14.08:09:35,cd,rs,yyy,zzz
10,a,16-DEC-13.08:09:26,cd,rs,yyy,zzz
62,a,18-DEC-13.03:09:23,cd,rs,yyy,zzz

例如,我手动调整Aging桶如下: 报告生成日期是手动输入,因此我可以使用任何格式,如05-Nov-14或05-NOV-2014或20141105 但是,我没有权利改变输入的输入,即15-SEP-14.11:09:06

DateofReportGen,DateIn($3),DateIn,NoOfDays,Ageing Bucket
05-Nov-14,15-SEP-14.11:09:06,15-Sep-14,51,31-60Days
05-Nov-14,23-SEP-14.08:09:35,23-Sep-14,43,31-60Days
05-Nov-14,15-MAY-14.08:09:26,15-May-14,174,91-180Days
05-Nov-14,17-FEB-12.03:09:23,17-Feb-12,992,Greaterthan>730Days
05-Nov-14,04-NOV-14.11:09:06,04-Nov-14,1,0-30Days
05-Nov-14,12-AUG-14.08:09:35,12-Aug-14,85,61-90Days
05-Nov-14,16-DEC-13.08:09:26,16-Dec-13,324,181-365Days
05-Nov-14,18-DEC-13.03:09:23,18-Dec-13,322,181-365Days 

我正在努力寻找" No of Days"但是被击中了

awk -F, -v q='"' '{
 c="date -d"q$3q" +%s";c|getline d1;close(c); 
 c="date -d"q05-NOV-14q" +%s";c|getline d2;close(c);
 printf "%s,%d\n", $0,(d2-d1)/3600/24}' Input.csv

期望的输出:

基于老化桶的产品和材料计数:

Product,Material,0-30Days,31-60Days,61-90Days,91-180Days,181-365Days,365-730Days,Greaterthan>730Days
ab,pq,0,2,0,1,0,0,1
cd,rs,1,0,1,0,2,0,0

我正在寻找你的建议,没有Perl / Python访问!!!

编辑#1:感谢gboffi的输入,在执行下面的脚本时,我得到了#34; elapsed_days"所有订单项均为16410.3 ...请建议

awk -F, 'NR==1 {print}
         NR>1  {date=$3;
                gsub("[-.]"," ",date);
                printf $0 ",";system("date --date=\"" date "\" +%s")}
'  $filename | awk -F, -v OFS=, -v now=`date +%s` '
         NR==1 {print $0 ",elapsed_days"}
         NR>1  {$8=now-$8; $8=$8/24/3600;print $0}' Input.csv

输出 - 编辑#1:

Des11,Des22,DateIn,Product,Material,Loc11,Loc22,elapsed_days
10,a,15-SEP-14.11:09:06,ab,pq,yyy,zzz,16410.3
20,a,23-SEP-14.08:09:35,ab,pq,yyy,zzz,16410.3
10,a,15-MAY-14.08:09:26,ab,pq,yyy,zzz,16410.3
62,a,17-FEB-12.03:09:23,ab,pq,yyy,zzz,16410.3
10,a,04-NOV-14.11:09:06,cd,rs,yyy,zzz,16410.3
20,a,12-AUG-14.08:09:35,cd,rs,yyy,zzz,16410.3
10,a,16-DEC-13.08:09:26,cd,rs,yyy,zzz,16410.3
62,a,18-DEC-13.03:09:23,cd,rs,yyy,zzz,16410.3

1 个答案:

答案 0 :(得分:1)

我有两个通过的解决方案

awk -F, 'NR==1 {print}
         NR>1  {date=$3;
                gsub("[-.]"," ",date);
                printf $0 ",";system("date --date=\"" date "\" +%s")}
'  $filename | awk -F, -v OFS=, -v now=`date +%s` '
         NR==1 {print $0 ",elapsed_days"}
         NR>1  {$8=now-$8; $8=$8/24/3600;print $0}'

当然,如果您有$8天数,则可以根据需要格式化最终输出。

回应OP问题

从我的终端复制并粘贴以下内容

  1. 我向您展示我的数据版本,从原始帖子中复制并粘贴,

    % cat dates
    Des11,Des22,DateIn,Product,Material,Loc11,Loc22
    10,a,15-SEP-14.11:09:06,ab,pq,yyy,zzz
    20,a,23-SEP-14.08:09:35,ab,pq,yyy,zzz
    10,a,15-MAY-14.08:09:26,ab,pq,yyy,zzz
    62,a,17-FEB-12.03:09:23,ab,pq,yyy,zzz
    10,a,04-NOV-14.11:09:06,cd,rs,yyy,zzz
    20,a,12-AUG-14.08:09:35,cd,rs,yyy,zzz
    10,a,16-DEC-13.08:09:26,cd,rs,yyy,zzz
    62,a,18-DEC-13.03:09:23,cd,rs,yyy,zzz
    
  2. 我只执行2阶段管道的第一部分

    % awk -F, 'NR==1 {print}
               NR>1  {date=$3;
                      gsub("[-.]"," ",date);
                      printf $0 ",";system("date --date=\"" date "\" +%s")}
    ' dates 
    Des11,Des22,DateIn,Product,Material,Loc11,Loc22
    10,a,15-SEP-14.11:09:06,ab,pq,yyy,zzz,1410772146
    20,a,23-SEP-14.08:09:35,ab,pq,yyy,zzz,1411452575
    10,a,15-MAY-14.08:09:26,ab,pq,yyy,zzz,1400134166
    62,a,17-FEB-12.03:09:23,ab,pq,yyy,zzz,1329444563
    10,a,04-NOV-14.11:09:06,cd,rs,yyy,zzz,1415095746
    20,a,12-AUG-14.08:09:35,cd,rs,yyy,zzz,1407823775
    10,a,16-DEC-13.08:09:26,cd,rs,yyy,zzz,1387177766
    62,a,18-DEC-13.03:09:23,cd,rs,yyy,zzz,1387332563
    

    请注意在最后一个字段上方的输出中,其中包含系统date命令的输出

  3. 我最终执行整个管道,每行的经过天数不同 你的手术出了什么问题?查看日期命令的输出,例如date --date="15 SEP 14 11:09:06" +%s,如果这不是问题,还有什么?

    % awk -F, 'NR==1 {print}
               NR>1  {date=$3;
                      gsub("[-.]"," ",date);
                      printf $0 ",";system("date --date=\"" date "\" +%s")}
    ' dates | awk -F, -v OFS=, -v now=`date +%s` '
              NR==1 {print $0 ",elapsed_days"}
              NR>1  {$8=now-$8; $8=$8/24/3600;print $0}'
    Des11,Des22,DateIn,Product,Material,Loc11,Loc22,elapsed_days
    10,a,15-SEP-14.11:09:06,ab,pq,yyy,zzz,81.9347
    20,a,23-SEP-14.08:09:35,ab,pq,yyy,zzz,74.0593
    10,a,15-MAY-14.08:09:26,ab,pq,yyy,zzz,205.059
    62,a,17-FEB-12.03:09:23,ab,pq,yyy,zzz,1023.23
    10,a,04-NOV-14.11:09:06,cd,rs,yyy,zzz,31.893
    20,a,12-AUG-14.08:09:35,cd,rs,yyy,zzz,116.059
    10,a,16-DEC-13.08:09:26,cd,rs,yyy,zzz,355.018
    62,a,18-DEC-13.03:09:23,cd,rs,yyy,zzz,353.226
    %