awk question

Hi,

I have a awk script which takes the input as dat file and generates doc file as output. This script is running fine for long time. But lately users are complaining about one problem. If there are multiple parts number it should display the information by part numbers.
like this:

ORDER #:15WZ0000	Part #:15117110		Model Y.:6   Recv. Dock:DDD   ST Loc.:18003
.......................................................................................
ORDER #:15WZ0006	Part #:15856412		Model Y.:6   Recv. Dock:DDD   ST Loc.:18003

Previous Cumm. Quantity:0		Horizon Start:20051225	Horizon End:20060204
Cumulative Quantity    :0		Horizon Start:20051225	Dispatch Date:20060205


FORECAST           PERIOD    DATE REQUIRED               QUANTITY
======================================================================
Firm                Weekly   20060206                           810
Firm                Weekly   20060213                           542
======================== End of the Item 15856412 =======================
Firm                Weekly   20060305                           105
Planning            Weekly   20060213                           840
Planning            Weekly   20060220                          1170
Planning            Weekly   20060227                          1200
Planning            Weekly   20060306                          1740
Planning            Weekly   20060313                          1110
Planning            Weekly   20060320                          1155
Planning            Weekly   20060327                           825
Planning            Weekly   20060410                           690
Planning            Weekly   20060417                           930
Planning            Weekly   20060424                          1365
Planning            Weekly   20060501                          1365
Planning            Weekly   20060508                          1215
Planning            Weekly   20060515                          1035
Planning            Weekly   20060522                           855

As you can see there are two part number and after the first part number it displays a line that has the data saying "end of item".

It works fine when there are multiple part numbers in one file. But, If I have multiple files then it shows the correct information for 1st part but not for other parts like part level information.

In my second post, I am uploading the script which are using to generate this report.

Script which generates the report file:

BEGIN { FS = "|"
LOOP=0
PRINT=0
}

