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?
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!
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.