Technical article 114623

How can I use the shell (sh) to sum some values in a file?

OpenServer 6.0.0OpenServer 5.0.0OpenServer 5.0.2OpenServer 5.0.4OpenServer 5.0.5OpenServer 5.0.6OpenServer 5.0.7UnixWare 7.0.0UnixWare 7.0.1UnixWare 7.1.0UnixWare 7.1.1UnixWare 7.1.3UnixWare 7.1.4

Written · Last revised

Problem

I have a flat file containing two fields consisting of a product id
and a value.  How can I use the shell to sum all the product id
giving a total of each product id range?

Solution

The input file is in the format:

004839-001^400.000 005555-101^3.000 005555-101^310.000 006534-001^20.000 007340-002^10.000 007349-001^7.000 007400-002^4.000 007463-001^840.000 007820-001^1.000 007829-002^3560.000 007856-001^90.000 008123-103^156.000 008284-003^3.000 008312-101^5.000

Ensure the file has no carriage return/line feeds by using the command:

dtox input_file.txt > unix_file

Sort the file by the product id with the command:

sort unix_file > sorted

Use the following script to generate a totals file:

:
> totals
C=0
LAST="`head -1 sorted | cut -d "^" -f 1`"
cat sorted | while read i
do
        A=`echo $i | cut -d "^" -f 1`
        B=`echo $i | cut -d "^" -f 2 | cut -d "." -f 1`
        if [ "${A}" != "${LAST}" ]; then
                echo "$LAST,$C" >> totals
                C=0
        fi
        D=`expr ${C} + ${B}`
        C=$D
        LAST=$A
done

Ensure that the script is executable with the command:

chmod 755 <script>

The totals file will be in the format:

004839-001,400 005555-101,313 006534-001,20 007340-002,10 007349-001,7 007400-002,4 007463-001,840 007820-001,1 007829-002,3560 007856-001,90 008123-103,156 008284-003,3 008312-101,5

Note that there is only one 005555-101 product id and it is the sum of the two original fields.

There are other ways the script can be written.  However, this script
demonstrates the use of the "expr" command to perform simple
calculations on flat files.
NOTE:     This script was run in the korn shell (ksh).
TIPS:     I want the first letter of a variable A?
# echo $A | cut -c1

See also

The man pages of:
  expr
  while
  if

From the Xinuos technical article database, record 114623. Reproduced with permission. The original is at www.sco.com/ta/114623, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.