Hi,
This ksh script display the lines of your file for the
days last days.
All displayed lines are prefixed by the day of week (Sun-Sat).
LastDays.shl your_file 21
will display lines from 01/13/01 to 01/27/01 (last date of the file).
LastDays.ksh
File=${1:?"Usage: $0 file [days]"}
Days=${2:-28}
LastDate=$(tail -1 $File | awk '{print $1}')
awk -v LAST="$LastDate" -v DAYS="$Days" -f LastDays.awk $File
LastDays.awk
Note: The CalDate function isn't use here
but may be usefull
#===================================================
# F U N C T I O N S
#====================================================
#----------------------------------------------------
# Convert Julian to calendar date
#----------------------------------------------------
function CalDate(jj, a,b,c,d,e,f,x,w,z,dd,mm,yy) {
#sub(",",".",jj)
z = jj + 0.5
w = int((z - 1867216.25) / 36524.25)
x = int(w / 4.0)
a = z + 1.0 + w - x
b = a + 1524.0
c = int((b - 122.1) / 365.25)
d = int(365.25 * c)
e = int((b - d) / 30.6001)
f = int(30.6001 * e)
dd = b - d - f
if (e < 13.5)
mm = e - 1
else
mm = e - 13
if (mm > 2.5)
yy = c - 4716
else
yy = c - 4715
return sprintf("%4d %02d %02d", yy, mm, dd)
}
#----------------------------------------------------
# Convert calendar to Julian date
#----------------------------------------------------
function JulDate(y, m, d, jd) {
if ((100.0 * y + m - 190002.5) < 0)
jd = 0.5
else
jd = 0.0
jd = 367.0 * y
jd -= int(7.0 * (y + int((m + 9.0) / 12.0)) / 4.0)
jd += int(275.0 * m / 9.0)
jd += d
jd += 1721013.5
return jd
}
#----------------------------------------------------
# Weekday of Julian date (0=Sunday)
#----------------------------------------------------
function WeekDay(jd) {
return (jd + 1.5) % 7
}
#----------------------------------------------------
# Convert calendar (mm/dd/yy) to julian date
#----------------------------------------------------
function GetJulianDate(cd, yy, mm, dd) {
mm = substr(cd, 1, 2)
dd = substr(cd, 4, 2)
yy = substr(cd, 7, 2)
if (yy < 100) {
if (yy <= 68)
yy += 2000 ;
else
yy += 1900 ;
}
return JulDate(yy, mm,dd)
}
#===================================================
# P A T T E R N S / A C T I O N S
#===================================================
#---------------------------------------------------
# Init ...
#---------------------------------------------------
BEGIN {
split("Sun-Mon-Tue-Wed-Thu-Fri-Sat",Wdays,"-"

LastJulian = GetJulianDate(LAST)
FirstJulian = LastJulian - DAYS
}
#---------------------------------------------------
# Select lines : Date > LAST - DAYS
#---------------------------------------------------
{
Date = $1
Julian = GetJulianDate(Date)
Wd = Wdays[WeekDay(Julian)+1]
if (Julian > FirstJulian)
print Wd,$0
}
Jean Pierre.