Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays in bash shell scripts

Tags:

bash

shell

unix

#!/bin/sh
# Script to count the total in an array
# Define the name of the file
#
fname=names.txt

# Read in the contact details from the keyboard
echo "Please enter the following contact details:"
echo
echo "Given name: \c"
read name
echo " value: \c"
read value
# Write the details to the text file
echo $name:$value >> $fname

I'm trying to code something in bash scripting, I have a txt file and I entered the following names on it e.g

lex +7.5
creg +5.3
xondr/xonde +1.5
gloria-1
lex +7.5
gloria -1
creg +5.3
xondr/xonde +1.5
lex +7.5
#and so on

I want a code or a loop that when I run the program it should show the names of that are on the txt file and show there total,if lex appears 7 times and gloria 3 times it will show lex 52.5 gloria-3 etc. I don't know if you get my idea...

like image 864
thequantumtheories Avatar asked Feb 06 '26 19:02

thequantumtheories


1 Answers

It sounds like you want something like:

$ awk '{x[$1] += $2} END {for( i in x) print i, x[i]}' input-file
like image 151
William Pursell Avatar answered Feb 09 '26 11:02

William Pursell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!