Store value in array with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Store value in array with awk
# 1  
Old 11-04-2012
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:
Code:
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 column in X[i] and the second one in Y[j].
And how can I refer to this value for example print on screen?
Tnx in advance for your help
Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by vgersh99; 11-04-2012 at 11:07 AM.. Reason: code tags, please!
# 2  
Old 11-04-2012
Just an example (to be adapted depending on your needs)

Code:
$ awk '{X[NR]=$1;Y[NR]=$2}END{print X[2]"\n"Y[10]}' input
32783
29

NR stand for Number of Record since the RS (Record Separator) is the newline character by default, NR here simply refer to the line number.
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 11-04-2012
If you are talking shell array, try this. Syntax will vary from shell to shell, this one is bash (index starting at 0):
Code:
$ X=($(awk '{print $1}' file))
$ echo ${#X[@]}                            # element count
13
$ echo ${X[@]}                             # all elements
32782 32783 32784 32785 32786 32787 32788 32789 32790 32791 32792 32793 32794
$ Y=($(awk '{print $2}' file))
$ echo ${X[2]}, ${Y[2]}                    # third pair
32784, 01

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-04-2012
Here is another example, saving $1 in array val

Code:
FNR == NR {
  />/ && idx[FNR] = ++i
  $2 || val[i] = $1      # Store source location
  next
}

This User Gave Thanks to kristinu For This Post:
# 5  
Old 11-04-2012
Thanks to all for solving my problem
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

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

3. Shell Programming and Scripting

Can -v option in awk be used to store an array of variables?

I want to pass an array of variables to be inserted by awk in the 2nd column of a file. Empl No. Employee Age 1000000 22 1100000 24 1200000 26 Now, I want to pass an array having three different ages which need to replace the... (7 Replies)
Discussion started by: Nishi_Licious
7 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

How to store output of command to an array

Hello Guys, I am trying to store output of command to an array and then want to print, but it showing an error that "bad substitution". I am not getting why it's happening. Can anyone help me? #!/usr/bin/sh hr=0 min=0 i=1 last $1 | grep -w `date "+%b"` | grep -v '\(0:.*\)' | grep -vw sshd... (8 Replies)
Discussion started by: kasparov
8 Replies

6. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

7. 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

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. Shell Programming and Scripting

Store values in an Array

Hi all. Well, I have the next code: 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? (8 Replies)
Discussion started by: crcbad
8 Replies

10. 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
Login or Register to Ask a Question