anyone here speak (n)awk properly?

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
I've got an irksome issue. I tend to use awk for textual manipulation and stuff. Now I've been asked to get it to calculate things. Well and good, but it's not doing what I expect. Someone care to help me out?

I have this in KSH:
Code:
days=$(( secs / 86400 ))
secs=$(( secs - 86400 * days ))
if (( secs < 0 )); then
    days=$(( days - 1 ))
    secs=$(( secs + 86400 ))
fi

j=$(( days/146097 ))
i=$(( days - 146097*j ))
a=$(( i + 719468 ))
b=$(( ( 4*a + 3 )/146097 ))
c=$(( a - ( 146097*b )/4 ))
d=$(( ( 4*c + 3 )/1461 ))
e=$(( c - ( 1461*d )/4 ))
m=$(( ( 5*e + 2 )/153 ))
year=$(( 400*j + 100*b + d + m/10 ))
mon=$(( m + 3 - 12*( m/10 ) ))
day=$(( e - ( 153*m + 2 )/5 + 1 ))

# Time of day.
hour=$(( secs / 3600 ))
secs=$(( secs - 3600 * hour ))
min=$(( secs / 60 ))
sec=$(( secs - 60 * min ))

echo "$year:$day:$mon:$hour:$min:$sec"

it works, or seems to heheh.

I've turned it into this in nawk:
Code:
nawk {
                SECS=$1
                DAYS=(SECS / 86400)
                SECS=((SECS - 86400) * DAYS)
                if (SECS < 0) {
                        DAYS=(DAYS - 1)
                        SECS=(SECS + 86400)}
                j= (DAYS / 146097)
                i= ((DAYS - 146097)*j)
                a= (i+719468)
                b= (((4*a)+3)/146097)
                c= ((a-(146097*b))/4)
                d= ((4*c + 3)/1461)
                e= ((c -(1461*d))/4)
                m= ((5*e + 2)/153)
                year= ((400*j) + (100*b) + d + (m/10))
                mon= (m + 3 - 12 *(m/10))
                day= (e - (153*m+2)/5 + 1)

                hour= (SECS / 3600)
                secs= (SECS - 3600 * hour)
                min= (secs / 60)
                sec= (secs - 60 * min)

                print year":"day":"mon":"hour":"min":"sec
        }

the input number in both cases is [highlight]1121351040[/highlight]. the KSH computes it thusly: 2005:14:7:14:24:0 and it's correct.
the nawk code computes it like so: 1820.74:0.2:-2598.63:4.04235e+09:0:0 which is pissing me off. anyone spot the mistake?
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Untested (tbh I haven't a clue what ksh or nawk are) but try:
Code:
nawk {
                SECS=$1
                DAYS=(SECS / 86400)
                SECS=((SECS - (86400 * DAYS))
                if (SECS < 0) {
                        DAYS=(DAYS - 1)
                        SECS=(SECS + 86400)}
                j= (DAYS / 146097)
                i= ((DAYS - (146097*j))
                a= (i+719468)
                b= (((4*a)+3)/146097)
                c= ((a-(146097*b))/4)
                d= ((4*c + 3)/1461)
                e= ((c -(1461*d))/4)
                m= ((5*e + 2)/153)
                year= ((400*j) + (100*b) + d + (m/10))
                mon= (m + 3 - 12 *(m/10))
                day= (e - (153*m+2)/5 + 1)

                hour= (SECS / 3600)
                secs= (SECS - 3600 * hour)
                min= (secs / 60)
                sec= (secs - 60 * min)

                print year":"day":"mon":"hour":"min":"sec
        }
I think you mis-translated the brackets and multiplication
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
cheers JB! I've tried your code, but had to remove two brackets at SECS and DAYS. then it worked and came up with yet another answer: 1850.58:0.2:-2642.1:0:0:0
:(

edit:

to be more precise nawk didn't like these:

SECS=[highlight]([/highlight](SECS - (86400 * DAYS))

and

i= [highlight]([/highlight](DAYS - (146097*j))

looking at it I don't particularly like them either, but I'll try anything. been looking at this stuff for too long :(
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
Code:
{
                SECS=$1
                DAYS=int(SECS / 86400)
                SECS=(SECS - (86400 * DAYS))
		if (SECS < 0) {
                        DAYS=(DAYS - 1)
                        SECS=(SECS + 86400)
		}

                j= (DAYS / 146097)
                i= (DAYS - (146097*j))
                a= (i+719468)
                b= (((4*a)+3)/146097)
                c= ((a-(146097*b))/4)
                d= ((4*c + 3)/1461)
                e= ((c -(1461*d))/4)
                m= ((5*e + 2)/153)

                year= ((400*j) + (100*b) + d + (m/10))
                mon= (m + 3 - 12 *(m/10))
                day= (e - (153*m+2)/5 + 1)

                hour= int(SECS / 3600)
                secs= (SECS - 3600 * hour)
                min= (secs / 60)
                sec= (secs - 60 * min)
                print year":"day":"mon":"hour":"min":"sec
}

That fixes a bit more, evidently the problem is your shell does integer division and nawk does not. Why on earth would anyone want to do this btw?
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
well, it was a quick and dirty way to convert seconds-since-the-epoch to um, Julian (is it Julian, I always seem to forget this stuff :/)

as is, I've gotten a workaround, which is nice, though it did hurt a bit after spending some time trying to make it work :/
 

Users who are viewing this thread

Top Bottom