compute total from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compute total from a text file
# 1  
Old 01-03-2006
compute total from a text file

Hi,

I've encountered a problem with a perl and ksh script that totals a certain field in a text file. The computed total of the script is 295540304 but the expected is 297959288, a 2 million difference. The KSH script reads from bottom to top, and the discrepancy started on line 47 (1279th MAN segment from the bottom)

PERL script:
my $Sum;
my $addend1 = $ARGV[0];
my $addend2 = $ARGV[1];

$Sum = $addend1 + $addend2;
print $Sum;

KSH script:
TheSum=0
i=1
while [ $i -le 1302 ]
do
Addend=$(cat test.20051219101007.fin | grep MAN | cut -c 18-32 | tail -$i)
TheSum=$(ComputeSUM.pl $TheSum $Addend)
echo $TheSum
i=`expr $i + 1`
done

the test file is at:
http://www.geocities.com/styroporn/t...1219101007.zip

not sure why there was a discrepancy, but it started at line 1279. i tried it using values of 50000, to check if there is a limitation to max value but it computed perfectly

please help
# 2  
Old 01-03-2006
Your script has a lot of problems. Try this:
Code:
#/usr/bin/ksh
TheSum=0
while read Code Addend ; do
        [[ $Code = MAN* ]] && ((TheSum=TheSum+Addend))
done < test.20051219101007.fin
echo $TheSum
exit 0
$ ./scr2
297959288
$

The particular problem that attracted your attention is caused by the statement that starts out "Addend=$(". As you crawl from the end of the file towards the beginning, you are trying to store more and more data in the variable Addend. Eventually you overflow it and get unpredictable results.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Compute average ignoring outliers of different segments within a dat file using awk

I have data files that look like this, say data.txt 0.00833 6.34 0.00833 6.95 0.00833 7.08 0.00833 8.07 0.00833 8.12 0.00833 8.26 0.00833 8.70 0.00833 9.36 0.01667 20.53 0.01667 6.35 0.01667 6.94 0.01667 7.07 0.01667 8.06 0.01667 8.10 0.01667 8.25 0.01667 8.71 0.01667 9.31... (7 Replies)
Discussion started by: malandisa
7 Replies

3. Shell Programming and Scripting

Compute in milisecond by use of mktime

Hi, I want to calculate diff b/w these starttime and endtime with use of mktime. I need response time in milisecond. I am using mktime to get these times. last three digits are in milisecond Starttime 2013-04-03 08:54:19,989 End time 2013-04-03 08:54:39,389 (9 Replies)
Discussion started by: random_thoughts
9 Replies

4. Shell Programming and Scripting

Total of a column from a file

Hi i need to calculate the total of a column from a file in ksh vi file.txt System : CBSE ent=0.1 me=Cap Subject Maths Science xxxxx 56 98 yyyy 89 67 ooo 67 32 Here i need to calculate only the total of Maths column alone i.e., 56+89+67 ... (4 Replies)
Discussion started by: Priresh
4 Replies

5. Shell Programming and Scripting

Output no. of rows and total value of a file to a new file

I am relatively new to Unix and I would be grateful if someone could help me with the syntax of a script. I am trying to write a Unix script against the file ‘ajtest.dat’ (see below) so that it outputs the number of rows and total value of column 3 in a file called ‘aj1’ as follows: No. of... (11 Replies)
Discussion started by: ajmutley
11 Replies

6. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

7. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

8. Shell Programming and Scripting

to compute diskspace

Guys, have any idea for the script like this? also to compute w/ decimal. thanks a=10 b=20 c=30 d=40 if a < b then ( a -b)*1024 = free space b + (c -d) = total space if a > b then (b / d)*1024 = cpu (3 Replies)
Discussion started by: kenshinhimura
3 Replies

9. UNIX for Dummies Questions & Answers

grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to... (5 Replies)
Discussion started by: MrAd
5 Replies
Login or Register to Ask a Question