looping thru input files with different number of days.
1 Attachment(s)
I want to itemize addition for each type of transactions. The example below is for 3 days,10/13/2010, 10/14/2010, & 10/15/2010.
PROD =89.25+89.25+104.47= 282.97 TAX =9.37+9.37+10.37 = 29.71 ETAX = 1+1+1 = 3 Total = 315.68 My problem is how do I make a loop so that the script can parse thru all the items on all dates if the input file have variable transaction days? What I mean is that it might be 1 day only, or 2 days, or 10 days. Please see attachment for easier to read file(notepad). Date Trans ITEM_NO Debit Credit Balance Oct13'10 PROD 328 89.25 89.25 Oct13'10 TAX 328 9.37 98.62 Oct13'10 ETAX 328 1.00 99.62 Oct14'10 PROD 328 89.25 188.87 Oct14'10 TAX 328 9.37 198.24 Oct14'10 ETAX 328 1.00 199.24 Oct15'10 PROD 328 104.47 303.71 Oct15'10 TAX 328 10.97 314.68 Oct15'10 ETAX 328 1.00 315.68 Oct16'10 TOTAL 328 315.68 CR 0.00 |
depends on the language you use, for perl a while(<>) would work nicely
Code:
perl -e 'while(<>) { chomp; @info = split(/ /); push $data{$info[1]}, $info[-1]; }; $total = 0; print "$item = "; foreach $item (keys(%data)) { $ttot = 0; foreach $val (@{$data{$item}) { $ttot += $val; print "$val +"; } print " = $ttot\n"; $total += $ttot; } print "Total = $total\n";' |
I would prefer a script in bash/awk where I have already a little background, but yes, I am also interested how it works in other languages. Thanks.
|
Sorry I don't know awk well enough to help out, but here is that one liner corrected and working
Code:
perl -e 'while(<>) { chomp; @info = split(" "); next if $info[1] \!~ /PROD|TAX|ETAX/; push @{$data{$info[1]}}, $info[-2] }; $total = 0 ; foreach $item ( keys ( %data ) ) { $ttot = 0; $i=0; $size = @{$data{$item}}-1; print "$item = "; foreach $val ( @{$data{$item}} ) { $ttot += $val ; print "$val"; print " + " if $i < $size; $i++; } print " = $ttot\n" ; $total += $ttot ; } print "Total = $total\n" ; ' sample.txt Code:
#!/usr/bin/perl |
Hi,
if I understand correctly, then you just want the total of the fields PROD, TAX and ETAX, right? It does not really matter how many days there are. So you just need to add the numbers of the according field. Here is an awk example that will do it for the TAX field: Code:
awk '/\<TAX\>/{tax+=$4}END{print tax}' sample.txt |
Quote:
|
Quote:
|
Quote:
print "total: "prod+tax+etax Just try it and observe the output. |
Quote:
|
All times are GMT -5. The time now is 07:17 AM. |