1

I'm writing a SHELL script that will call an AWK script file printing specific fields based on the options specified by the user from the .sh script

EXAMPLE

-N=field with a list of Names               ==> is the field number 2
-A=field with a list of Addresses           ==> is the field number 4
-P=field with a list of Prices              ==> is the field number 6
-D=field with a list of Dates               ==> is the field number 10


./SHELLSCRIPT.sh -N -A -P -D

NAMES,ADDRESSES,PRICES,DATES
NAMES,ADDRESSES,PRICES,DATES
NAMES,ADDRESSES,PRICES,DATES

EXAMPLE

./SHELLSCRIPT.sh -N -D

NAMES,DATES
NAMES,DATES
NAMES,DATES

I'm trying to initialize a variable inside AWK using the -v option passing the number of fields but I'm having some difficulties on printing it...

here is the AWK script:

#!/usr/bin/awk -f
BEGIN {
        FS="\",\"" ;
        -v test=2,4,6,10
        for (i in test)
        print $test[i];
        }

AWK script must exclude some field/s when the user doesn't specify some option/s in the shell script.

4
  • 1
    +1 for showing sample input and sample output, -1 for not including your code ;-)... If you know about -v for awk, you can't be too far away from a working solution. Add your code and we can help you, other wise you're expecting us to play 20 questions with you. Good luck. Commented Apr 26, 2014 at 14:13
  • If you are passing more than one variable into awk, oddly enough, you must specify -v before each and every one - caught me out before now!!! Commented Apr 26, 2014 at 14:18
  • 1
    or you can split(test,inArr, ","); for(i in inArr) ... Good luck to all. Commented Apr 26, 2014 at 14:22
  • Thanks shellter, the "split" function is what I was looking for. Commented Apr 27, 2014 at 12:45

2 Answers 2

5

-v is an awk ARGUMENT, not a language construct. It's something you specify on the awk command line, like -F. Nevere use a shebang to invoke awk from a script as it just takes away from available functionality. Instead of what you had:

#!/usr/bin/awk -f
BEGIN {...}

you'd do this:

/usr/bin/awk '
BEGIN {...}
' "$@"

which then lets you use awk args, access shell positional parameters, exported variables, etc. as you like, e.g.:

/usr/bin/awk -v test='2,4,6,10' '
BEGIN {...}
' "$@"

Now, to address your actual problem you'd write:

/usr/bin/awk -F'"," -v test='2,4,6,10' '
BEGIN {
    split(test,array1,/,/)
    for (i in array1)
        array2[array1[i]]
}
{
    print "array1:"
    for (i=1; i in array1; i++) {
        print i, $(array1[i])
    }
    print "array2:"
    for (i in array2) {
        print i, $i
    }
}
' "$@"

array1 would be what you use when you do care about the order in which the fields are output, array2 would be when you don't care about the order.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe this wil help:

awk -v bozo=12 -v brains=99 'BEGIN{print bozo,brains}' /dev/null
12 99

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.