Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-03-2010, 12:40 AM
|
#1
|
|
Member
Registered: May 2009
Posts: 32
Rep:
|
looping thru input files with different number of days.
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
Last edited by btacuso; 11-03-2010 at 12:46 AM.
|
|
|
|
11-03-2010, 04:43 AM
|
#2
|
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,051
Rep:
|
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";'
hmm guess that really shouldn't be a one-liner, note I didn't test that at all (too early in the morning)
|
|
|
|
11-03-2010, 10:19 AM
|
#3
|
|
Member
Registered: May 2009
Posts: 32
Original Poster
Rep:
|
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.
|
|
|
|
11-03-2010, 05:30 PM
|
#4
|
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,051
Rep:
|
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
or spread out as a regular script
Code:
#!/usr/bin/perl
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";
|
|
|
|
11-03-2010, 06:10 PM
|
#5
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
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
With small changes this can be extended to sum up the other fields, too.
|
|
|
|
11-03-2010, 08:28 PM
|
#6
|
|
Member
Registered: May 2009
Posts: 32
Original Poster
Rep:
|
Quote:
Originally Posted by estabroo
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
or spread out as a regular script
Code:
#!/usr/bin/perl
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";
|
Thanks. I will keep this as a reference if I switch to perl.
|
|
|
|
11-03-2010, 08:33 PM
|
#7
|
|
Member
Registered: May 2009
Posts: 32
Original Poster
Rep:
|
Quote:
Originally Posted by crts
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
With small changes this can be extended to sum up the other fields, too.
|
Yes, I want those plus the sum total. I might receive a file which has a day or more transactions. I will try to figure out how to make a loop out of your sample. Thanks.
|
|
|
|
11-03-2010, 10:22 PM
|
#8
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Quote:
Originally Posted by btacuso
Yes, I want those plus the sum total. I might receive a file which has a day or more transactions. I will try to figure out how to make a loop out of your sample. Thanks.
|
The point is that you do not need a loop. awk processes every line of the file. If it finds the search pattern in a line then field number for is added to the variable tax. It does not matter how many days there are. Simply add the other patterns (ETAX, PROD) and add to variables etax and prod when the corresponding pattern is encountered. In the END block you can then still calculate the sum total like
print "total: "prod+tax+etax
Just try it and observe the output.
|
|
|
|
11-03-2010, 11:54 PM
|
#9
|
|
Member
Registered: May 2009
Posts: 32
Original Poster
Rep:
|
Quote:
Originally Posted by crts
The point is that you do not need a loop. awk processes every line of the file. If it finds the search pattern in a line then field number for is added to the variable tax. It does not matter how many days there are. Simply add the other patterns (ETAX, PROD) and add to variables etax and prod when the corresponding pattern is encountered. In the END block you can then still calculate the sum total like
print "total: "prod+tax+etax
Just try it and observe the output.
|
It worked. Now I can close this thread. Thanks again.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:17 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|