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 03-10-2011, 12:55 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
arithmetic operation in bash


Hi,
I've the following input file (sorted.txt)

Code:
A 1
A 1
A 1
B 3
B 2
and I want to sum all the numbers by letter and put it in an array. I want the following result:
Code:
A 3
B 5
I've created this script:

Code:
cat sorted.txt | while read line
do
    KEY=$(echo "$line" | awk '{ print $1 }');
    VALUE=$(echo "$line" | awk '{ print $2 }');

    if [[ -n map[$KEY] ]]
    then
        old=map[$KEY];
        map[$KEY]=$old+$VALUE;
    else
        map[$KEY]=$VALUE;
    fi
done
But the problem is that it doesn't work. What's wrong?
 
Old 03-10-2011, 03:46 PM   #2
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Code:
map[$KEY]=$old+$VALUE;
should read

Code:
map[$KEY]=$(( $old+$VALUE ));
 
Old 03-10-2011, 03:55 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
As tsg said. The $ sign is not necessary inside the arithmetic operator:
Code:
map[$KEY]=$(( old+VALUE ));
An alternative using only awk:
Code:
awk '{map[$1]+=$2}END{for (i in map)print i, map[i]}' sorted.txt
 
Old 03-10-2011, 03:58 PM   #4
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
As tsg said. The $ sign is not necessary inside the arithmetic operator:
This is true. I generally add it out of habit for clarity, though.
 
Old 03-10-2011, 04:30 PM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Ok, thanks for the help.
I'm going to give another hint:

Bash spawns a new sub-shell when piping commands together. Since Bash is picky about scoping variables to sub-shells, my script doesn't work like I expected it to.

Using this "while" will help a lot with the rest of the script.

Code:
while read line
do
let COUNT=$COUNT+1
done < filename.txt
echo $COUNT
 
Old 03-10-2011, 04:33 PM   #6
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
This will work.

Code:
while read line
do
    KEY=$(echo "$line" | awk '{ print $1 }');
    VALUE=$(echo "$line" | awk '{ print $2 }');

    old=${map[$KEY]};
    map[$KEY]=$(( old+VALUE ))
done < A.txt
 
Old 03-10-2011, 08:12 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Quote:
This will work.
True, but unless the data is more complicated I would suggest either using bash or awk, it would seem pointless to use both.
colucix has already provided an appropriate awk script.
As for all bash:
Code:
#!/bin/bash

declare -A map

while read -r KEY VALUE
do
    (( map[$KEY] += VALUE ))
done<A.txt
 
1 members found this post helpful.
  


Reply


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
How to make arithmetic operation of data file with script file? mauludi Linux - Newbie 5 09-01-2010 06:26 AM
Arithmetic operation with awk fugitive569 Programming 2 12-04-2009 11:10 AM
display after arithmetic operation tostay2003 Programming 7 08-14-2008 12:51 PM
Bash arithmetic Blackout_08 Programming 2 06-08-2006 11:37 PM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 03:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration