Running Total


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running Total
# 1  
Old 08-06-2007
Running Total

HPUX 11i v2 #!/bin/sh

Hi all.

I have a space delimited flat file of about 9000 lines. I would like to get a running total of field 3 to the variable $TOTAL. Field 3 can be formatted as listed....

$ 0.00
$2804.15
<$ 4.14> (negative)

Any ideas are most appreciated!!!! TIA!!
# 2  
Old 08-06-2007
Code:
total=`awk '{ c+=$3 }END{ print c }' filename`

not sure whether you are looking for this one !
# 3  
Old 08-06-2007
Quote:
Originally Posted by lyoncc
I have a space delimited flat file of about 9000 lines. I would like to get a running total of field 3 to the variable $TOTAL. Field 3 can be formatted as listed....
$ 0.00
$2804.15
<$ 4.14> (negative)
Any ideas are most appreciated!!!! TIA!!
Lyoncc,
Your requirement needs more clarification:
1) You say you have a space delimited file, but you display field 3 with
space, "$ 0.00".
2) How would negative numbers be stored in field 3?
# 4  
Old 08-06-2007
Yes you're right

I did say space delimited...that's wrong, because field 3 can be $ 0.00 or even $ 0.00. Negatives can be <$ 0.75> or <$ 0.75>. So basically, the whitespace after the $ is variable in length.

data example:

*****4194 0100 $ 615.09 Raines, John
*****9810 0100 <$ 1.07> Yip, Albert A
*****0719 0100 $ 84.58 Yu, Epah

TIA!!!!!
# 5  
Old 08-06-2007
Code:
typeset -i mSum=0
typeset -R1 mLast
while read m1 m2 m3 mValue mN
do
  mValueNbr=`echo ${mValue} | sed 's/[^0-9]//g'`
  mLast=${mValue}
  if [ "${mLast}" = ">" ]; then
    mNbr='-'${mValueNbr}
  else
    mNbr=${mValueNbr}
  fi
  mSum=${mSum}+${mNbr}
done < input_file
mSumMask=`echo ${mSum} | sed 's/..$/.&/g'`
echo 'Sum = '${mSumMask}

# 6  
Old 08-07-2007
this takes care of the negative values Smilie

Code:
awk '{ if ( match($3, "$") ) { if ( match($4, ">") ) { cnt-=substr($4, 0, index($4, ">") - 1) } else { cnt+=$4 } } }END{ print cnt }' filename

# 7  
Old 08-07-2007
I wrote me a perl script for just this sort of thing,
any use?

Code:
#!/usr/bin/perl -ws

sub usage()
{
    print <<"EOF";

# tots up all numbers in a tabular file
# assumes first column is the number to
# be totalled unless specified with -col=n
#
# e.g. try: 'du -s * | total'
#
# or
#
# ls -l | total -col=5
EOF
    die "\n";
}

$h ||= undef;
usage if $h;

$col ||=1;
$col--;

while (<>) {

    chomp;

    next unless /./; # skip blanks
    $number = (split)[$col];
    $total += $number;
    print;
    print " ($number)\n";

} 

print STDOUT "\n" , "=" x length($total), "\n$total\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sub TOTAL Columns

Input 435,BL_lmapm03,rrr,RDF1+TDEV,0cef,45,mask1 435,BL_lmapm03,rrr,TDEV,080a,50,mask2 435,BL_lmapm02,fff,RDF1+TDEV,0ceg,45,mask4 435,BL_lmapm02,fff,TDEV,080b,60,mask6 435,BL_lmapm06,hhh,TDEV,080f,60,mask9 Output 435,BL_lmapm03,rrr,RDF1+TDEV,0cef,45,mask1... (4 Replies)
Discussion started by: greycells
4 Replies

2. Solaris

Total

i want to list Total HDD count in any soalris machine..can someone suggest some commands or combinations of commands (6 Replies)
Discussion started by: omkar.jadhav
6 Replies

3. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

4. Shell Programming and Scripting

Print available running instance out of total

Hello. I have a status command in AIX box, which provides output as below: $ status You are running the application on pegasus2 ----Program Name------|--Avail / Total---------| MQ | 1/2 | ORACLE | 10/10 | TMADMIN ... (3 Replies)
Discussion started by: panchpan
3 Replies

5. Shell Programming and Scripting

Running Total Running Wild

Hi. A shell scripting newbie here. I am trying to write a script that will create a running total of Sales, and increment a counter for each Sales entry, but when I executed the program it never stopped. counter=0 Sales=0 echo "enter sales price" read sales while do let counter=counter+1... (6 Replies)
Discussion started by: Ccccc
6 Replies

6. 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

7. UNIX for Dummies Questions & Answers

du total

Hi All Can anyone help me with the following du querry. I am trying to achieve a total size for all the zipped files in a directory. Using du -k *.gz gets me a file by file list but no handy total at the bottom. Thanks Ed (9 Replies)
Discussion started by: C3000
9 Replies

8. 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

9. UNIX for Advanced & Expert Users

How to prevent job1 from running while job2 is running..

Hi, Please I need your expert advise on how to prevent/lock from execution job1 while job2 is still running in Unix... THanks:) (3 Replies)
Discussion started by: tikang
3 Replies
Login or Register to Ask a Question