awk program date function no longer running


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk program date function no longer running
# 1  
Old 02-07-2020
awk program date function no longer running

I work at a company that uses a program written in AWK to track various data and prepare reports. Worked with this program for three years plus (the author is no longer with us) and the YTD Production report will not return a report with a date after 123119. This is a problem. Below is the (I believe) the code for the print YTD report function.


<
Code:
function f_printytd(            ytd_date, i, j, k) {

    plus6 = 4
    plus7 = 5
    ytd_date = false
    while ( ytd_date !~ /^[01][0-9][0-3][0-9][901][0-9]$/ ) {
        printf("\n\t\tEnter last date for report (MMDDYY): ") 
        getline ytd_date < keyboard
        if ( toupper(ytd_date) == "X" ) return false
        if ( length(ytd_date) == 0 ) {
            printf("\n\t\tUsing %s", datestr)
            ytd_date = mdy_global
        }
    }

    yymmdd = f_mdy2ymd(ytd_date)            #y2k
    yy = substr(yymmdd, 1, 2)
    if ( yy < 50 )    YY = 20
    else        YY = 19
    lastdate = sprintf("%s%s", YY, yymmdd)

    FS = "%"
    while ( ( getline < runlist ) > 0 ) {
        A_ytd_gtype[$1] = $4
        A_ytd_var[$1] = $5
    }
    close(runlist)

    FS = "%"
    while ( ( getline < packfile ) > 0 ) {
        ymd = f_mdy2ymd($2)            #y2k
        y = substr(ymd, 1, 2)
        if ( y < 50 )    Y = 20
        else        Y = 19
        thisdate = sprintf("%s%s", Y, ymd)

        if ( thisdate > lastdate ) continue

        gt = A_ytd_gtype[$1]
        vr = A_ytd_var[$1]

        if ($3 ~ /^\#/) {
            sub("#", "", $3)
            A_ytd_trucks[gt] +=  $3
            A_ytd_trucks["T"] +=  $3
            A_ytd_hours[gt] += $4
            A_ytd_hours["T"] += $4    
        }
        else {
            A_ytd_dt[$2]
            ytd_cwt = $4 * prod_wght[$3] / 100
            A_ytd_cwtV[vr] += ytd_cwt
    
            if ($3 ~ /^1/) {
                A_ytd_cwt1[gt] += ytd_cwt
                A_ytd_cwt1[vr] += ytd_cwt
                A_ytd_cwt1["T"] += ytd_cwt
                if (prod_clas[$3] >= plus7) {
                    A_ytd_size[gt] += ytd_cwt
                    A_ytd_size[vr] += ytd_cwt
                    A_ytd_size["T"] += ytd_cwt
                }
            }
            else if ($3 ~ /^2/) {
                A_ytd_cwt2[gt] += ytd_cwt
                A_ytd_cwt2[vr] += ytd_cwt
                A_ytd_cwt2["T"] += ytd_cwt
            }
            else if ($3 ~ /^B/) {
                A_ytd_cwtB[gt] += ytd_cwt
                A_ytd_cwtB[vr] += ytd_cwt
                A_ytd_cwtB["T"] += ytd_cwt
            }
            else {
                A_ytd_cwtC[gt] += ytd_cwt
                A_ytd_cwtC[vr] += ytd_cwt
                A_ytd_cwtC["T"] += ytd_cwt
            }
        }
    }
    close(packfile)
    
    printf("\t\t%s        %s\n\n\t\t%s%s\n\n", datestr, co,
        "YTD Production Summary Report Through ",
        f_datef(ytd_date))                > tmpfile

    f_ytd_printinfo("O", "Outside Growers:")
    f_ytd_printinfo("S", "Share Growers:")
    if ( f_ytd_printinfo("T", "\nTOTALS:") < 0 ) return

    cwtTT = A_ytd_cwt1["T"] + A_ytd_cwt2["T"] + \
        A_ytd_cwtB["T"] + A_ytd_cwtC["T"]

    printf("\nProduction By Variety:\n\n")            > tmpfile
    printf("%11s%16s%12s%7s%7s%7s%7s\n", "Variety", "Total CWT", 
        "%7oz.", "%Ones", "%Twos", "%Bees", "%Cull")    > tmpfile

    for ( x in A_ytd_cwtV ) {
        if ( A_ytd_cwt1[x] + 0 == 0 )    prt_size_val = 0
        else    prt_size_val = A_ytd_size[x] * 100 / A_ytd_cwt1[x]
        printf(" %5.1f%% %-8s:%10.2f%12.1f%7.1f%7.1f%7.1f%7.1f\n", 
            A_ytd_cwtV[x] * 100 / cwtTT, x, A_ytd_cwtV[x],
            prt_size_val,
            A_ytd_cwt1[x] * 100 / A_ytd_cwtV[x],
            A_ytd_cwt2[x] * 100 / A_ytd_cwtV[x],
            A_ytd_cwtB[x] * 100 / A_ytd_cwtV[x],
            A_ytd_cwtC[x] * 100 / A_ytd_cwtV[x])    > tmpfile
        varT += A_ytd_cwtV[x]
    }
    printf("\t%19s\n\t%-8s:%10.2f\n", 
        "==========", "Checksum", varT)            > tmpfile

    for ( xx in A_ytd_dt ) if ( xx !~ /r$/ ) num_days++

    if (num_days < 1) 
        printf("\n\nNo packing days reported.\n") > tmpfile
    else printf("\n\nRun %d days, Avg: %.1f MTH/day,  %d 12BC/day.\n", 
        num_days, A_ytd_hours["T"] / num_days,
        cwtTT / num_days)                 > tmpfile
    close(tmpfile)
    printf("\a\n\t\t") 
    comm = sprintf("%s %s", printer, tmpfile)
    system(comm)
}

function f_ytd_printinfo(n, name,               tot_ytd_cwt) {

    tot_ytd_cwt = A_ytd_cwt1[n] + A_ytd_cwt2[n] + \
        A_ytd_cwtB[n] + A_ytd_cwtC[n]
    if (tot_ytd_cwt * 100 < 5) {        # less than 5 pounds
        printf("\nNo Production For %s\n\n", name)    > tmpfile
        return -1
    }

    printf("\n%s\n%s\n", name, "================")        > tmpfile

    printf("%5.1f%% %-10s%10.2f%30s%9.1f%%\n", 
        A_ytd_cwt1[n] * 100 / tot_ytd_cwt, "Ones:", 
        A_ytd_cwt1[n], "Plus Seven Oz. :", 
        A_ytd_size[n] * 100 / A_ytd_cwt1[n])        > tmpfile

    printf("%5.1f%% %-10s%10.2f%30s%10.1f\n", 
        A_ytd_cwt2[n] * 100 / tot_ytd_cwt, "Twos:", A_ytd_cwt2[n], 
        "Main Table Hrs.:", A_ytd_hours[n])        > tmpfile

    printf("%5.1f%% %-10s%10.2f%30s%10.1f\n", 
        A_ytd_cwtB[n] * 100 / tot_ytd_cwt, "Bees:", A_ytd_cwtB[n], 
        "Truck loads run:", A_ytd_trucks[n])        > tmpfile

    printf("%5.1f%% %-10s%10.2f%30s%10d\n", 
        A_ytd_cwtC[n] * 100 / tot_ytd_cwt, "Cull:", 
        A_ytd_cwtC[n], "Ones per MTH   :", 
        A_ytd_cwt1[n] / A_ytd_hours[n])            > tmpfile

    printf("%27s\n", "==========")                > tmpfile

    printf("%6s%-11s%10.2f\n\n", "", "Total:", tot_ytd_cwt)    > tmpfile
}

function f_printpct(              i, j, k, tot, total_cwt,
            total_cw1, total_cw2, total_cwB, total_cwC) {

    f_clear()
    printf("\n\t%s%s  %s\n\n\n", 
        co, "    Print List Of Runs   ", hist_code) 
    
    num_runs = f_getrunrg(A_runs)
    if ( ! num_runs ) f_exit()

    FS = "%"
    for ( i = 1; i <= num_runs; i++ ) {
        split(A_runs[i], runinfo, FS)
        runn         = runinfo[1]
        A_runs[runn]
        A_runn[i]    = runn
        grcode        = runinfo[2]
        A_gstr[runn]       = grwr_name[grcode]
        A_lotnum[runn]     = runinfo[3]
        A_grt[runn]    = runinfo[4]
        A_vty[runn]    = runinfo[5]
    }

    run_file = sprintf("%s%s\\%s", root_dir, data_dir, runf)
    FS = "%"
    while ( ( getline < run_file ) > 0 ) {
        if ( $1 in A_runs ) {
            pct_cwt = prod_wght[$2] * $3 / 100
            if ( $2 ~ /^1/ ) {
                A_pct_cw1[$1] += pct_cwt
                if ( prod_clas[$2] >= 5 ) {
                    A_pct_7oz[$1] += pct_cwt
                }
            }
            else if ( $2 ~ /^2/ ) A_pct_cw2[$1] += pct_cwt
            else if ( $2 ~ /^B/ ) A_pct_cwB[$1] += pct_cwt
            else if ( $2 ~ /^C/ ) A_pct_cwC[$1] += pct_cwt
        }
        else continue
    }
    close(run_file)

    line = 500
    for ( j = 1; j <= num_runs; j++ ) {
        if ( line > 50 ) {
            if ( line < 500 ) printf("\f")        > tmpfile
            printf("%s%s%s%3d\n\n%s\n\n\n", datestr, co, 
            "      GAS Run Percentages         Page",
            ++page, rng_str)            > tmpfile
            printf("%-5s%-2s%-33.31s%-10s%6s%6s%6s%6s%6s\n\n",
            "RUN", "", "GROWER DESCRIPTION", "CWT RAN",
            "%+7oz.", "%1's", 
            "%2's", "%B's", "%C's")            > tmpfile
    
            line = 5
        }
        r = A_runn[j]
          grwrstr = sprintf("%s #%s, %s",
            A_gstr[r], A_lotnum[r], A_vty[r])

          tot = A_pct_cw1[r] + A_pct_cw2[r] + A_pct_cwB[r] + A_pct_cwC[r]
          if ( tot == 0 ) continue
    
        if ( A_pct_cw1[r] == 0 ) seven_oz = 0
        else seven_oz = A_pct_7oz[r] * 100 / A_pct_cw1[r]
          printf("%-6d%s-%-32.30s%9.2f%7.1f%6.1f%6.1f%6.1f%6.1f\n\n",
            r, A_grt[r], grwrstr, tot,
            seven_oz,
            A_pct_cw1[r] * 100 / tot,
            A_pct_cw2[r] * 100 / tot,
            A_pct_cwB[r] * 100 / tot,
            A_pct_cwC[r] * 100 / tot)        > tmpfile
    
          line += 2

          total_7oz += A_pct_7oz[r]
          total_cw1 += A_pct_cw1[r]
        total_cw2 += A_pct_cw2[r]
        total_cwB += A_pct_cwB[r]
        total_cwC += A_pct_cwC[r]
    }

      total_cwt = total_cw1 + total_cw2 + total_cwB + total_cwC

    if ( total_cw1 == 0 ) tot_seven_oz = 0
    else tot_seven_oz = total_7oz * 100 / total_cw1
      printf("\n%-40s%9.2f%7.1f%6.1f%6.1f%6.1f%6.1f\n",
        "Totals for Runs in Range:", total_cwt,
        tot_seven_oz,
        total_cw1 * 100 / total_cwt,
        total_cw2 * 100 / total_cwt,
        total_cwB * 100 / total_cwt,
        total_cwC * 100 / total_cwt)            > tmpfile

    close(tmpfile)

    printf("\a\n\t") 
    comm = sprintf("%s %s", printer, tmpfile)
    system(comm)
}

function f_FormatDate(date,             c) {

    if ( substr(date, 5, 2) > 50 )    c = 19
    else                c = 20

    return sprintf("%s%s%s", c, substr(date, 5, 2), substr(date, 1, 4))
}

>
Any help would be greatly appreciated. Thanks, Paul G.

Last edited by Scrutinizer; 02-07-2020 at 03:31 PM.. Reason: code tags
# 2  
Old 02-07-2020
Hi,

