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
  • 84,225
  • 14
  • 89
  • 153
Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52

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
  • 130,504
  • 14
  • 57
  • 93