arithmetic operation in bash
Hi,
I've the following input file (sorted.txt) Code:
A 1 Code:
A 3 Code:
cat sorted.txt | while read line |
Code:
map[$KEY]=$old+$VALUE; Code:
map[$KEY]=$(( $old+$VALUE )); |
As tsg said. The $ sign is not necessary inside the arithmetic operator:
Code:
map[$KEY]=$(( old+VALUE )); Code:
awk '{map[$1]+=$2}END{for (i in map)print i, map[i]}' sorted.txt |
Quote:
|
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 |
This will work.
Code:
while read line |
Quote:
colucix has already provided an appropriate awk script. As for all bash: Code:
#!/bin/bash |
All times are GMT -5. The time now is 09:32 PM. |