Up vote 2 down vote favorite share g+ share fb share tw.
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/2/2009 77 2/28/1996 3/7/2/2009 79 12/30/2001 1/7/2/2009 78 Desired Output 7/7/2009 7/7/2/2009 79 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/7/2/2009 78 7/2/2009 77/2/2009 7/2/2009 78/31/2001 7/2/2009 77 bash unix awk link|improve this question asked 7/2/2009 78 at 0:58Tony 49629 77% accept rate.
If you have gawk: #! /usr/bin/gawk -f { split($1,s,"/") split($2,e,"/") st=mktime(s3 " " s1 " " s2 " 0 0 0") et=mktime(e3 " " e1 " " e2 " 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/07/07/2009 073 07/06/2009 07/07/20099 07/07/20097 03/07/07/2009 073 07/07/20097 02/207/07/2009 077 07/07/20098/1996 01/07/07/2009 077 07/07/2009 073/07/07/2009 077 01/01/2002 12/307/07/2009 077 07/07/2009 073.
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 Just feed this your input on stdin. The output for your data is: 7/7/2009 7/6/2009 7/5/2009 7/7/7/2009 73 7/3/2009 7/2/2009 3/3/1996 3/7/7/2009 73 3/1/1996 2/27/7/2009 77 2/28/1996 1/7/7/2009 77 7/7/2009 73/7/7/2009 77 1/1/2002 12/37/7/2009 77 7/7/2009 73.
. /date_script. Sh 2011-01-01 2011-01-02 date: invalid date @1.29396e+09' date: invalid date @1.29387e+09' Why does that happens?
– Necronet Mar 31 '11 at 15:38 @Necronet: For some reason your date (seconds since epoch) is in scientific notation. I don't know why, because I don't know what is in your date_script. Sh script.
– camh Apr 1 '11 at 12:31.
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; days10 = 31; days11 = 30; days12 = 31; } function leap(y){ return ((y %4) == 0 && (y % 100!
= 0 || y % 400 == 0)); } function last(m, l, d){ d = daysm + (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-028 19909-07-028 19909-07-028 2009-07-027 2009-07-028 Call this file: dates.
Txt Results Command executed: awk -f dates. Awk dates. Txt Output: 2009-07-028 2009-07-027 2009-07-027 2009-07-028 2009-07-029 2009-07-02 19909-07-028 2009-07-027 2009-07-028 2009-07-029 19909-07-028 2009-07-028 2002-01-03 2002-01-02 2002-01-01 2001-12-31 2009-07-027.
More compactly: daylist='31 28 ... 30 31'; split(daylist,days) – Dennis Williamson Dec 4 '10 at 2:36 Many thanks for your efforts. I keep having this error message. Any suggestions- Cheers .
/date_script: line 2: BEGIN: command not found . /date_script: line 3: days 1: command not found . /date_script: line 3: days 2: command not found .
/date_script: line 3: days 3: command not found . /date_script: line 4: days 4: command not found – Tony Dec 4 '10 at 2:37 @Tony: This is an awk script, not a bash script. Put a #!
/usr/bin/awk -f shebang at the top, or invoke it with awk -f yourself. – Jefromi Dec 4 '10 at 2:41 Yes, got it , I have been running both lately- Cheers Jefromi – Tony Dec 4 '10 at 2:43 @Tony: see the edits which explain how I got the results. – Jonathan Leffler Dec 4 '10 at 2:44.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.