Try changing:
Code:
while ( ytd_date !~ /^[01][0-9][0-3][0-9][901][0-9]$/ ) {

into
Code:
while ( ytd_date !~ /^[01][0-9][0-3][0-9][0-9][0-9]$/ ) {

and try and test if that might work out for you...

Last edited by Scrutinizer; 02-07-2020 at 05:48 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-07-2020
Thanks so much! That fixed it. This will really make me look like I know what I'm doing come Tuesday when the report is used. Smilie
# 4  
Old 02-07-2020
Quote:
Originally Posted by paulgdavitt
Thanks so much! That fixed it. This will really make me look like I know what I'm doing come Tuesday when the report is used. Smilie
Good to hear the suggestion seems to work out for you Smilie
Let me explain what it means:
Code:
while ( ytd_date !~ /^[01][0-9][0-3][0-9][901][0-9]$/ ) {

This is a so-called "regular expression" , which means that the number must contain 6 digits (each bracket pair represents a digit), where the
first one ([01]) may be 0 or 1,
the second ( [0-9]) between 0 and 9
the 3rd between 0 and 3 and
...
the 5th number ([901]) 9, 0 or 1.
..

So the modification changes ([901] to [0-9]) so that the 5th number may be between 0 and 9 inclusively.

Last edited by Scrutinizer; 02-08-2020 at 03:01 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check the processes running longer than 2 hours.?

HI can someone help me to check the process running more than 2 hours. I have the below command which shows the time and process id, however, I only need the processes running more than 2 hours. (8 Replies)
Discussion started by: Vinod
8 Replies

2. UNIX for Dummies Questions & Answers

AWK error - string cannot be longer than X bytes

Hi Friends, Could you please tell me why i am getting the below eror while working with awk. I am confused :confused: what to do ? awk: 0602-591 String 1,9,20,6,6 cannot be longer than 399 bytes. The source line is 1. The error context is >>> <<< awk: 0602-591... (2 Replies)
Discussion started by: i150371485
2 Replies

3. UNIX for Dummies Questions & Answers

apply a function twice successively with the same input in awk program

Hi ! It is a general question. When an awk script looks like: #! bin/awk function example(i){ <body> } { example(1) #the function uses input_1 and return output_a } { example(2) #the function uses previous output_a as an input and returns... (15 Replies)
Discussion started by: beca123456
15 Replies

4. Shell Programming and Scripting

Ending user sessions which have been on a program for longer than 1 hour

hi, im looking to write a script to end user sessions which are on a paticular program and have been for over an hour so I can free up the system a bit. I understand that the "who" command lists the current users logged into the system, however it does not let me see what program they are on... (12 Replies)
Discussion started by: 02JayJay02
12 Replies

5. Shell Programming and Scripting

awk: Input line Cannot be longer than 3,000 bytes.

Guys, I want to get the high CPU utilization from top. I am using below code : top -d2 >> /home/dba_monitoring/host_top_output.txt echo "Script started `date`" > $runlog usage=`grep "^ *$1" /home/dba_monitoring/host_top_output.txt | awk '{print $12}' | sed 's/%//'` And getting below... (7 Replies)
Discussion started by: wahab
7 Replies

6. Solaris

Graphical program no longer works after Solaris 10 upgrade

This is a fairly complex issue. I do not have a lot of knowledge on X11. But here are the things. I am running a program called Synergy off a Solaris server. The server sits in a remote network and can be accessed via NAT. Using Putty, I will enable X11 forwarding and launch Synergy via Putty.... (0 Replies)
Discussion started by: Leion
0 Replies

7. Shell Programming and Scripting

AWK input can not be longer than 3000 bytes

Hi, i have following line in my code. eport.pl < $4 | dos2ux | head -2000 | paste -sd\| - | awk -v S="$1" ' Issue is, i get a message saying "awk:input line | found /file/path cannot be longer than 3000 bytes." "source line number is 3" Can someone help me with this please? (4 Replies)
Discussion started by: usustarr
4 Replies

8. Shell Programming and Scripting

program name and function name builtins

Hi Is there a way to get the program/script name or function name usng built ins. Like in many languages arg holds the program name regards (2 Replies)
Discussion started by: xiamin
2 Replies

9. Shell Programming and Scripting

putting ftp in korn function - no longer working

This works if it's not in a function. I pulled it into one and I get syntax error, no mathcing '<'. I have to call it several times and need it to be a function. Any ideas? function FTP_Engine_File { ftp -inv ${FTP_SERVER} << EOF_FTP >> ${FTP_LOG} user ${FTP_USER} ${FTP_PSWD} ... (4 Replies)
Discussion started by: brdholman
4 Replies

10. UNIX for Advanced & Expert Users

scripts no longer running (solaris 8)

hello: I am a somewhat experienced unix user, but brand new to this forum. I am encountering a strange new problem. I have a shell script called foo.ksh it has been running for years (literally) on my Sun (Solaris 8) machine. Recently we put a version of samba on this machine to... (3 Replies)
Discussion started by: smcadoo
3 Replies
Login or Register to Ask a Question

Featured Tech Videos