LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-17-2011, 12:12 PM   #1
congos
LQ Newbie
 
Registered: Oct 2011
Posts: 20

Rep: Reputation: Disabled
arrays in text


#!/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
 
Old 11-17-2011, 12:17 PM   #2
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,479

Rep: Reputation: Disabled
You probably want to look at using 'awk'.

man awk
 
Old 11-17-2011, 01:03 PM   #3
congos
LQ Newbie
 
Registered: Oct 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by fatmac View Post
You probably want to look at using 'awk'.

man awk
how can i do it?im new to shell scripting
 
Old 11-17-2011, 01:13 PM   #4
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,479

Rep: Reputation: Disabled
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 ...
#

Last edited by fatmac; 11-17-2011 at 01:15 PM.
 
1 members found this post helpful.
Old 11-17-2011, 05:01 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could just do it in bash; bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 11-17-2011, 10:45 PM   #6
tollingalong
Member
 
Registered: Nov 2011
Posts: 42

Rep: Reputation: Disabled
Generally, awk is installed on most machines. You can call awk form within your bash script and make it easier.
 
Old 11-18-2011, 01:01 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
Help Me(how to read text file using arrays in c++) rezen85 Programming 2 10-19-2010 09:50 AM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration