Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/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
im try 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 dont know if you get my idea.... thanks
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,479
Rep:
Linux is about learning; we try to help you find your solutions.
Below are 2 web sites that will guide you.
#
AWK - Wikipedia, the free encyclopedia
en.wikipedia.org/wiki/AWK
The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data ...
Structure of AWK programs - AWK commands - Sample applications
#
The GNU Awk User's Guide www.gnu.org/s/gawk/manual/gawk.html
You +1'd this publicly. Undo
This file documents awk , a program that you can use to select particular ... This is Edition 4 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk ...
#
Bash would be a bit trickier to use here, since the values to add up are floating point. You'd have to use an external tool like bc or awk to do the calculations anyway, so why not just do the whole thing in awk and save a step?
The method I'd use in awk is to populate an array with each name (column1) as the index string. Every time a name comes up, the value (column2) can be added to the existing total for that name. Finally, loop through the array in the END statement to print out your final values.
PS: Please use [code][/code] tags around your code, to preserve formatting and to improve readability.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.