Celeb Glow
news | March 29, 2026

Pass a variable in awk command

 spke="myvaribale" var="cidr" val=$azure_spoke2_vnet_cidr clp="azure_vnets"

I am placing these variable in this command : I dont want to write this myvaribale in awk rather I want to use $spke variable there when I use directly in command its not working.

 awk -v spke="$spke" -v var="$var" -v val="$val" -v clp="$clp" '/variable/ { cloudp=gensub(/(^variable[[:space:]]")(.*)(".*$)/,"\\2",$0) } /myvaribale[[:space:]]=/ { spoke=$1 } spoke==spke && $1==var && cloudp ~ clp { $0=gensub(/(^.*=[[:space:]]")(.*)(".*$)/,"\\1"val"\\3",$0) }1' 

1 Answer

First, note that /regexp/ is shorthand for $0 ~ /regexp/.

In the $0 ~ /regexp/ form, you can replace /regexp/ (in which the thing between slashes is a string constant) by a computed regexp using string concatenation:

$0 ~ spke "[[:space:]]="

where spke is the awk variable that you initialized on the command with -v spke="myvariable"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy