arrays in text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arrays in text
# 1  
Old 11-17-2011
Java arrays in text

Code:
#!/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
Code:
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

Last edited by Scott; 11-17-2011 at 04:14 PM.. Reason: Code tags...
# 2  
Old 11-17-2011
bash can't do that, bash doesn't support floating point numbers. It also doesn't have associative arrays.

awk has both:
Code:
awk '{ T[$1] += $2 } END { for(K in T) print K, T[K]; }' filename

# 3  
Old 11-18-2011
Hi.
Quote:
Originally Posted by Corona688
bash can't do that, bash doesn't support floating point numbers. It also doesn't have associative arrays.
It looks like bash4 has associative arrays: Bash provides one-dimensional indexed and associative array variables -- excerpt from page at: Bash Reference Manual

Floating-point can be done with bc, but in general, I would probably use awk if I needed to have all those features in a single package ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Arrays

Am using bash For eg: Suppose i have a array arr=(1 2 3 4 5 6 7 8 9 10 11 12) suppose i give input 5 to a script and script should able to print values greater than or equal to 5 like below: Input: 5 output: 5,6,7,8,9,10,11,12 (7 Replies)
Discussion started by: manid
7 Replies

2. Shell Programming and Scripting

Using arrays?

I have never used arrays before but I have a script like this: var1=$(for i in $(cat /tmp/jobs.021013);do $LIST -job $i -all | perl -ne 'print /.*(\bInfo.bptm\(pid=\d{3,5}).*/' | tr -d "(Info=regpid" | tr -d ')'; $LIST -job $i -all | cut -f7 -d','| sed -e "s/^\(*\)\(*\)\(*\)\(.*\)/\1... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Shell Programming and Scripting

ksh arrays

Hi, I have a ksh script in which I need to fill an array with a list of filenames. It currently works like this: set -A array \ val1 \ val2 \ val3However, I was wondering why it's not possible to do something like this to make it easier to parse values to the array: set -A array... (3 Replies)
Discussion started by: Subbeh
3 Replies

4. Programming

Arrays in C++

I've noticed something interesting in C++ programming. I've always done tricky stuff with pointers and references to have functions deal with arrays. Doing exercises again out of a C++ book has shown me an easier way, I didn't even know was there. It's weird to me. When dealing with arrays, it... (4 Replies)
Discussion started by: John Tate
4 Replies

5. Shell Programming and Scripting

How can I use the arrays ?

Hi all, I have a file test1.txt with the below contents abc def ghj xyz I tried printing these values using arrays. Script tried : =========== set -A array1 `cat test1.txt` count=${#array1 } i=0 while do echo "element of array $array1" done (1 Reply)
Discussion started by: dnam9917
1 Replies

6. Shell Programming and Scripting

Find and replace using 2 text files as arrays.

Here's the nonfunctional code I have so far #!/bin/bash searchFor=(`cat filea.txt` ) replaceWith=(`cat fileb.txt`) myMax=${#searchFor} myCounter=1 while ; do sed -i 's/${$searchFor}/${$replaceWith}/g' done The goal is to use each line in filea.txt as a search term, and each line... (2 Replies)
Discussion started by: Erulisseuiin
2 Replies

7. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

8. UNIX for Dummies Questions & Answers

arrays how to?

Hello, I am some what of a newbie to awk scripting and I seem to be struggling with this problem. I know I need to use arrays but I can't figure out how to use them. I have an input file that looks like this; Name,Team,First Test, Second Test, Third Test Crystal,Red,5,17,22... (1 Reply)
Discussion started by: vlopez
1 Replies

9. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. Shell Programming and Scripting

Arrays

Dear all, How can i unset arrays. I mean all the subscripts including the array after using them. Could you direct me to some links of array memory handling in the korn shell. Thanks (2 Replies)
Discussion started by: earlysame55
2 Replies
Login or Register to Ask a Question