Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-24-2011, 03:17 PM   #1
LQ Newbie
Registered: Oct 2011
Posts: 20

Rep: Reputation: Disabled
Post 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
Old 11-24-2011, 03:53 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Why would you want to avoid awk? This is a perfect application for it:

awk '{ array[$1] += $2 } END{ for ( i in array ) { print i , array[i] } }' file
I also don't understand your reference to getopt. What does that have to do with it?

Anyway, using the same basic logic as the above awk command, and bash v4's associative arrays:

declare -A array

while read name value; do

     array[$name]=$( echo "${array[$name]:-0} + $value" | bc )

done < file

for name in "${!array[@]}"; do

     echo "$name ${array[$name]}"


Last edited by David the H.; 11-24-2011 at 03:56 PM. Reason: made variable names more natural
1 members found this post helpful.
Old 11-24-2011, 03:53 PM   #3
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
This Bash 4.0+ script using an associative array seems to work. Could probably be simpler.


declare -A map

while read line; do
    key=${line%% *}
    value=${line##* }
    [ -z "${map[$key]}" ] && map[$key]=0
    map[$key]=$(bc <<< "${map[$key]} + $value")
done < $@

for person in ${!map[*]}; do
    echo -e $person\: ${map[$person]}
EDIT: Oops, yea, David's is cleaner.


bash scripting, shell scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script using getopt to parse optional arguments nano2 General 6 04-28-2011 08:09 AM
[SOLVED] bash and arrays disca Programming 3 07-27-2010 08:26 AM
[SOLVED] Bash: all variables are arrays. Why? catkin Programming 5 09-09-2009 08:52 AM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
bash arrays question introuble Programming 1 05-20-2006 03:07 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration