Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with getline

Status
Not open for further replies.

jescat

Technical User
Jul 29, 2004
32
US
I have an issue with an awk script I am stumped on and am looking for some assistance. Here is an example of an input file:

5019970714 W 4 0000000132
5019970721 W 4 0000000000
5020040201 20040315 F 4 0000001291

When $3 == "F" I need to explode the date range of start date(substr($0,3,10) and end date substr($0,13,10) into weeks and divide the total quatity(the rightmost column) amoung the weeks with the remainder of the quantity added to the beginning weeks. Example output of above input:

5019970714 W 4 0000000132
5019970721 W 4 0000000000
5020040201 W 4 0000000185
5020040208 W 4 0000000185
5020040215 W 4 0000000185
5020040222 W 4 0000000184
5020040229 W 4 0000000184
5020040307 W 4 0000000184
5020040314 W 4 0000000184

When there is one line in the file I have to explode everything this works fine. However if there are two or more lines I have problems. Here is an example inputfile:

5019970707 W 4 0000000132
5020040201 20040301 F 4 0000000132
5019970721 W 4 0000000000
5020040201 20040315 F 4 0000001291

Here is my output (along with some debug statements)

5019970707 W 4 0000000132
5020040201 W 4 0000000027
20040201 |
20040208|
20040208 |
5020040208 W 4 0000000027
20040208 |
20040215|
20040215 |
5020040215 W 4 0000000026
20040215 |
20040222|
20040222 |
5020040222 W 4 0000000026
20040222 |
20040229|
20040229 |
5020040229 W 4 0000000026
5019970721 W 4 0000000000
5020040201 W 4 0000000185
20040201 |
20040201 |
20040201 |
5020040201 W 4 0000000185
20040201 |
20040208|
20040208 |
5020040208 W 4 0000000185
20040208 |
20040208 |
20040208 |
5020040208 W 4 0000000184
20040208 |
20040215|
20040215 |
5020040215 W 4 0000000184
20040215 |
20040215 |
20040215 |
5020040215 W 4 0000000184
20040215 |
20040222|
20040222 |
5020040222 W 4 0000000184

After all that(hopefully I explained well enough) I looks as though my getline command is not always working when there are muliple line I have to explode(as you will be able to see with the debug lines). I have logic in my script to read the output of another script in as a variable(getline) but for whatever reason this command is not always "working" I don't see any reason why this is happening any assistance would be greatly apprciated.

Here is my script the dateplus.awk script is a script I downloaded months ago and contains serveral cool date coversion routines, I haven't had a problem with this script in the past and don't believe it the cause of this issue:

BEGIN {
FILLER = " "
WFLEXDATE = "W "
CMD = "awk -f /apps/server/script/dateplus.awk -- -s "
CMD2 = "awk -f /apps/server/script/dateplus.awk -- +7 "
}
function lpad(F_STR,F_LEN,F_CHAR,NEW_STR) {
F_LEN = F_LEN - length(F_STR);
for (z=1;z<=F_LEN;z++) {
NEW_STR = NEW_STR F_CHAR;
}
return NEW_STR F_STR;
}
{

RECTYPE=substr($0,1,2);
BDATE=substr($0,3,10);
EDATE=substr($0,13,10);
FLEXDATE=substr($0,23,1);
QUALIFIER=substr($0,33,10);
QUANTITY=substr($0,43,10);
if ( RECTYPE == "50" && FLEXDATE == "F" ) {
CMD BDATE | getline T_BDAY;
close(CMD);
CMD EDATE | getline T_EDAY;
close(CMD);
NUMWEEKS = ( T_BDAY - T_EDAY + 1 ) / 7;
if ( int(NUMWEEKS) < NUMWEEKS ) {
NUMWEEKS = int(NUMWEEKS) + 1;
}
REMAINDER = QUANTITY % NUMWEEKS;
if ( int(REMAINDER) < REMAINDER ) {
REMAINDER = int(REMAINDER) + 1;
}
QUANTITY = int(QUANTITY / NUMWEEKS);
QUANTITY = lpad(QUANTITY,10,0);
ADJQUANTITY = QUANTITY + 1;
ADJQUANTITY = lpad(ADJQUANTITY,10,0);
for (i=1;i<=NUMWEEKS;i++) {
if ( i <= REMAINDER ) {
if ( i == 1 ) {
print RECTYPE BDATE FILLER WFLEXDATE QUALIFIER ADJQUANTITY;
} else {
print BDATE "|";
CMD2 BDATE | getline BDATE;
close(CMD2);
print BDATE "|";
BDATE = BDATE" ";
print BDATE "|";
print RECTYPE BDATE FILLER WFLEXDATE QUALIFIER ADJQUANTITY;
}
} else {
if ( i == 1 ) {
print RECTYPE BDATE FILLER WFLEXDATE QUALIFIER QUANTITY;
} else {
print BDATE "|";
CMD2 BDATE | getline BDATE;
close(CMD2);
print BDATE "|";
BDATE = BDATE" ";
print BDATE "|";
print RECTYPE BDATE FILLER WFLEXDATE QUALIFIER QUANTITY;
}
}
}
} else {
print $0;
}
}

Thanks
JesCat
 
I narrowed the problem down to the specific issue yet I can't explain why awk does this. If someone can explain the logic behind this I would appreciate it.

Input file:

20030101
20030201
20030101

AWK script:

BEGIN {
CMD = "t.sh "
}
{
CMD $0 | getline MYDATE;
close(CMD);
print $0,MYDATE;
}

Shell script(t.sh):
echo $1

Output:

20030101 20030101
20030201 20030201
20030101 20030201


Conclusion:

It looks like AWK if buffering distinct values read in from "getline". If it encounters a duplicate value it uses the previous value read in. Does this make sense? Has anyone else ran into this, is there a workaround?

Thanks
JesCat
 
Code:
BEGIN {
    CMD = "t.sh "
}
{
    cmd=CMD $0
    cmd | getline MYDATE;
    close(cmd);
    print $0,MYDATE;
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks alot! That worked perfectly. If that just a syntax snag in that the command getting passing in must only be one parameter/string?

JesCat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top