inserting variables within my awk statement

awk, bash

Solution

You can use -v option:

ZONE=`date "+%Z %Y"`
DAY=`date "+%a"`
awk -vzone="$ZONE" -vday="$DAY" 'BEGIN { print zone, day }'

Problem

Here is a snippet of my awk statement..I'm trying to insert these 2 variables in the statement but they are not getting evaluated. Can someone point me in the right direction? ``` ZONE=`date "+%Z %Y"` DAY=`date "+%a"` awk '{if (NR<2) {print "["$1, $2, $3"]"}}' ``` I'm trying this: ``` awk '{if (NR<2) {print "[" $DAY, $1, $2, $3, $ZONE "]"}}' ``` This tip here helped solve my problem. Protect the shell variables from awk by enclosing them with "'" (i.e. double quote - single quote - double quote). awk '{print "'"$VAR1"'", "'"$VAR2"'"}' input_file

Original source