Use of scale in bash

1

I'm trying to print an associative vector with decimals on the screen but I can not find a way to do it:

#!/bin/bash

cantidades="cantidades.csv"
line=20
declare -A sumaCantidad
declade -A media
declare -A numeroCantidades
for i in 'seq 2 $line'
do
NOMBRE='cat $cantidades | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f4'
CANTID='cat $cantidades | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f5'
sumaCantidad[$NOMBRE]=$((sumaCantidad[$NOMBRE]+CANTID))
numeroCantidades[$NOMBRE]=$((numeroCantidades[$NOMBRE]+1))
done
for i in 'seq 2 $line'
do
NOMBRE='cat $cantidades | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f4'
media[$NOMBRE]={sumaCantidades[$NOMBRE]}/${numeroCantidades[$NOMBRE]}
echo "$media[$NOMBRE]}"
done

What happens is that when printing the media only prints the whole number, without decimals, and I want you to do the operation with decimals. How could I do it?

The file looks like this

[...],[...],[...],NOMBRE,CANTIDAD

0,0,0,Juan,4

0,0,0,Juan, 7

0,0,0,Jose,10

0,0,0,Maria,4

0,0,0,Maria,6

etc

    
asked by Chariot 24.10.2018 в 23:47
source

2 answers

3

To your comment, imagine that you want to add for each person, how many values and the average, yes, you could, but I think you get too complicated. The awk can differ depending on the Linux / UNIX version. I've done it on Mac, so if you try on ubintu you may have to change something. I have recreated a txt file with some data and an awk file where the code goes

The txt where we have data is called hello.txt

a:s:d:Pepe:4
a:s:d:Manolo:10
a:s:d:Pepe:12
a:s:d:Pepe:14
a:s:d:Maria:4
a:s:d:Manolo:23

the awk where the script that has to treat that file goes, I called it src.awk

{
elem[$4] = 1
suma[$4] += $5
cant[$4]++
} END {
   print("Nombre\tSuma\tCant\tMedia")
   for (e in elem) {
      print(e, "\t", suma[e],"\t", cant[e],"\t",suma[e]/cant[e])
   }
}

and the execution:

awk -F: -f src.awk hola.txt

The awk what it does is take care of opening the data file (hello.txt) it reads it line by line and to each line it applies the script that I wrote in src.awk and it's over. With the $ number you are going to reference the column you want, and there is each value. I generate the elem array to have the names that appear, since it will be the index to save the sum and the times that appear. Look at the parameters -F for the column separator and I think that with this you gain a lot of power to treat a text file

the first 3 columns of hello.txt I have filled them with something that in principle you do not need, a bit to follow your example

I hope you've been served

    
answered by 25.10.2018 в 13:16
0

I have already found it, so that it can operate with decimals the "bc" command is used in the following way:

for i in 'seq 2 $line'
do
NOMBRE='cat $cantidades | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f4'
media[$NOMBRE]=$(echo "scale=2;${sumaCantidades[$NOMBRE]/$numeroCantidades[$NOMBRE]}" | bc)
echo "$media[$NOMBRE]}"
done
    
answered by 26.10.2018 в 12:05