LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 11-24-2011, 03:17 PM   #1
congos
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: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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] } }' 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:

Code:
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]}"

done

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
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

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

Code:
#!/bin/bash

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]}
done
EDIT: Oops, yea, David's is cleaner.
 
  


Reply

Tags
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


All times are GMT -5. The time now is 07:28 PM.

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