0

I have a command which is using awk with a variable:

LETTER='kiwi'
awk -v string="$LETTER" '/string/{ print $0 }' file.txt

But it seems I can't pass the value $LETTER to the awk. What I have tried is:

LETTER='kiwi'
awk -v string="$LETTER" '\/string\/{ print $0 }' file.txt

What I would like to achieve: Pass to the awk command whatever variable I need.

Many thanks.

  • 1
    Does this answer your question? [How to match a pattern given in a variable in awk?](https://stackoverflow.com/q/39384283/7939871) – Léa Gris Mar 22 '22 at 10:31
  • 1
    Many thanks @LéaGris my final solution was: ```awk -v string="$LETTER" '$0~string{ print $0 }' file.txt``` – LearningPath Mar 22 '22 at 16:44

0 Answers0