Pass awk array variable to shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass awk array variable to shell
# 1  
Old 04-19-2013
Pass awk array variable to shell

Hi, all

suppose I have following myfile (delimited by tab)

Code:
aa bb
cc dd
ee ff

and I have following awk command:

Code:
awk 'BEGIN{FS="\t"}{AwkArrayVar_1[x++]=$1;AwkArrayVar_2[y++]=$2};END{for(i=0; i<NR; i++) print i, AwkArrayVar_1[i], AwkArrayVar_2[i],}' myfile

My question is: how can I assign the awk array variable to shell array variable, such as ShellArrayVar_1 = AwkArrayVar_1, and ShellArrayVar_2 = AwkArrayVar_2?
# 2  
Old 04-19-2013
If you want a shell array, I would use shell programming directly. I am not aware of a way to assign an awk array to a shell array.
Code:
n=0
while read fld_1 fld_2; do
  array_var_1[n]=$fld_1
  array_var_2[n]=$fld_2
  n=`expr $n + 1`
done < myfile

# 3  
Old 04-20-2013
Thank you very much.

What if my file is delimited by "|" or";"? can shell read fields of such files into array variables?
# 4  
Old 04-20-2013
Yes, to do that, set the IFS (input field separator) variable as follows:
Code:
aa|bb
cc|dd
ee|ff

Code:
IFS='|'
n=0
while read fld_1 fld_2; do
  array_var_1[n]=$fld_1
  array_var_2[n]=$fld_2
  n=`expr $n + 1`
done < myfile

# 5  
Old 04-20-2013
Thank you so much for helping me.

I am sorry to ask you so many questions; but I am just wondering what if I only want to read the 2nd filed? maybe with combined use of "cut" command? such as

Code:
while read fld2; do
array_var_2[n]=$fld_2
n=`expr $n + 1`
done < (cut -f2 myFile)


Last edited by littlewenwen; 04-20-2013 at 01:00 AM..
# 6  
Old 04-20-2013
Yes, you can use the second field alone, using cut. Here is how I would do it, which seems simple and clear.
Code:
n=0
cut -f 2 myfile > fld2.txt
while read fld_2; do
  array_var[n]=$fld_2
  n=`expr $n + 1`
done < fld2.txt

Of course, you could also:
Code:
n=0
while read fld_1 fld_2; do
  array_var[n]=$fld_2
  n=`expr $n + 1`
done < myfile

This User Gave Thanks to hanson44 For This Post:
# 7  
Old 04-20-2013
Thank you very much for help, really appreciate it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

2. UNIX for Dummies Questions & Answers

Pass shell Variable to awk

Hello, May i please know how do i pass the shell variable to awk expression in the below script. It is returning null #!/bin/bash UNINUM=720922 UNINUM_DESC=`awk -F'|' -v UNINUM=$2 '/UNINUM/ {print $4}' datafile` echo $UNINUM_DESC datafile 4|First|720194|asdasdad 4|First|720735|asdasdsa... (8 Replies)
Discussion started by: Ariean
8 Replies

3. Shell Programming and Scripting

Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem.. Need your help. is there any alternative to make it in arrays from variable values nawk 'BEGIN {SUBSEP=" "; split("101880|110045 101887|110045 101896|110045... (9 Replies)
Discussion started by: cskumar
9 Replies

4. UNIX for Dummies Questions & Answers

How to pass a variable from shell to awk

I know this topic has been dealt with previously, but the solutions I've seen don't work for me apparently. I need to pass a variable defined in the shell to one in awk: $ echo $var1 3 $ cat aaa aaa 1 bbb 2 ccc 3 ddd 4 eee 5I've tried this, without success: $ awk... (2 Replies)
Discussion started by: metaltree
2 Replies

5. Shell Programming and Scripting

Pass array variabel to awk from shell

Hi I need to pass an array to Awk script from Shell. Can you please tell how to do it? How to pass this array add_ct_arr to an awk script or access it in awk? i=1 while ; do add_ct_arr=$(echo ${adda_count} | awk -v i=$i -F" " '{print $i;}') echo ${add_ct_arr} ... (1 Reply)
Discussion started by: appsguy616
1 Replies

6. UNIX for Dummies Questions & Answers

How To Pass an Array Variable

Hi, I have a master BASH shell script where I define a bunch of variables: $var1=why $var2=is $var3=(this so hard) I would then like to call another shell script and pass these variables to it: $script2 $var1 $var2 $var3 This works fine for var1 and var2. However, var3 is an array,... (9 Replies)
Discussion started by: msb65
9 Replies

7. Shell Programming and Scripting

pass variable from awk to shell script

Hello Experts, Actually I was searching for a solution here in this forum , but didn't get what exactly I want . Is this possible to do in awk ? I am trying to do some thing like below in ksh script . Upto my knowledge I can pass shell script to awk with "-v " option. But I... (3 Replies)
Discussion started by: user_prady
3 Replies

8. Shell Programming and Scripting

Is it possible to pass variable from awk to shell back

I am passing a varaible to from Shell to awk then I am doing some maniplation for that variable inside awk. I want that maniplated variable value back to shell , Is this possible .Please let me know. (12 Replies)
Discussion started by: unishiva
12 Replies

9. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies

10. UNIX for Dummies Questions & Answers

How to pass Shell script variable to awk

Hi, I have a shell script with an ambedded awk script. i need to pass a script variable to the awk script. Please help. Thanks in advance Himani (3 Replies)
Discussion started by: HIMANI
3 Replies
Login or Register to Ask a Question