How to get weekday for date value using awk
e.g. Input 12/06/08, get FRI
Moonhk
Dunno about getting Friday from Thursday's date ;)
~$ echo "13/06/08" | awk '{split($0,a,"/");print \
toupper(strftime("%a", mktime("20"a[3]" "a[2]" "a[1]" 12 0 0")))}'
FRI
Grant.
--
http://bugsplatter.mine.nu/
> On Thu, 12 Jun 2008 20:51:16 -0700 (PDT), moonhkt <moo...@gmail.com>
> wrote:
>
>>Hi All
>>
>>How to get weekday for date value using awk
>>
>>e.g. Input 12/06/08, get FRI
>>
> Dunno about getting Friday from Thursday's date ;)
Ah, dire conditions may shrink time, hence you'll have
to use a cheat, here's one usink awk, a bunch of "ones"
and awkwardness ;-)
$ date -d $(echo "12/06/08" | awk 'NR==1{$1=1+$1} 1' RS=/ | tac | awk '1' ORS=) +%a
Fri
Aor if you insist on needing upper case:
$ date -d $(echo "12/06/08" | awk 'NR==1{$1=1+$1} 1' RS=/ | tac | awk '1' ORS=) +%^a
FRI
a mighty 'Par 5'...
at least it can be a showcase starter for the toolbox ;D)
Another cheat:
$ echo "12/06/08" | awk '{split($0,a,"/");print \
toupper(strftime("%a", mktime("20"a[3]" "a[2]" "a[1]" 24 0 0")))}'
FRI
Hermann
And there is also a pure AWK solution that do not need strftime, but
directly implements the Zeller's congruence algorithm:
function weekday( year, month, day, x1, x2, x3, x4 ) {
x1 = int( (14 - month) / 12 )
x2 = year - x1
x3 = month + 12 * x1 - 2
x4 = x2 + int(x2/4) - int(x2/100) + int(x2/400) + int( (31*x3) / 12)
return (x4 + day) % 7
}
--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado