Scale in associative arrays - bash

I'm trying to print an associative array with decimals but I don't know the way to do it:
#!/bin/bash
quantity="quantity.csv"
line=20
declare -A addQuantity
declade -A average
declare -A numberOfQuantity
for i in `seq 2 $line`
do
name=`cat $quantity | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f4`
quantity=`cat $quantity | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f5`
addQuantity[$name]=$((addQuantity[$name]+quantity))
numberOfQuantity[$name]=$((numberOfQuantity[$name]+1))
done
for i in `seq 2 $line`
do
name=`cat $quantity | sort -n -k2 -t"," | head -n$i | tail -n1 | cut -d ',' -f4`
average[$name]=$((addQuantity[$name]/numberOfQuantity[$name]))
echo "$name - $quantity - ${addQuantity[$name] - $ {numberOfQuantity[$name]} - ${average[$name]}"
done
This works good but I want to print the average with decimals.
My file .csv is that:
[...],[...],[...],name,quantity
0,0,0,name1,4
0,0,0,name1, 7
0,0,0,name2,10
0,0,0,name3,4
0,0,0,name3,6

Related

echo output not getting assigned to a variable in shell script

end=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 2 |rev
begin=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 3 |rev
echo $end
echo $begin
echo abc_11204.00_15713.00_.csv | rev | cut -d'_' -f 2 |rev ---- This works
But echo $end is not printing anything
I even tried:
set end=echo abc_11204.00_15713.00_.csv | rev | cut -d'_' -f 2 |rev
echo $end
This prints empty
Please help me with this
Sample input : abc_123.00_345.00_xyz.csv
Output : end=345.00
begin=123.00
Could you please try following. Easy approach with awk.
start=$(echo "$input_variable" | awk -F'_' '{print $2}')
end=$(echo "$input_variable" | awk -F'_' '{print $3}')
When I print variable's values it will be as follows:
echo "$start"
123.00
echo "$end"
345.00

Bash Script: Filter large files for value

I have several config files with around 20k lines each and I need to get some values from them.
I know that each of the values I need starts with a specific word "CONFNET" so I tried to get the values with a while loop, which reads every line.
But unfortunately this is extremely inefficient and slow.
Is there a better solution to this?
for filename in ~/configs/*; do
ip=$(cat $filename | strings | grep -i -A 7 "addnet_outside" | head -7 | grep "IP" | sed "s/IP//" | sed "s/=//" | sed -e 's/^[ \t]*//')
hostname=$(cat $filename | strings | grep -a "Inst:" | head -1 | sed "s/Inst://" | sed -e 's/^[ \t]*//')
while IFS= read -r line; do
object_name=$(echo $line | strings | grep "CONFNET" | sed "s/CONFNET//" | awk '{print $1}')
object_value=$(echo $line | strings | grep "CONFNET" | sed "s/CONFNET//" | awk '{print $3}' | sed -e 's/^[ \t]*//')
if [ ! -z $object_name ] && [ ! -z $object_value ]
then
echo $hostname "->" $object_name ":" $object_value
done < "$filename"
done

Shell Script do while flow

I have a script whose content is like:
#!/bin/bash
DB_FILE='pgalldump.out'
echo $DB_FILE
DB_NAME=''
egrep -n "\\\\connect\ $DB_NAME" $DB_FILE | while read LINE
do
DB_NAME=$(echo $LINE | awk '{print $2}')
STARTING_LINE_NUMBER=$(echo $LINE | cut -d: -f1)
STARTING_LINE_NUMBER=$(($STARTING_LINE_NUMBER+1))
TOTAL_LINES=$(tail -n +$STARTING_LINE_NUMBER $DB_FILE | \
egrep -n -m 1 "PostgreSQL\ database\ dump\ complete" | \
head -n 1 | \
cut -d: -f1)
tail -n +$STARTING_LINE_NUMBER $DB_FILE | head -n +$TOTAL_LINES > /backup/$DB_NAME.sql
done
I know what it is doing. But i have a doubt about the flow of do while in this case. At line egrep -n "\\\\connect\ $DB_NAME" $DB_FILE | while read LINE will egrep runs first or while . Because DB_NAME is empty at start of code.
Could anyone please explain the flow of do while in this case.

Using bash command on a variable that will be used as reference for an array

Short and direct, basically I want to use the value of $command on a variable, instead using it inside the while loop as a command itself. So:
This Works, but I think it's ugly:
#!/bin/bash
IFS=$'\n'
lsof=`which lsof`
whoami=`whoami`
while true ; do
execution_array=($(${lsof} -iTCP -P 2> /dev/null | grep ':' | grep ${whoami} | awk '{print $9}' | cut -f2 -d'>' | sort | uniq ))
for i in ${execution_array[*]}; do
echo $i
done
sleep 1
done
unset IFS
This doesn't work ( no output happens ), but i think is less ugly:
#!/bin/bash
IFS=$'\n'
lsof=`which lsof`
whoami=`whoami`
command="${lsof} -iTCP -P 2> /dev/null | grep ':' | grep ${whoami} | awk '{print $9}' | cut -f2 -d'>' | sort | uniq"
while true ; do
execution_array=($(command))
for i in ${execution_array[*]}; do
echo $i
done
sleep 1
done
unset IFS
This solved my problem:
#!/bin/bash
IFS=$'\n'
lsof=$(which lsof)
list_connections() {
${lsof} -iTCP -P 2> /dev/null | grep ':' | grep $(whoami) | awk '{print $9}' | cut -f2 -d'>' | sort | uniq
}
while true ; do
execution_array=($(list_connections))
for i in ${execution_array[*]}; do
echo $i
done
sleep 1
done
unset IFS

BASH command: How to save output of bash command into variable and later pipeline into command

i have a question about how to store the output into variable and then later pipeline into another command
var=$(ps -auxc | grep -vE '^USER' )
#get top CPU
echo $var | sort -nr -k3 | head -1
#get top memory
echo $var | sort -nr -k4 | head -1
Make sure to use quotes in assignment and while accessing variable:
var="$(ps -auxc | grep -vE '^USER')"
#get top CPU
sort -nr -k3 <<< "$var" | head -1
#get top memory
sort -nr -k4 <<< "$var" | head -1
I'm not sure if this would always work:
IFS= read -rd '' var < <(ps -auxc | grep -vE '^USER') ## -d '' may be -d $'\0'
echo -n "$var" | sort -nr -k3 | head -1
However using readarray could:
readarray -t var < <(ps -auxc | grep -vE '^USER')
printf '%s\n' "${var[#]}" | sort -nr -k4 | head -1

Resources