I want the awk to interpret the variable as follows
#!/bin/bash
file=tau
f=2.54
order=even
awk '{sum+=$2}; END {print '${file}_${f}_${order}_v1.xls', sum/NR}'
${file}_${f}_${order}_v1.xls >> safe/P-state-summary.xls
I want the desired output as follows -
tau_2.54_even_v1.xls sum/NR
Can anybody help me out with this ?
First, you need to export
environment variables if you want them to be passed in the environment of a child process like awk
.
Second, you can use ENVIRON["name"]
to get an environment variable in awk
. So the following works for me:
#!/bin/bash
export file=tau
export f=2.54
export order=even
awk '{sum+=$2}; END {print ENVIRON["file"] "_" ENVIRON["f"] "_" ENVIRON["order"] "_v1.xls", sum/NR}'
Don't forget that you can set "AWK variables" on commandline
awk -v FOO=bar '...<AWK code that uses the AWK variable FOO>...'
I think this is what you want:
#!/bin/bash
file=tau
f=2.54
order=even
awk "{sum+=\$2}; END {print \"${file}_${f}_${order}_v1.xls\", sum/NR}" \
${file}_${f}_${order}_v1.xls >> safe/P-state-summary.xls
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With