Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date manipulation in Awk

Status
Not open for further replies.

ganeshmb

Programmer
Dec 5, 2001
32
US
I need an awk script for the problem illustrated below.

My input file will look like this,


560,2/20/1998,aq
560,2/24/1998,bq
560,2/27/1998,aw
560,2/28/1998,qwe
560,3/4/1998,iop
560,3/6/1998,pai


My required output is,



560,2/20/1998,aq
560,2/21/1998,aq
560,2/22/1998,aq
560,2/23/1998,aq
560,2/24/1998,bq
560,2/25/1998,bq
560,2/26/1998,bq
560,2/27/1998,aw
560,2/28/1998,qwe
560,3/1/1998,qwe
560,3/2/1998,qwe
560,3/3/1998,qwe
560,3/4/1998,iop
560,3/5/1998,iop
560,3/6/1998,pai



Note:
1) There are exactly three fields in all input records.
2) Dates will ALWAYS be in sorted order as given in this example.
3) Manipulation is necessary only on the second field(Date) to arrive at the final data. Any other pattern you might observe in first or third fields of this example is purely unintentional.

Thanks in advance.
 
Forget it, should have read closer. sorry.
 
I guess you have gotten my problem totally wrong. Input file is already sorted by date. So, no sorting is required. The crux of my requirement is the need for continous dates in my output.
Two rows like this,

560,2/20/1998,aq
560,2/24/1998,bq


should become

560,2/20/1998,aq
560,2/21/1998,aq
560,2/22/1998,aq
560,2/23/1998,aq
560,2/24/1998,bq


 
and you care 'bout leap years, doncha? ;)

I wish I had time - maybe over the weekend

vlad
 
Hi,

I hope the following scripy will resolve your ptoblem :

awk -f dm.awk input_file

--- dm.awk ---
BEGIN {
FS = "," ;
dm[1]=31; dm[2]=28; dm[3]=31; dm[4]=30; dm[5]=31; dm[6]=30;
dm[7]=31; dm[8]=31; dm[9]=30; dm[10]=31; dm[11]=30; dm[12]=31;
}
#
# Increment date
#
function NextDay(aDate, d,y) {
aDate[2] += 1 ;
d = dm[aDate[1]] ;
y = aDate[3] ;
if (aDate[1] == 2 && (y%4 == 0 && (y%100 != 0 || y%400 ==0)))
d += 1 ;
if (aDate[2] > d) {
aDate[2] = 1 ;
aDate[1] +=1 ;
if (aDate[1] > 12) {
aDate[1] = 1 ;
aDate[3] += 1 ;
}
}
}
#
# Compare two dates
#
function SameDates(aDate1, aDate2) {
return (aDate1[2] == aDate2[2] &&
aDate1[1] == aDate2[1] &&
aDate1[3] == aDate2[3])
}
#
# Memorize current line (F1,D,F3)
#
function Memo() {
PrvF1 = $1 ;
PrvD[1] = Date[1] ;
PrvD[2] = Date[2]
PrvD[3] = Date[3] ;
PrvF3 = $3 ;
}
#
# First line, initialize memorized line
#
NR == 1 {
split($2, Date, "/") ;
Date[2] -= 1;
Memo() ;
}
#
# Generate missing dates and print current line
#
{
split($2, Date, "/") ;
NextDay(PrvD) ;
while (! SameDates(PrvD,Date)) {
printf "%s,%d/%d/%d,%s\n",PrvF1,PrvD[1],PrvD[2],PrvD[3],PrvF3 ;
NextDay(PrvD) ;
}
print $0 ;
Memo() ;
}
--- End of dm.awk --- Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top