使用awk填写缺少的日期 [英] Fill the missing dates using awk

查看:101
本文介绍了使用awk填写缺少的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件中缺少一些日期。例如

I have some missing dates in a file. e.g.

$ cat ifile.txt

$cat ifile.txt

20060805
20060807
20060808
20060809
20060810
20060813
20060815
20060829
20060901
20060903
20060904
20060905
20070712
20070713
20070716
20070717

日期的格式为YYYYMMDD。我的意图是填写日期之间的缺失日期(如果它们缺少5天的最大值),例如

The dates are in the format YYYYMMDD. My intention is fill the missing dates in between the dates if they are missing maximum for 5 day e.g.

20060805
20060806   ---- This was missed
20060807
20060808
20060809
20060810
20060811  ----- This was missed
20060812  ----- This was missed
20060813
20060814  ----- This was missed
20060815  
20060829
20060830 ------ This was missed
20060831 ------ This was missed
20060901  
20060902 ------ This was missed
20060903
20060904
20060905
20070712
20070713
20070714 ----- This was missed
20070715 ----- This was missed
20070716
20070717

如果间隔超过5天,则不需要其他日期。例如,我不需要填写20060815和20060829之间的日期,因为它们之间的间隔超过5天。

Other dates are not needed where there is a gap of more than 5 days. For example, I don't need to fill the dates between 20060815 and 20060829, because the gap between them is more than 5 days.

我以以下方式进行操作,但是不要

I am doing it in following ways, but don't get anything.

#!/bin/sh
awk BEGIN'{
          a[NR]=$1
          } {
          for(i=1; i<NR; i++)
          if ((a[NR+1]-a[NR]) <= 5)
             for (j=1; j<(a[NR+1]-a[NR]); j++)
             print a[j]
          }' ifile.txt

所需的输出:

20060805
20060806 
20060807
20060808
20060809
20060810
20060811 
20060812 
20060813
20060814 
20060815  
20060829
20060830 
20060831 
20060901  
20060902 
20060903
20060904
20060905
20070712
20070713
20070714 
20070715 
20070716
20070717


推荐答案

尝试跟随,写en并使用GNU awk 中显示的示例进行测试。

Could you please try following, written and tested with shown samples in GNU awk.

awk '
FNR==1{
  print
  prev=mktime(substr($0,1,4)" "substr($0,5,2)" "substr($0,7,2) " 00 00 00")
  next
}
{
  found=i=diff=""
  curr_time=mktime(substr($0,1,4)" "substr($0,5,2)" "substr($0,7,2) " 00 00 00")
  diff=(curr_time-prev)/86400
  if(diff>1){
    while(++i<=diff){ print strftime("%Y%m%d", prev+86400*i) }
    found=1
  }
  prev=mktime(substr($0,1,4)" "substr($0,5,2)" "substr($0,7,2) " 00 00 00")
}
!found
'  Input_file

这篇关于使用awk填写缺少的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