FW: awk function question

FW: awk function question

Post by Kemp, Chery » Sun, 31 Dec 1899 09:00:00



This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01BF8D33.250757F0
Content-Type: text/plain;
        charset="iso-8859-1"

I originally posted this without a subject. Hopefully this time I will get a
response from more than one person.
Thanks

-----Original Message-----
From: Kemp, Cheryl
Sent: Monday, March 13, 2000 9:10 AM

Subject:

 <<grpride.ksh>>
Can someone please take a look at this and tell be what I'm doing wrong?
This is really an awk question, but I posted to the awk group and the
suggestion I was given didn't work. I'm really missing something here with
using functions and arrays.
What I am trying to do in the script is create an julian day based on the
5th field and insert this value in the 6th field when the 6th field is
blank.

Cheryl Kemp
Senior Data/Database Analyst
Coventry Health Care
Phone: (724) 778-6487

------_=_NextPart_000_01BF8D33.250757F0
Content-Type: application/octet-stream;
        name="grpride.ksh"
Content-Disposition: attachment;
        filename="grpride.ksh"

#!/bin/ksh

# Find name of input file for employer_grp_rider table

#DWT_FILE=`egrep 'DWT' *.dwh | nawk -F^ '{printf tolower($1)}'`
DWD_FILE=mcadwd
DWD_FILE_COPY=$DWD_FILE.orig
DWD_FILE_WORK=$DWD_FILE.work

# Make copy of original file (backup) and make working copy of file.  Sort it and write it out back to the original file

cp $DWD_FILE $DWD_FILE_COPY
sort $DWD_FILE_COPY -t ^ +1n -2 +4n -5 +2 -3 +0n -1 +3 -4 > $DWD_FILE_WORK

# Meat of program - nawk script

nawk 'BEGIN { FS="^"; }
function ToJul( date, jul_day ) {
spdate=split($5, datum, "/" )
lmonth = datum[1]
lday = datum[2]
lyear = datum[3]
if ( lyear < 80 )
lyear += 2000
else if ( lyear < 100 )
lyear += 1900
jul_day = lday - 32076 + 1461 * (lyear + 4800 + int(int((lmonth - 14)\
 / 12) / 4 \
)) \
+ 367 * int((lmonth - 2 - int((lmonth - 14) / 12) * 12) / 12 ) - \
3 * \
int(int((lyear + 4900 + int((lmonth - 14) / 12)) / 100) / 4 ) \
ret jul_day}

function prt_emp_buff(emp_buff,  loop)
{
        OFS=""
        for (loop = 1; loop <= numfields; ++loop)
        {
                printf (OFS"%s", emp_buff[loop])
                OFS="^"
        }
        printf "\n"  
}

NR == 1 {
        numfields=split($0, emp_buff)
        next
}

$1 == emp_buff[1] &&
$2 == emp_buff[2] &&
$3 == emp_buff[3] &&
$5 == emp_buff[5] &&  
! emp_buff[6] {
                emp_buff[6] = (ToJul(jul_day))
echo emp_buff[6]
}

{      
        prt_emp_buff(emp_buff)
        numfields=split($0, emp_buff)
}

END { prt_emp_buff(emp_buff) }

'# $DWD_FILE_WORK > $DWD_FILE

#/usr/bin/rm $DWD_FILE_WORK
chmod 777 $DWD_FILE

------_=_NextPart_000_01BF8D33.250757F0--

 
 
 

FW: awk function question

Post by David Killoug » Sun, 31 Dec 1899 09:00:00


The jul_day being passed to ToJul has no value. (do you want $5)
echo is not part of the awk language
! emp_buff[6] is risky, use && (emp_buff[6] != "")

Quote:>}

>$1 == emp_buff[1] &&
>$2 == emp_buff[2] &&
>$3 == emp_buff[3] &&
>$5 == emp_buff[5] &&
>! emp_buff[6] {
> emp_buff[6] = (ToJul(jul_day))
>echo emp_buff[6]
>}


 
 
 

FW: awk function question

Post by Rudy Fernande » Sun, 31 Dec 1899 09:00:00


There seem to be a number of problems.
Firstly, ToJul doesn't seem to be getting invoked until the following 4 lines are commented out

$1 == emp_buff[1] &&
$2 == emp_buff[2] &&
$3 == emp_buff[3] &&
$5 == emp_buff[5] &&

Secondly, ToJul does not seem to be doing the correct calculation. I get results like these

Jul_day for 1/1/99 =    9901213
Jul_day for 5/8/99 =    9901220

Maybe the test data I'm using is incorrect.

Personally, I find Informix extremely handy at carrying out date manipulations. You might want to consider loading the data
into Informix, doing the manipulation through a Stored Procedure and unloading the result.

Rudy


> ...

> # Meat of program - nawk script

> nawk 'BEGIN { FS="^"; }
> function ToJul( date, jul_day ) {
> spdate=split($5, datum, "/" )
> lmonth = datum[1]
> lday = datum[2]
> lyear = datum[3]
> if ( lyear < 80 )
> lyear += 2000
> else if ( lyear < 100 )
> lyear += 1900
> jul_day = lday - 32076 + 1461 * (lyear + 4800 + int(int((lmonth - 14)\
>  / 12) / 4 \
> )) \
> + 367 * int((lmonth - 2 - int((lmonth - 14) / 12) * 12) / 12 ) - \
> 3 * \
> int(int((lyear + 4900 + int((lmonth - 14) / 12)) / 100) / 4 ) \
> ret jul_day}

> ...