LinuxQuestions.org
Review your favorite Linux distribution.
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, 01: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, 01:17 PM   #2
fatmac
Senior Member
 
Registered: Sep 2011
Location: Surrey/Hants Border, UK
Distribution: BSD & Linux - As long as they are Lightweight
Posts: 1,500

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

man awk
 
Old 11-17-2011, 02: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, 02:13 PM   #4
fatmac
Senior Member
 
Registered: Sep 2011
Location: Surrey/Hants Border, UK
Distribution: BSD & Linux - As long as they are Lightweight
Posts: 1,500

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 02:15 PM.
 
1 members found this post helpful.
Old 11-17-2011, 06:01 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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, 11: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, 02:01 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:55 AM
Help Me(how to read text file using arrays in c++) rezen85 Programming 2 10-19-2010 10:50 AM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 02:39 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 06:40 AM


All times are GMT -5. The time now is 08:20 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration