awk match whole word using a variable

Hello,
I am writing a script in awk where I need to match a whole word that is stored inside a variable.
For example:
I am working on a text that looks like this, and I want to print the second row:

sfasfsomethingsfasf
this is something I can use
this is not somethingIcanuse

wordToMatch="something";
if ($0 ~/\<wordToMatch\>/) print "OK";

This does not work, but when I put the actual string inside, it does work, but I can't use it.

if ($0 ~/\<something\>/) print "OK";

How can I use a variable with a regular expression?

Thank you.

are you using AWK for this?

if so you need to pass the variable into awk :

awk -v var="$wordToMatch"  ' $0 ~ /var/ {printf("%s", "OK")}

Hope this helps you.

var="test"
echo "here is a test line" | awk '$0~temp {print "variable found"}' temp=$var
variable found

You need to read the variable into awk like this
awk -v temp=$var 'awk program' file
or
awk 'awk program' temp=$var file

@capitanui
$0 ~ /var/ not correct
$0 ~ var correct
You also miss a ' at the end

@Jotne
You are right, thanks. I am also in a learning phase!:slight_smile:

Thanks for your answers, but I need this to be part of the "action" part, not the pattern part.
Here's my actual code, which does not work (sorry for complicating):

(FNR==1 && FILENAME !~ "module_"topModuleName) {
  for (wireName in wiresArray)
    {
     split(wiresArray[wireName],sk1," ");
     if (sk1[1] == "reg" || sk1[1] == "wire") # replace regs and wires only
       {
        split(wireModulePins[wireName],sk2,"@@");

        # go over all the wires' module pins
        for (i=1;i<=length(sk2),;i++)
          {
           split(sk2,sk3,",");
           modName=sk3[1];
           pinName=sk3[2];

           # see if the wire is connected to a pin in the module whose file is currently being parsed
           if ((FILENAME ~ "module_"modName) && ($0 ~ /\<pinName\>))
             pinDir="input";
          }
       }
    }
}

This is not the entire actual code - where do you define the variable? And - what's the slash for in front of pinName? Please be aware of the difference between regex constants and regex variables!

This is the actual code.
I tried using pinName as the variable, but obviously this is not the way do it.
How can I use a variable in a regexp?

Thank you.

You didn't read Jotne's post.

What did I miss there?

How to read variable in to awk!!!
You are searching for string <pinName> , not for an varibale named pinName.
Use option -v or variable behind awk

Sorry, but I must have not explained myself correctly.
I don't need to read a variable to awk.
The variable is already an awk variable.
Look at the code I posted. It is all 'awk' code. No shell involved there.

Thanks.

I do suggest you post your input file, how you like to output to be and how to get there.
This way we can more understand what you are doing.

It is a little more complicated than that.
I am not making small manipulations on texts.
I tried to narrow it down to the relevant part.

I did some search and I realized there is no way to use a variable
inside a regular expression, so I worked around the problem.

Thank you.