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_fileSort the file by the product id with the command:
sort unix_file > sortedUse 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
doneEnsure 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