shell / awk arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell / awk arrays
# 1  
Old 08-20-2008
shell / awk arrays

I have a bash shell script that sources a data file, the data file has array values such as:

#--data file ---#
sg_name[0]="db1"
sg_size[0]="12892"
sg_allow[0]="50000"

sg_name[1]="db2"
sg_size[1]="12892"
sg_allow[1]="50000"

export sg_name sg_size sg_allow
#--end data file --#

In my shell script I source this file:
. /data/data.file

so all the array variables are available.

Is there a way to easily use these variables within 'awk' ?
Or, can awk source this file also to use the array variables?

Thanks
# 2  
Old 08-20-2008
awk -v options does what you wish.

Code:
awk -v sg_name=${sg_name[0]} '{ print sg_name }'

# 3  
Old 08-20-2008
nawk and recent versions gawk (GNU awk) can read exported variables via ENVIRON e.g.
Code:
$ WET=august
$ export WET
$ gawk 'BEGIN { print ENVIRON["WET"] }' file
august
$

# 4  
Old 08-20-2008
I'm familiar with using the '-v' flag to set variables.

But I was wondering if it was possible to keep the same array structure in awk as it is in the shell.

I take it awk can not source that data file to utilize the array variables?

Maybe Im not wording this the best way Smilie
# 5  
Old 08-20-2008
Can you provide an example of how you plan to use it?
# 6  
Old 08-20-2008
I don't think there is a way to automatically import all arrays to awk. You have to do it manually with -v I guess.
# 7  
Old 08-20-2008
Heres something like what I have in a shell script:

. /data.file
anum="0"
while [ ! -z "${sg_name[${anum}]}" ]
do
echo "sg_name[${anum}]}"
echo "sg_size[${anum}]}"
echo "sg_allow[${anum}]}"
done

I'd like to continue to utilitize that array within awk the same way. But I guess the only way is to pass all variables via the '-v' flag.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arrays in awk

Array A contains lines of numbers from files. Array B contains number of fields for each line of each file. I want to run on array A. To do that I need to know the number of fields for each file in array A (because each line doesn't have the same NF). The NF is in array B, problem is: I don't... (6 Replies)
Discussion started by: guitarist684
6 Replies

2. Shell Programming and Scripting

awk Arrays

So I'm back once again beating my head off a wall trying to figure out how to get this to work. My end goal is to take input such as what's below, which will be capture in real time with a tail -f from a file or piped output from another command: ... (5 Replies)
Discussion started by: ShadowBlade72
5 Replies

3. Shell Programming and Scripting

Using arrays with awk

I'm a little stuck and would be grateful of some advice! I have three files, two of which contain reference data that I want to add to a line of output in the third file. I can't seem to get awk to print array contents as I would expect. The input files are: # Input file AAA,OAA,0313... (2 Replies)
Discussion started by: maccas17
2 Replies

4. Shell Programming and Scripting

help in awk arrays!

Hi, buddies I am new to shell scripting and trying to solve a problem. I read about arrays in awk that they are quite powerful and are associative in nature. Awk Gurus Please help! I have a file: Id1 pp1 0t4 pp8 xy2 Id43 009y black Id6 red xy2 Id12 new pp1 black I have... (5 Replies)
Discussion started by: geli21
5 Replies

5. UNIX for Dummies Questions & Answers

Help to know the various possible uses of awk arrays

Hi to all, I write this time to ask for different syntax or ways of arrays within awk and uses. Since I don't know how actually could work and the real potencial of awk arrays, I look for a proactive reply/help in order to get more information than it seems I'm trying to get. I think I... (2 Replies)
Discussion started by: cgkmal
2 Replies

6. Shell Programming and Scripting

awk arrays can do this better - but how?

Hi, I have spent the afternoon trawling Google, Unix.com and Unix in a Nutshell for information on how awk arrays work, and I'm not really getting too far. I ahve a batch of code that I am pretty sure can be better managed using awk, but I'm not sure how to use awk arrays to do what I'm... (1 Reply)
Discussion started by: littleIdiot
1 Replies

7. Shell Programming and Scripting

Accessing shell arrays in awk

I am trying to pass a shell array I created to match on values amongst other things. I have a sample: #! /bin/ksh i=0 for id in `cat id.lst` do IDs=$id (( i=i+1 )) done cat sqloutput.txt | awk -F, -v ids=$ID '{ # Do stuff }' But it looks as if I cannot do this.... (1 Reply)
Discussion started by: Zombywoof
1 Replies

8. Shell Programming and Scripting

Arrays in awk

Hi, I've written the following code to manipulate the first 40 lines of a data file into my desired order: #!/bin/awk -f { if (NR<=(4)){ a=a$0" "} else { if ((NR >= (5)) && (NR <= (13))) { b=b$0" " } else {if ((NR >= (14)) && (NR <= (25))){ c=c$0" "} ... (5 Replies)
Discussion started by: catwoman
5 Replies

9. Shell Programming and Scripting

Need Help with awk and arrays

now its owkring - thanks fo rthe help all . (7 Replies)
Discussion started by: fusionX
7 Replies

10. UNIX for Advanced & Expert Users

Two or more arrays in Awk

Hi All, I have been working on awk and arrays. I have this small script: cat maillog*|awk -F: '$2=="SMTP-Accept" && $5~/string/ {lastdate=substr($1,1,8); internaluser=$5; v++} END {for (j in v) {print lastdate, v, j}'| sort>> mail.list This gives me the number of mails users are getting. ... (1 Reply)
Discussion started by: nitin
1 Replies
Login or Register to Ask a Question