using an awk internal variable as parameter for an external array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using an awk internal variable as parameter for an external array
# 1  
Old 08-25-2011
Question using an awk internal variable as parameter for an external array

Hello,

I am running a bash script under linux which first defines an CA-array like
Code:
j=0
num1=120.00
num2=10.00
until [$j -gt 5 ]
do
  CA[$j]='echo $num1 + $j*$num2'
  j=$[$j+1]
done

within the later awk section of this same script I want to read data from a file. If the value of the second column is equal to the value of CA[0 to 5] the array MF[0 to 5] should read the value from column 59:
Code:
awk '
BEGIN {
  ...
  i=0
  while ( i<5 ) {
    if ( match($2,/^'${CA[1]}'/) ) { MF[i] = $59 }
      i = i + 1
  }
}
END ' ${FILE}

As long in the bold written part the parameter of the array is fixed to certain value (here "1") the script works fine. In later stage I do want to read dozend of lines from the file and not only 5!

Does anyone know how I do have to change the script ('${CA[1]}') in order to have "i" of the awk loop to define the parameter fo the external array CA? Any help or work arround is appreciated.

Regards,

MotAah

Last edited by Scott; 08-25-2011 at 10:14 AM.. Reason: Code tags
# 2  
Old 08-25-2011
No way - there are no two-way communications between syntax constructions of shell and awk (like you want).
You can use a scalar variable with embeded newlines. Send its value to awk through -v option and split it in the BEGIN block to an awk array. Or use a temp file.
This User Gave Thanks to yazu For This Post:
# 3  
Old 08-25-2011
An easy way to generate a string of "thing1 thing2 thing3 ..." from an array using any separator you want is:

Code:
OLDIFS="$IFS"
IFS="|" # adjust to whatever you want
LIST="${ARR[*]}"
IFS="${OLDIFS}"

awk -v LIST="$LIST" 'BEGIN { split(LIST, A, "|"); } ....

which will turn the array ARR=( 1 2 3 ) into LIST="1|2|3" for easy feeding into awk, which splits it on the same separator, into the awk array A.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 08-26-2011
Hi,

thank you for the quick reply. I am using the list option now which transfers the array into the awk script well. I might try the temp-file option as well as I currently running into a format problem. But that is another problem.

Thanks again for your help

MotAah
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Disk in Linux machine are internal or external

How to check whether the disk in linux machine are internal or external ( from nas or san). How to identify internal(local) and external disks. Following are some details of my server. Thanks. #df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 15G 3.5G 10G... (2 Replies)
Discussion started by: salmanraza
2 Replies

2. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

3. Shell Programming and Scripting

Help needed with awk external variable

I'm trying to get the universities result data into different file, where the $9 contains unversity field and field7,4 & 5 contains the keys to sort the students by marks. How to use uni variable to match against $9 inside awk? c=0 for uni in `cat /tmp/global_rank| awk -F ',' '{print... (1 Reply)
Discussion started by: InduInduIndu
1 Replies

4. Shell Programming and Scripting

How can we assign value to an array variable from an external file?

is it possible to assign value to an array variable from an external file?? if yes then how?? I am using below code but its not working. #!bin/bash myarray < file_name echo ${mayarray} (6 Replies)
Discussion started by: mukulverma2408
6 Replies

5. Shell Programming and Scripting

[awk] - how to insert an external variable

I want to incorporate the variable in the for statement as a column of my processed file. In the INCORRECT example below, it is $i which corresponds to the i in my for loop: for i in x86_64 i686; do awk '{ print $1" "$4" "$5" "$i }'awk $file-$i > processed-$i.log doneThanks! (3 Replies)
Discussion started by: graysky
3 Replies

6. Shell Programming and Scripting

Insert external variable in a AWK pattern

Dear all, ¿How can i insert a variable in a AWK pattern? I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain. cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/ { print "\n"$0 }' I know that this command makes right what i want to do, but mi... (8 Replies)
Discussion started by: antuan
8 Replies

7. Shell Programming and Scripting

If statement in awk with external variable

So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have. # !/bin/sh echo "Enter client ID" read ID echo "Enter month (01, 02, 03)" read month echo "Enter day (03, 15)" read day echo "Enter Year (07, 08)" read... (6 Replies)
Discussion started by: doublejz
6 Replies

8. Shell Programming and Scripting

Parameter Problem With an Array Variable

Hi, I have two bash shell scripts: test: rrdhcp78-120:test_data msb65$ cat ~/bin/test #!/bin/sh array1=() echo 'elements in array1:' ${#array1} ~/bin/test2 $array1 test2: rrdhcp78-120:test_data msb65$ cat ~/bin/test2 #!/bin/sh array2=${1} (5 Replies)
Discussion started by: msb65
5 Replies

9. Shell Programming and Scripting

awk help (external variable)

i need help with an awk question. i am looking to have an external variable be defined outside of awk but used in awk. so if we have fields $1, $2, $3 so on and so forth, i would like to be able to dictate what field is being printed by something like $. so if i had a counter called test, make it 3... (8 Replies)
Discussion started by: pupp
8 Replies
Login or Register to Ask a Question