Math

i have file (my_file.txt) that looks like this:

 000000000000010000
 000000000000010000
 000000000000005000
 000000000000005000
 000000000000005000
 000000000000005000
 000000000000005000
 000000000000005000
 000000000000005000
 000000000000005000

all said and one, it should look like this $600.00

how do i add them up to total of $600.00 ?

can i use sed, perl or bc ? i don't even know where to start.

Thanks,

Yes, lots of ways to do this. One example in Perl (not using error checking to keep it simple):

open(IN, "<my_file.txt");
while($in = <IN>)
{
   $sum += $in;
}
$sum =~ /(\d+?)(\d\d)$/;
$dollars = $1;
$cents = $2;
print "\$$dollars.$cents\n";

all the choices you mention will work.

awk '
{
  val=$0 + 0
  sum=sum+val
}
END {
  l=length(sum)
  if (l > 2) 
    printf("$%s.%s\n",substr(sum,1,(l-2)),substr(sum,(l-1)))
  else
    printf("$0.%s\n",sum)
}' <my_file.txt

(smells like homework)

---------- Post updated at 10:42 AM ---------- Previous update was at 10:39 AM ----------

That's not going to handle cents properly. Just saying. 50 cents + 50 cents is not 100 cents, it's a dollar.

If this is intended to be arbitrarily accurate, you should use bc. awk uses floating point numbers and may lose a penny here and there.

As in the Perl example, just use integer arithmetic and you'll be fine. Separate dollars from cents at the end of your script.

Thanks y'all so much.

Change printf after the else to use %02d instead of %s.

What would be wrong with a simple

awk '{sum+=$0} END {printf "$%.2f\n", sum/100}' file
$600.00

(except for the caveat that corona688 pointed out)?

Hi.

Also utility numsum , part of:

num-utils - programs for dealing with numbers from the command line

in systems like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.0 (jessie)

For example, assuming numbers are in file data3,

numsum data3

produces:

60000

Best wishes ... cheers, drl

Floating point math. Good enough for some, not for others :slight_smile:

FP varies so much and there are many different variables....

Avoid and you'll pass an audit easier!

A=`cat my_file.txt | awk '{sum+=$1} END { print sum}'`
B=$(echo "scale=2; $A / 100" | bc)

echo "$"$B

Using integers isn't the only point here. The size of the sum is what matters in this problem. Since awk uses double precision floating point arithmetic (even when processing integers), you can have a problem if the accumulated sum (in cents) is 15 or more digits (i.e., more than $999,999,999.99). So, if your final result is less than 1 billion dollars, RudiC's script should be fine. If your result could be bigger, you can use shell and bc or dc to get the results you want no matter how big the sum is:

#!/bin/ksh
(echo '2k0[$]P'; while read line
	do	echo " $line"
	done < my_file.txt
echo '100/pq') | dc

printf '$'
(echo 'a=0;scale=2'; while read line
	do	echo "a+=$line"
	done < my_file.txt
	echo 'a/100'
) | bc

On most UNIX and UNIX-like systems, bc is a front-end to dc . Therefore, I tend to use dc instead of bc to avoid creating the extra process. But, bc is in the standards and dc isn't. So, if your system doesn't have dc or you want your script to be portable to systems that might not have dc , use bc .

Both sections of the above script print:

$600.00

when your sample input is contained in my_file.txt .