I want to use a BASH variable inside AWK that is an AWK command. Here is my test code that I plan to make more complicated if I can find a solution to this problem.
exptransform=(
'print \"test\"'
)
awk -v awk_exptransform="${exptransform}" '
BEGIN{
printf ("Test.\n")
printf ("This is another test: %s\n",awk_exptransform)
awk_exptransform
}
'
This gives successful output for the first two lines in the begin statement. The third line returns nothing. How do I get AWK to execute the contents of the variable as a command?
I have done many searches on the Internet and on this site with a variety of search terms. I have read the relevant section of sed & awk (Dougherty, & Robbins, 1997). What am I missing? Is this possible?
Here is what I am trying to do:
cat test.txt
exptransform=(
'print $9 - $8'
'print 10^($9 - $8)'
)
for (( h=0 ; h<${#exptransform[@]} ; ++h )) ; do
awk -v awk_exptransform="${exptransform[h]}" 'BEGIN{FS = "\t"} {if ( $4~/1/ && $5~/2/ && $6~/1/ && $7~/1/ && $2!~/20160630_0609/ && $2!~/20170208_2324/ && $2!~/20160804_0635/) awk_exptransform}' test.txt
done
Desired output:
subject run day_1_date include night tone_delivered sleep_arousal tone_1 tone_n
00105 20170413_0406 20170411 1 2 1 1 70 105
00105 20170413_0535 20170411 1 2 1 1 70 70
35
0
99999999999999996863366107917975552
1
Actual output:
subject run day_1_date include night tone_delivered sleep_arousal tone_1 tone_n
00105 20170413_0406 20170411 1 2 1 1 70 105
00105 20170413_0535 20170411 1 2 1 1 70 70
Sincerely,
Dante Picchioni