1

I have a file location_data.py, which contains a string variable

loc = "somewhere-$xyz"

In my shell, I have a variable name 'xyz' which has a value of 1. When I do:

>loc=$(awk '/loc/{print $3}' location_data.py)

I get

"somewhere-${xyz}"

instead, I want

"somewhere-1"

enter image description here

ANUJ JHA
  • 43
  • 1
  • 6

1 Answers1

1

You need to pass shell variable to awk by passing it to an awk variable then search inside /.../ will also not work.

use like:

locNew=$(awk -v locVar="$loc" '$0 ~ locVar{print $3}' location_data.py)

OR

locNew=$(awk -v locVar="$loc" 'index($0,locVar){print $3}' location_data.py)

Also try to create a new shell variable(as a Good programming practice), in case you want to refer them in later on.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93