Weird Awk issue

Hi All,
a bit of a weird one here. I'm trying to pass a variable into an awk command, and I keep getting an error.

I have the line

nawk -F"," -v red=$random_variable '{print $red}' $w_dir/$file_name > $w_dir/${column_name}

that keeps failing with the error

nawk: can't open file {print $red}
source line number 1

It's a bit weird, because I actually have another similar line later in my script that works perfectly

abbreviation=$(head -$counter $w_dir/$file_name| tail -1|nawk -F"," -v rad=$index '{print $rad}')

Maybe that's because the output is being assigned to another variable?
Any assistance will be very much appreciated.

Thanks

are there shell metacharacters in $random_variable that cause the shell to interpret things differently?

When I echo $random_variable, which have done while trying to troubleshoot, I get a number - 10, 11 or 13.

I have even tried running substituting the value

nawk -F"," -v red=13 '{print $red}' $w_dir/$file_name > $w_dir/${column_name}

Still the same error.

Don't preface the variabele with a '$' sign:

{print red}

instead of:

{print $red}

Regards

$red is perfectly valid if it means, say, field no. 13. In any event the error message is, indeed, weird.

I tried again.

nawk: can't open file {print red}
source line number 1

Echo the used variables to see if they setted proper.

Regards

Hey Franklin.

I currently do echo the $random_variable variable, that is correct. I'm not sure I'm able to echo the "red" variable

And the other variables?

Check if the space after -F"," is missing:

nawk -F","<space>-v red=13 ...

Hi. I just checked everyone of those variables. Still no difference.

Post the entire script and place it within code tags(!).

Code tags??

Select the code and click on the "#" symbol above the edit form.

I'll call it a week now - it's 17:37 over here. I'll pick up again on monday.

cheers Guys

The syntax looks correct. By the error message you are getting and the fact that you said this was in a script (not just on the command line) I suspect that there is a spurious, non-printable character in your script that is hosing up the scanning of the line properly and causing it to think that

Try this:
a. you can often find these non-printable chars by using "od -c filename | less"
b. re-type in the command on a new line and delete the old line altogether

Here is an example of how I reproduced your error. Look closely at the output of 'od -c' where there is actually a bad char (002 == CNTL-B) that your editor may not show you, but truly throws an error for nawk:

$ cat badnawk
#!/bin/sh
nawk -F":" -v red=2 '{print $red}' $0
$
$ sh badnawk
nawk: can't open file {print $red}
 source line number 1
$
$
$ od -c badnawk
0000000   #   !   /   b   i   n   /   s   h  \n   n   a   w   k       -
0000020   F   "   :   "       -   v 002       r   e   d   =   2       '
0000040   {   p   r   i   n   t       $   r   e   d   }   '       $   0
0000060  \n
0000061

I checked. There are no hidden characters. For double measure, I have re-typed that line of code (not copied and pasted). Still the same error.

Finally resolved.

nawk -F"," -v red="$random_variable" '{print $red}' $w_dir/$file_name > $w_dir/${column_name}

The double-quotes around the variable did it.