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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with awk 1

Status
Not open for further replies.

sabetik

IS-IT--Management
Nov 16, 2003
80
GU
Not able to get to the total for the last line. Please help


{




if(substr($0,1,1)=="H") {
# do this
#remove first Character
sub( /./, "" )




#read header
fh1 = substr($0,1,7)
fh2 = substr($0,8,7)
fh3 = substr($0,15,8)

fh4 = substr($0,23,8)
fh4a = substr($0,32,7)
fh4b = substr($0,39,7)
fh4c = substr($0,46,7)
#fh5 = substr($0,69,11)

fh6 = substr($0,60,6)

fh6a = substr($0,69,11)
fh7 = substr($0,103,12)

# total line (after each group of data line)
printf ("%-1s,%-2s,%-3s,%-4s,%-5s, %51.2f ,%7.2f \n","","","","",qty, tfd6/100,tfd7/100) > "INVOICE.xls"



printf "\n\n\n" > "INVOICE.xls"
printf ("Dealer_code,%-7s, \n", fh1) > "INVOICE.xls"
printf ("INV#,%-7s \n", fh2) > "INVOICE.xls"
printf ("date, %-8s \n", fh3 ) > "INVOICE.xls"
printf ("ETDdate,%-8s \n", fh4) > "INVOICE.xls"
printf ("G-Weight,%-8s \n", fh4a/100) > "INVOICE.xls"
printf ("N-Weight,%-8s \n", fh4b/100) > "INVOICE.xls"
printf ("Meas,%-8s \n", fh4c/100) > "INVOICE.xls"

printf ("FRGCHG,%8.2f \n",fh6a/100 ) > "INVOICE.xls"
printf ("T_order:,%8.2f \n",fh6/100) > "INVOICE.xls"
gtfh56 = fh6a + fh6
printf ("Grand_total:,%8.2f \n",gtfh56/100) > "INVOICE.xls"
printf ("Conatiner_no., %-12s \n", fh7) > "INVOICE.xls"
printf "\n" > "INVOICE.xls"





printf ("%-10s,%-10s, %-4s, %-12s, %-4s, %-8s, %-7s \n", "Case_NO."," Order_NO."," L/N"," Part_no."," Qty."," Unit Price"," Amount") > "INVOICE.xls"




tfd6 = 0
tfd7 = 0
qty = 0

# read DATA record



} else if(substr($0,1,1)=="D") {



# Removed first character

sub( /./, "" )
fd1 = substr($0,1,8)
fd2 = substr($0,9,7)
fd3 = substr($0,18,3)
fd4 = substr($0,21,12)
# Insert "-" after each group of 5 chars.
gsub( /...../, "&-", fd4 )
# Remove any trailing "-".
sub( /- *$/, "", fd4 )
fd5 = substr($0,37,5)
fd6 = substr($0,43,9)
fd7 = substr($0,53,14)
tfd6 = tfd6 + fd6
tfd7 = tfd7 + fd7
qty = qty + fd5




printf ("%-7s,%-7s,%-4s,%-15s,%-5s,%8.2f,%8.2f \n",fd1,fd2,fd3,fd4,fd5,fd6/100,fd7/100 ) > "INVOICE.xls"




}


}
[/qoute]
 
It is difficult without some sample data to test with.

Also, I haven't seen the /regexp/ syntax used with sub() and gsub() before... maybe that works? But maybe not. Try just putting the regular expressions between "" instead.

Annihilannic.
 
Also, I haven't seen the /regexp/ syntax used with sub() and gsub() before... maybe that works? But maybe not. Try just putting the regular expressions between "" instead.
No, regular expressions should be denoted by [tt]/[/tt] except when you are assigning them to variables or passing them to functions that you've defined.
 
Thanks for the reply. here few lines of test file. after run the AWK program you will see that last data doed not total.

H9806201A659450200604172006041700000000000000000000000000000004806000000000000000000000000000000000000APS SHIPMENT
DCC719367V1704X 00111111111111 00000200000024030000000000048060 78
H9806201A659785200604172006041700000000000000000000000000000027514000000000000000000000000000000000000APS SHIPMENT
DAC711323LA1304X 00022222222222 00000200000133000000000000266000 14
DAC711323V1304X 00333333333333 00000200000004570000000000009140 14
 
add this at the end of your program

END{
# total line (after last group of data lines)
printf ("%-1s,%-2s,%-3s,%-4s,%-5s, %51.2f ,%7.2f \n","","","","",qty, tfd6/100,tfd7/100) > "INVOICE.xls"
}

HTH,

p5wizard
 
Thank you with a star "*". work very well.

Kamran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top