1

This question comes from this other.

Case scenario for my Linux shell script:

$ cat test.txt
C1    C2    C3
1     a     snow
2     b     snowman
snow     c     sowman

Searching for lines with third field containing "snow" works OK:

$ awk '$3 ~/snow/' test.txt
1     a     snow
2     b     snowman

But I need to do it by using variables:

$ word="snow"

$ echo $word
snow

$ awk -v variable="snow" '$3 ~/variable/' test.txt

$ awk -v variable="$word" '$3 ~/variable/' test.txt

$

As can be seen, there are no results.

How could I perform AWK search variable-based?

Cyrus
  • 77,979
  • 13
  • 71
  • 125
Sopalajo de Arrierez
  • 3,261
  • 4
  • 31
  • 48

1 Answers1

2

You should change $3 ~/variable/ TO $3 ~ variable since a /../ contains regexp not variables. So in your case it will try to search "a string" named variable NOT "a variable" named variable.

RavinderSingh13
  • 117,272
  • 11
  • 49
  • 86