如何生成给予启动和使用bash脚本AWK结束日期的日期序列? [英] How to generate a sequence of dates given starting and ending dates using AWK of BASH scripts?

查看:157
本文介绍了如何生成给予启动和使用bash脚本AWK结束日期的日期序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的数据集

第一场和第二场开始表示和研究的结束日期(M / D / YYYY)。

人们如何扩展数据转换成所需的输出格式,考虑到闰年用AWK或bash脚本?

您的帮助是非常AP preciated。

输入

  2009年7月2日2009/7/7
  1996年2月28日1996年3月3日
  二零零一年十二月三十〇日2002年1月4日

所需的输出

  2009/7/7
  2009年7月6日
  2009年7月5日
  2009年7月4日
  2009年7月3日
  2009年7月2日
  1996年3月3日
  1996年3月2日
  1996年3月1日
  1996年2月29日
  1996年2月28日
  2002年1月4日
  2002年1月3日
  2002年1月2日
  1/1/2002
  2001年12月31日
  二〇〇一年十二月三十零日


解决方案

如果您有 GAWK

 #!的/ usr / bin中/ GAWK -f
{
    拆分($ 1,S,/)
    斯普利特($ 2,即,/)
    ST = mktime(S [3],S [1],S [2]为0 0 0)
    等= mktime(E [3],E [1],E [2]0 0 0)
    对于(i =等; I> = ST; I- = 60 * 60 * 24)打印的strftime(%M /%D /%Y,I)
}

示范:

  ./ daterange.awk inputfile中

输出:

  07/07/2009
2009年7月6日
07/05/2009
07/04/2009
07/03/2009
07/02/2009
1996年3月3日
1996年3月2日
1996年3月1日
1996年2月29日
1996年2月28日
01/04/2002
2002年1月3日
2002年1月2日
01/01/2002
2001年12月31日
二〇〇一年十二月三十零日

I have a data set with the following format

The first and second fields denote the dates (M/D/YYYY) of starting and ending of a study.

How one expand the data into the desired output format, taking into account the leap years using AWK or BASH scripts?

Your help is very much appreciated.

Input

  7/2/2009   7/7/2009
  2/28/1996  3/3/1996
  12/30/2001 1/4/2002

Desired Output

  7/7/2009
  7/6/2009
  7/5/2009
  7/4/2009
  7/3/2009
  7/2/2009
  3/3/1996
  3/2/1996
  3/1/1996
  2/29/1996
  2/28/1996
  1/4/2002
  1/3/2002
  1/2/2002
  1/1/2002
  12/31/2001
  12/30/2001

解决方案

If you have gawk:

#!/usr/bin/gawk -f
{
    split($1,s,"/")
    split($2,e,"/")
    st=mktime(s[3] " " s[1] " " s[2] " 0 0 0")
    et=mktime(e[3] " " e[1] " " e[2] " 0 0 0")
    for (i=et;i>=st;i-=60*60*24) print strftime("%m/%d/%Y",i)
}

Demonstration:

./daterange.awk inputfile

Output:

07/07/2009
07/06/2009
07/05/2009
07/04/2009
07/03/2009
07/02/2009
03/03/1996
03/02/1996
03/01/1996
02/29/1996
02/28/1996
01/04/2002
01/03/2002
01/02/2002
01/01/2002
12/31/2001
12/30/2001

这篇关于如何生成给予启动和使用bash脚本AWK结束日期的日期序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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