![]() |
bc,getopt and arrays in bash
trying to sum elements in an array using bc and getopt,i have a file with names and thier vaules if the names appears 3 times i should multiply its value with 3 then find the sum of all the elements together
cat foo.txt max 2.3 henry 3 fransis 4.5 max 2.3 henry 3 max 2.3 it should show on the terminal max 6.9 henry 6 fransis 4.5 and so on im trying to avoid awk |
Why would you want to avoid awk? This is a perfect application for it:
Code:
awk '{ array[$1] += $2 } END{ for ( i in array ) { print i , array[i] } }' fileAnyway, using the same basic logic as the above awk command, and bash v4's associative arrays: Code:
declare -A array |
This Bash 4.0+ script using an associative array seems to work. Could probably be simpler.
Code:
#!/bin/bash |
| All times are GMT -5. The time now is 05:24 PM. |