Variable filtering in awk

Hello all,
can you explain why this filter does not work, it prints all the lines in the file:

awk -v sel="TestString" 'sel'  file 

while these work:

awk '/TestString/' file
awk -v sel="TestString" '$0~sel'  file

Thanks!:slight_smile:

This

awk -v sel="TestString" '$0~sel'  file

is telling awk to filter - to do something!

In other words, my question is about how to write an awk statement that will use the variable and use it in the form of a statement like:

awk -v var="TestString" '/ var /' file

to achieve filtering.
Any suggestions?
Thanks!

Below awk prints all as its default action is to print everything if "sel" is true (not null or non-zero)...

awk -v sel="TestString" 'sel'  file

The awk listed below prints nothing as "sel" is false (null or zero)...

awk -v sel="" 'sel'  file

Just curious:
Is there anyway to get something 'like' this to work, where the var is used for the / / construct ?

awk -v var="TestString" '/ var /' file

Any suggestion is appreciated.

yes

awk -v var="TestString" '$0~var' file

/.../ is a regex constant; /var/ tests input lines for occurrence of a literal string "var". To match variables, you need to do as joeyg and Jotne suggest.