user3086551
user3086551

Reputation: 435

How to use awk variable inside double quotes?

I have the following shell variable defined:

memory_params="some memmory params"
class_path="some class path"
class_name="some class name"

Now, I use these bash variable inside awk

awk -v var1="${memory_params}" \
    -v var2="${class_path}" \
    -v var3="${class_name}" \
  '{ 
     cmd="java var1 -cp var2 var3";
     system(cmd);  
   }'

But, this doesn't take the values of var1, var2, and var3 for obvious reason. How can awk variables be used inside double quotes?

Upvotes: 2

Views: 1815

Answers (2)

Harkirat Singh
Harkirat Singh

Reputation: 11

I have also come across same scenario,below command I used which resolved my issue.

database_impala=test_db awk -v var="${database_impala}" '{ print var"."$0}'

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133518

try following once and let me know if this helps. Use variables in system command directly rather than creating a variable and passing it to system.

awk -v var1="${memory_params}" \
    -v var2="${class_path}" \
    -v var3="${class_name}" \
  BEGIN'{ 
     system("java " var1 " -cp " var2 OFS var3);  
   }'

Remove that BEGIN in case you have any Input_file to be read.

Upvotes: 2

Related Questions