{
if ( $1 == "H" )
{
printf("                              GM DELFOR      \n\n")
printf("Document #:%s                   Material Release #:%s           DUNS #:%
s\n",$6,$15,$13)
printf("Doc. Date :%s           Horizon Start Date:%s           Horizon End Date
:%s\n",$7,$9,$11)
printf("________________________________________________________________________
_______________\n\n")
}

if ( $1 == "L" && LOOP == "1" )
{
#printf("^L")
printf("........................................................................
...............\n")
LOOP=0
}

if ( $1 == "L" )
{
LOOP=1
PARTNUM=$4
printf("ORDER #:%s      Part #:%s               Model Y.:%s   Recv. Dock:%s   ST
 Loc.:%s\n\n",$11,$4,$7,$8,$2)
}

if ( $1 == "LD" )
{
   if ( $2 == 79 && $4 == 51 )
   {
   SUM79=$3
   DATE51=$5
   }
   if ( $2 == 79 && $4 == 52 )
   {
   DATE52=$5
   }

   if ( $2 == 3 && $4 == 51 )
   {
   SUM3=$3

DATE351=$5
   }
   if ( $2 == 3 && $4 == 11 )
   {
   DATE11=$5
   }
PREV_LINE=$1
}
if ( $1 == "D" && PREV_LINE == "LD" )
{
   printf("Previous Cumm. Quantity:%s           Horizon Start:%s        Horizon
End:%s\n",SUM79,DATE51,DATE52)
   printf("Cumulative Quantity    :%s           Horizon Start:%s        Dispatch
 Date:%s\n",SUM3,DATE351,DATE11)
PREV_LINE=$1
print "\n\nFORECAST           PERIOD    DATE REQUIRED               QUANTITY"
print "======================================================================"

}

if ( $1 == "D" )
{
   if ( $2 == 1 && $3 == "W")
   printf("Firm                Weekly   %s                    %10s\n",$5,$7)
if ( $2 == 4 && $3 == "W")
   printf("Planning            Weekly   %s                    %10s\n",$5,$7)

   if ( PRINT == 0 && ($2 =="" || $3 =="" ))
   {
   PRINT=1
   printf("======================== End of the Item %s =======================\n
",PARTNUM)
  }

}

}

Regards,
Inder

It would be eaiser to follow the program logic with a sample of the input file. Perhaps you could post the input file that generated the sample report above.

The following is the program:

BEGIN { FS = "|"
LOOP=0
PRINT=0
}

{
if ( $1 == "H" )
{
printf(" GM DELFOR \n\n")
printf("Document #:%s Material Release #:%s DUNS #:%s\n",$6,$15,$13)
printf("Doc. Date :%s Horizon Start Date:%s Horizon End Date:%s\n",$7,$9,$11)
printf("_______________________________________________________________________________________\n\n")
}

if ( $1 == "L" && LOOP == "1" )
{
#printf("
")
printf(".......................................................................................\n")
LOOP=0
}

if ( $1 == "L" )
{
LOOP=1
PARTNUM=$4
printf("ORDER #:%s Part #:%s Model Y.:%s Recv. Dock:%s ST Loc.:%s\n\n",$11,$4,$7,$8,$2)
}

if ( $1 == "LD" )
{
if ( $2 == 79 && $4 == 51 )
{
SUM79=$3
DATE51=$5
}
if ( $2 == 79 && $4 == 52 )
{
DATE52=$5
}

if ( $2 == 3 && $4 == 51 )
{
SUM3=$3
DATE351=$5
}
if ( $2 == 3 && $4 == 11 )
{
DATE11=$5
}
PREV_LINE=$1
}
if ( $1 == "D" && PREV_LINE == "LD" )
{
printf("Previous Cumm. Quantity:%s Horizon Start:%s Horizon End:%s\n",SUM79,DATE51,DATE52)
printf("Cumulative Quantity :%s Horizon Start:%s Dispatch Date:%s\n",SUM3,DATE351,DATE11)
PREV_LINE=$1
print "\n\nFORECAST PERIOD DATE REQUIRED QUANTITY"
print "======================================================================"

}

if ( $1 == "D" )
{
if ( $2 == 1 && $3 == "W")
printf("Firm Weekly %s %10s\n",$5,$7)
if ( $2 == 4 && $3 == "W")
printf("Planning Weekly %s %10s\n",$5,$7)

if ( PRINT == 0 && ($2 =="" || $3 =="" ))
{
PRINT=1
printf("======================== End of the Item %s =======================\n",PARTNUM)
}

}

}

The input data! We don't need a 2nd copy of the program.

Hi Perderabo,

I am sorry for uploading the program twice:

Here is the input data

UNB+UNOA:2+BFT:ZZ+A9J:ZZ+060205:1004+2000221++GMDELFOR
UNH+2000221001+DELFOR:D:97A:UN
BGM+241:::PS+07+5
DTM+137:20060205:102
DTM+158:20060205:102
DTM+159:20060624:102
FTX+AAI+++PLNNG RLSE 30 WILL SHOW BUILD WK RLSE 31 WILL SHOW MRD
NAD+SU+085397805::16++AROMAT CORP
NAD+MI+88835::92
GIS+37
NAD+ST+19054::92
LIN+++15294047:IN
PIA+1+6:RY
LOC+11+RYD
RFF+ON:15WZ0004
QTY+79:0:EA
DTM+51:20051225:102
DTM+52:20060204:102
QTY+3:0:EA
DTM+51:20051225:102
DTM+11:20060205:102
SCC+4++W:21
QTY+1:1:EA
DTM+158:20060508:102
QTY+1:1:EA
DTM+158:20060515:102
SCC+2
QTY+3:0:EA
DTM+51:20051225:102
DTM+52:20060319:102
SCC+3
QTY+3:0:EA
DTM+51:20051225:102
DTM+52:20060319:102
UNT+34+2000221001
UNZ+1+2000221
UNB+UNOA:2+BFT:ZZ+A9J:ZZ+060205:1004+2000220++GMDELFOR
UNH+2000220001+DELFOR:D:97A:UN
BGM+241:::PS+07+5
DTM+137:20060205:102
DTM+158:20060205:102
DTM+159:20060624:102
NAD+SU+085397805::16++AROMAT CORP
NAD+MI+88835::92
GIS+37
NAD+ST+18003::92
LIN+++15117110:IN
PIA+1+6:RY
LOC+11+DDD
LOC+159+2G5-F-G
RFF+ON:15WZ0000
QTY+79:3945:EA
DTM+51:20051225:102
DTM+52:20060204:102
QTY+3:3945:EA
DTM+51:20051225:102
DTM+11:20060203:102
SCC+1++W:21
QTY+1:810:EA
DTM+158:20060206:102
QTY+1:542:EA
DTM+158:20060213:102
SCC+2
QTY+3:5297:EA
DTM+51:20051225:102
DTM+52:20060219:102
SCC+3
QTY+3:5297:EA
DTM+51:20051225:102
DTM+52:20060305:102
PAC+++KC323034
QTY+52:15:EA
LIN+++15856412:IN
PIA+1+6:RY
LOC+11+DDD
LOC+159+2F6-A
RFF+ON:15WZ0006
QTY+79:0:EA
DTM+51:20051225:102
DTM+52:20060204:102
QTY+3:0:EA
DTM+51:20051225:102
DTM+11:20060205:102
SCC+1++W:21
QTY+1:105:EA
DTM+158:20060213:102
SCC+4++W:21
QTY+1:840:EA
DTM+158:20060220:102
QTY+1:1170:EA
DTM+158:20060227:102
QTY+1:1200:EA
DTM+158:20060306:102
QTY+1:1740:EA
DTM+158:20060313:102
QTY+1:1110:EA
DTM+158:20060320:102
QTY+1:1155:EA
DTM+158:20060327:102
QTY+1:825:EA
DTM+158:20060410:102
QTY+1:690:EA
DTM+158:20060417:102
QTY+1:930:EA
DTM+158:20060424:102
QTY+1:1365:EA
DTM+158:20060501:102
QTY+1:1365:EA
DTM+158:20060508:102
QTY+1:1215:EA
DTM+158:20060515:102
QTY+1:1035:EA
DTM+158:20060522:102
QTY+1:855:EA
DTM+158:20060529:102
SCC+2
QTY+3:91:EA
DTM+51:20051225:102
DTM+52:20060219:102
SCC+3
QTY+3:2109:EA
DTM+51:20051225:102
DTM+52:20060305:102
PAC+++KC323034
QTY+52:15:EA
UNT+89+2000220001
UNZ+1+2000220

And, another thing is that I have the input data like:

QTY+1:105:EA
DTM+158:20060213:102

so, in the report under date required it should show 20060213 but it is showing 20060315.

It is showing the wrong date only for this part number and for all the other part numbers the date is correct.

So I don't know what I am doing wrong?

Regards,
Inder

Hi,

Can anybody please provide any insight, what I am doing wrong?

Thanks,
Inder

This does not look like the input file. The field seperator has been set to | in your awk script. The input data does not have any | symbols to seperate the fields.

Hi Rajesh,

I know that in the input file there are no "| signs. But this is the input file which generates that report. If you want, I can upload the shell script too which is calling this awk script and generating output file.

Actually, I am not the author of all these scripts.

Regards,
Inder

if [ -f 830gm.dat ]
then
/usr/bin/awk -f $BIN_DIR/gm_delfor.awk 830gm.dat >gm_delfor.doc
mpack -s "GM's DELFOR" ./gm_delfor.doc isingh@xyz.com
mv 830gm.dat 830gm.$wdate
mv gm_delfor.doc gm_delfor.$wdate
fi

-Inder

Hi Rajeeb,

Thanks a lot for pointing my in the right direction!!

After I wrote my previous messages, I have gone through the script one more time and found that the input file is not the same as I uploaded before.

The one problem has been resolved.

Thanks guys!!!

-Inder

Could you upload the actual input data file. I want to have a look at it. I am a big time fan of sed and awk. I would like to see what we can do with your script.

Hi Rajeeb,

I am attching here the input file for your review:

Thanks a lot for pointing me in the right direction. The problem with the wrong date has been resolved.

H|BFT|20060205|DELFOR||07|20060205|137|20060205|158|20060624|159|085397805||88835
L|19054||15294047|||6|RYD|0|ON|15WZ0004
LD|79|0|51|20051225
LD|79|0|52|20060204
LD|3|0|51|20051225
LD|3|0|11|20060205
D|4|W|158|20060508|1|1
D|4|W|158|20060515|1|1
D|2||51|20051225|3|0
D|3||52|20060319|3|0
D|||51|20051225
D|||52|20060319
H|BFT|20060205|DELFOR||07|20060205|137|20060205|158|20060624|159|085397805||88835
L|18003||15117110|||6|DDD|2G5-F-G|ON|15WZ0000
L|18003||15856412|||6|DDD|2F6-A|ON|15WZ0006
LD|79|3945|51|20051225
LD|79|3945|52|20060204
LD|3|3945|51|20051225
LD|3|3945|11|20060203
LD|79|0|51|20051225
LD|79|0|52|20060204
LD|3|0|51|20051225
LD|3|0|11|20060205
D|1|W|158|20060206|1|810
D|1|W|158|20060213|1|542
D|2||51|20051225|3|5297
D|3||52|20060219|3|5297
D|||51|20051225
D|1|W|52|20060305|1|105
D|4|W|158|20060213|1|840
D|4|W|158|20060220|1|1170
D|4|W|158|20060227|1|1200
D|4|W|158|20060306|1|1740
D|4|W|158|20060313|1|1110
D|4|W|158|20060320|1|1155
D|4|W|158|20060327|1|825
D|4|W|158|20060410|1|690
D|4|W|158|20060417|1|930
D|4|W|158|20060424|1|1365
D|4|W|158|20060501|1|1365
D|4|W|158|20060508|1|1215
D|4|W|158|20060515|1|1035
D|4|W|158|20060522|1|855
D|2||158|20060529|3|91
D|||51|20051225
D|3||52|20060219|3|2109
D|||51|20051225
D|||52|20060305