开发者

How to generate a sequence of dates given starting and ending dates using AWK of BASH scripts?

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


It can be done nicely with bash alone:

for i in `seq 1 5`;
do
  date -d "2017-12-01 $i days" +%Y-%m-%d;
done;

or with pipes:

seq 1 5 | xargs -I {} date -d "2017-12-01 {} days" +%Y-%m-%d


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

Edit:

The script above suffers from a naive assumption about the length of days. It's a minor nit, but it could produce unexpected results under some circumstances. At least one other answer here also has that problem. Presumably, the date command with subtracting (or adding) a number of days doesn't have this issue.

Some answers require you to know the number of days in advance.

Here's another method which hopefully addresses those concerns:

while read -r d1 d2
do
    t1=$(date -d "$d1 12:00 PM" +%s)
    t2=$(date -d "$d2 12:00 PM" +%s)
    if ((t2 > t1)) # swap times/dates if needed
    then
        temp_t=$t1; temp_d=$d1
        t1=$t2;     d1=$d2
        t2=$temp_t; d2=$temp_d
    fi
    t3=$t1
    days=0
    while ((t3 > t2))
    do
        read -r -u 3 d3 t3 3<<< "$(date -d "$d1 12:00 PM - $days days" '+%m/%d/%Y %s')"
        ((++days))
        echo "$d3"
    done
done < inputfile


You can do this in the shell without awk, assuming you have GNU date (which is needed for the date -d @nnn form, and possibly the ability to strip leading zeros on single digit days and months):

while read start end ; do
    for d in $(seq $(date +%s -d $end) -86400 $(date +%s -d $start)) ; do
        date +%-m/%-d/%Y -d @$d
    done
done

If you are in a locale that does daylight savings, then this can get messed up if requesting a date sequence where a daylight saving switch occurs in between. Use -u to force to UTC, which also strictly observes 86400 seconds per day. Like this:

while read start end ; do
    for d in $(seq $(date -u +%s -d $end) -86400 $(date -u +%s -d $start)) ; do
        date -u +%-m/%-d/%Y -d @$d
    done
done

Just feed this your input on stdin.

The output for your data is:

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


Another option is to use dateseq from dateutils (http://www.fresse.org/dateutils/#dateseq). -i changes the input format and -f changes the output format. -1 must be specified as an increment when the first date is later than the second date.

$ dateseq -i %m/%d/%Y -f %m/%d/%Y 7/7/2009 -1 7/2/2009
07/07/2009
07/06/2009
07/05/2009
07/04/2009
07/03/2009
07/02/2009
$ dateseq 2017-04-01 2017-04-05
2017-04-01
2017-04-02
2017-04-03
2017-04-04
2017-04-05


I prefer ISO 8601 format dates - here is a solution using them. You can adapt it easily enough to American format if you wish.

AWK Script

BEGIN {
    days[ 1] = 31; days[ 2] = 28; days[ 3] = 31;
    days[ 4] = 30; days[ 5] = 31; days[ 6] = 30;
    days[ 7] = 31; days[ 8] = 31; days[ 9] = 30;
    days[10] = 31; days[11] = 30; days[12] = 31;
}
function leap(y){
    return ((y %4) == 0 && (y % 100 != 0 || y % 400 == 0));
}
function last(m, l,  d){
    d = days[m] + (m == 2) * l;
    return d;
}
function prev_day(date,   y, m, d){
    y = substr(date, 1, 4)
    m = substr(date, 6, 2)
    d = substr(date, 9, 2)
    #print d "/" m "/" y
    if (d+0 == 1 && m+0 == 1){
        d = 31; m = 12; y--;
    }
    else if (d+0 == 1){
        m--; d = last(m, leap(y));
    }
    else
        d--
    return sprintf("%04d-%02d-%02d", y, m, d);
}
{
    d1 = $1; d2 = $2;
    print d2;
    while (d2 != d1){
        d2 = prev_day(d2);
        print d2;
    }
}

Call this file: dates.awk

Data

2009-07-02 2009-07-07
1996-02-28 1996-03-03
2001-12-30 2002-01-04

Call this file: dates.txt

Results

Command executed:

awk -f dates.awk dates.txt

Output:

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


You can convert date to unix timestamp and then sequencing on it, you can even have granularity of nanoseconds if you want (with '%N' in date)

The following example prints time from 2020-11-07 00:00:00 to 2020-11-07 01:00:00 in intervals of 5 minutes

# total seconds past 1970-01-01 00:00:00 as observed on UTC timestamp in UTC
# you change TZ to represent time in your timezone like TZ="Asia/Kolkata"

start_time=$(date -u -d 'TZ="UTC" 2020-11-07 00:00:00' '+%s')   
end_time=$(date -u -d 'TZ="UTC" 2020-11-07 01:00:00' '+%s')


# 60 seconds * 5 times (i.e. 5 minutes)
# you change interval according your needs or leave it to show every second

interval=$((60 * 5))


# generate sequence with intervals and convert back to timestamp in UTC
# again change TZ to represent timein your timezone

seq ${start_time} ${interval} ${end_time} | 
xargs -I{} date -u -d 'TZ="UTC" @'{} '+%F %T'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