LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 11-03-2010, 12:40 AM   #1
btacuso
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
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
Attached Files
File Type: txt sample.txt (1.2 KB, 5 views)

Last edited by btacuso; 11-03-2010 at 12:46 AM.
 
Old 11-03-2010, 04:43 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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)
 
Old 11-03-2010, 10:19 AM   #3
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
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.
 
Old 11-03-2010, 05:30 PM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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";
 
Old 11-03-2010, 06:10 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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.
 
Old 11-03-2010, 08:28 PM   #6
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by estabroo View Post
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.
 
Old 11-03-2010, 08:33 PM   #7
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by crts View Post
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.
 
Old 11-03-2010, 10:22 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by btacuso View Post
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.
 
Old 11-03-2010, 11:54 PM   #9
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by crts View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to subtract two dates in a file to get number of days? btacuso Linux - Newbie 2 07-13-2010 11:33 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 12:12 PM
looping trough files BackwardsDown Programming 4 02-09-2007 10:39 PM
File Input, how to get number of items? ftgow Programming 1 03-11-2005 01:31 AM


All times are GMT -5. The time now is 10:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration