Store values in an Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Store values in an Array
# 1  
Old 03-04-2009
Store values in an Array

Hi all.

Well, I have the next code:

Quote:
awk -F: '{
for(i=1; i<=NF; i++)
{
#b[i]="$" i
#b[i]="caca"
}
}' tmpid
I need to make an array with the values I have in the bucle, but just don't get it...

Question is, how can I store in an array that values, and how can I display them with echo?

Last edited by crcbad; 03-04-2009 at 08:10 AM..
# 2  
Old 03-04-2009
Still working in it, but dont get to save the values Smilie
# 3  
Old 03-04-2009
Code:
awk -F: '{
for(i=1; i<=NF; i++)
   b[i]=$i
}' tmpid

OR
Code:
awk -F: '{
  split($0, b, FS)
}' tmpid

# 4  
Old 03-04-2009
There are a lot of threads regarding this item, search for arrays awk.

Regards
# 5  
Old 03-04-2009
vgersh99,

I tried that before, but when I write echo ${b[1]} then I get a blank.

Why this happen?

Franklin52,

I'll be more careful from now on. Sorry.
# 6  
Old 03-04-2009
Quote:
Originally Posted by crcbad
vgersh99,

I tried that before, but when I write echo ${b[1]} then I get a blank.

Why this happen?
'echo' is a shell command. 'b' is your awk array.
Code:
awk -F: '{
for(i=1; i<=NF; i++) 
   b[i]=$i

for(j=1; j<=i; j++)
   printf("j->[%d] b[%d]->[%s]\n", j, j, b[j])
}' tmpid

# 7  
Old 03-04-2009
vgersh99,

Im new in the unix world. So now I understand what you told me before.

So, the variables I set within awk, can't be used in shell later?

I mean, I created an array named "b" with "x" elements. That variable is exclusive within awk, isn't it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. Shell Programming and Scripting

Store value in array with awk

Hi everybody I wanna store some values that r in a .txt file in some arrays for example I have: 32782 28 32783 02 32784 01 32785 29 32786 25 32787 25 32788 00 32789 25 32790 02 32791 29 32792 23 32793 01 32794 28 and I need to save the first... (4 Replies)
Discussion started by: Behrouzx77
4 Replies

3. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

4. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

5. Shell Programming and Scripting

Store values from a file into an array variable in Shell

Dear All, I have been trying to do a simple task of extracting 2 fields from the file (3 rows) and store it in an array variable. I tried with: #! /bin/bash ch=`cut -f10 tmp.txt` counter=0 for p in $pid do c=${ch} echo "$c ..$counter" counter=$((counter+1))... (2 Replies)
Discussion started by: ezhil01
2 Replies

6. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

7. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

8. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

9. UNIX for Dummies Questions & Answers

How to store the values in a file into an array

Hi, I do have a file and the contents are as follws: 10 20 30 40 50 Now I want to store those values into an array. How can be done this ?? (3 Replies)
Discussion started by: risshanth
3 Replies

10. UNIX for Advanced & Expert Users

how do I store the values in array using shell

Hi, Is is possible to get the value using shell script? x=1 y1 = 10 y2 = 15 y3 = 7 echo $y$x is giving y1 (variable name) but I need the value of y1 (i.e. 10 dynamically) Is there any solution? if so, please mail me at kkodava@maxis.com.my ... (2 Replies)
Discussion started by: krishna
2 Replies
Login or Register to Ask a Question