1

I have data like the following:

apple pear sugar pizza
pizza chicken pasta turkey

I want to grep for "pizza" but only grep the fourth column. I know i can use:

awk '$4 == "pizza"' 

However i have a variable named food which contains the word pizza. When i do

food=pizza
awk '$4 == "$food"'

I receive an error and also when i change to double apostrophes i receive this error:

awk: line 1: syntax error at or near ==

how can i achieve this?

thomas
  • 13

1 Answers1

0

Your shell variable won't expand inside the single quotes used for the awk program.

You can pass a shell variable to awk using the -v option:

food=pizza
awk -v food="$food" '$4 == food'
jesse_b
  • 37,005