AWK: Retrieving names of variables passed with -v


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: Retrieving names of variables passed with -v
# 1  
Old 12-03-2009
AWK: Retrieving names of variables passed with -v

I'm an experienced awk user, but this one has me stumped. I have an awk script which is called from a UNIX command line as you'd expect:

Code:
myscript.awk -v foo=$1 -v bar=$2 filename

My question is this: is there a mechanism for determining the names of the -v variables within a script?

ARGV doesn't include them (and quite rightly so, since they're not arguments).

I'd like my script to be able to parse a list of variables passed, so that I can print an error message along the lines of "Unknown variable name - please check and try again", or call a Usage() function, or some such.

Thanks in advance, guys 'n' gals.

Last edited by John Mac; 12-03-2009 at 01:17 PM..
# 2  
Old 12-03-2009
Why not put this whole idea inside a ksh script and let ksh handle the error checking?

ie:

Code:
#!/bin/ksh

error_ind=0

for arg in $* ; do

  case $arg in

    frogs=*) : ;;
    dogs=*) : ;;
    *=*) print unknown variable: $arg ; error_ind=1 ;;

  esac

done

if [[ $error_ind -eq 1 ]]; then
  exit 1
fi

awk etc..... here.....

# 3  
Old 12-03-2009
With Bell-lab's awk, you have:
Code:
bell-awk -v v11=aa -v v22=bb 'BEGIN{for (i in SYMTAB) print i}'
...
ENVIRON
v22
v11
FILENAME
...

SYMTAB used to be in gawk. There is
Code:
-W dump-variables

command line option for gawk, but not very useful in your case.
# 4  
Old 12-04-2009
Thanks, guys

Cheers, guys.

quirkasarus: Great minds, and all that: that's exactly what I ended up doing (and then scrapped it - see below).

binlib: You've cracked it! SYMTAB holds the variable names (amongst much else). Just what I was looking for:-)

Many thanks to both of you for looking into this.

Last edited by John Mac; 12-04-2009 at 06:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. UNIX for Dummies Questions & Answers

Retrieving names of files in a dir without overlapping

Hi, I have been trying to retrieve the names of files present in a directory one by one but the names of files are getting overlapped on one another. I tried the below command. ls -1 > filename please help me in getting the file names line by line without overlapping. I am using korn... (6 Replies)
Discussion started by: Pradhikshan
6 Replies

3. UNIX for Dummies Questions & Answers

Multiple variables to be passed in a loop

Hi, I need to pass the multiple values of src1 to another variable. I managed to print it but not sure how to assign it to a variable in a loop. src1=01,02,03 echo $src1|awk 'BEGIN {FS=","} {for(i=1;i<=NF;i++) print $i}' I need to pass the value as src2=01 src2=02 src2=03 Thanks... (4 Replies)
Discussion started by: shash
4 Replies

4. Shell Programming and Scripting

Shell Variables passed to awk to return certain rows

Hi Forum. I have the following test.txt file and need to extract certain rows based on "starting position", "length of string" and "string to search for": 1a2b3d 2a3c4d ..... My script accepts 3 parameters: (starting col pos, length to search for, string to search for) and would like to... (4 Replies)
Discussion started by: pchang
4 Replies

5. Shell Programming and Scripting

Multiple bash variables passed into nawk

I have a file that has 2 fields called b_file: 11977 DAR.V3.20150209.1.CSV 3295 DAR.V3.20150209.1.CSV 1721 DAR.V2.20150210.1.CSV I need to search a sftplog using the field 1, but want to maintain the relationship between field 1 and 2. I am passing field 1 as a parameter in a bash loop. ... (14 Replies)
Discussion started by: smenago
14 Replies

6. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

7. Shell Programming and Scripting

How to create files with two or more variables in its names?

Hi all, Iam writing a perl script to create many files with variables in their name. i am able to do it, if iam using only one variable. But with two variables the file names are NOT getting generated in the way i want. plz help me out. 1. open(SHW,">divw_unsigned_50_50_$k.reset") or die... (4 Replies)
Discussion started by: twistedpair
4 Replies

8. Shell Programming and Scripting

awk processing of passed variables

Currently have this: set current=192.168.0.5 set servicehost = `echo $current | awk -F. '{print $4}'` echo $numberoffields 5 ..but would like to reduce # of variables and eliminate echo to have something like this: set servicehost = `awk -v s="$current" -F. 'BEGIN{print $2}'`But... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

9. UNIX for Advanced & Expert Users

retrieving all group names with a given group number

hi, which Unix/C function can i use to retrieve all group names with a particular group id? The following C code prints out the group id number of a particular group name: ------------------------------------------------------------------------ #include <stdio.h> #include <grp.h> int... (3 Replies)
Discussion started by: Andrewkl
3 Replies

10. Shell Programming and Scripting

How to get files names passed to a script

I need to get files names passed to a script. Here number of files passed may vary like MyScript.ksh file1 file2 file3..... so on I am writting script somthing like this set -A Files while (i<=$#) do File=$i let i=i+1 done Is this correct way doing this. Is there any other way.... (5 Replies)
Discussion started by: unishiva
5 Replies
Login or Register to Ask a Question