awk match whole word using a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk match whole word using a variable
# 1  
Old 08-19-2013
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


Code:
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.

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

How can I use a variable with a regular expression?

Thank you.
# 2  
Old 08-19-2013
are you using AWK for this?

if so you need to pass the variable into awk :

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

Hope this helps you.
# 3  
Old 08-19-2013
Code:
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
# 4  
Old 08-19-2013
Quote:
Originally Posted by Jotne
Code:
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
@Jotne
You are right, thanks. I am also in a learning phase!Smilie
# 5  
Old 08-19-2013
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):

Code:
(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[i],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";
          }
       }
    }
}

# 6  
Old 08-19-2013
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!
# 7  
Old 08-20-2013
Quote:
Originally Posted by RudiC
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Replacing the first word if the word three match

Dear ALL, I have sample file : IDcentos-forum,bash,linuxCentOS,GNome IEfedora-milis,cli,linuxRedhat,KDE IRfreebsd-milis,aix,unixbsd,pyton required output: centos,bash,linuxCentOS,GNome fedora,cli,linuxRedhat,KDE freebsd,aix,unixbsd,pyton Can you help me pls.. (1 Reply)
Discussion started by: gnulyn
1 Replies

4. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

6. Shell Programming and Scripting

Print a word after a match

Hi all, I have the below line: 08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \" >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1 I want to print just one word after the string "-env", in... (7 Replies)
Discussion started by: Cvg
7 Replies

7. Shell Programming and Scripting

Match exact Variable in Awk

I want to match exact string stored in Variable INO and print next two lines. I have written below code which partially matches: awk '/\'$VAR'/{getline; getline; print}' Will be thankful , if somebody can help in to resolve this problem. Please view this code tag video for how to use... (9 Replies)
Discussion started by: nitj
9 Replies

8. Shell Programming and Scripting

Substituting a word by different word in variable

Hello Exprets, I have a requirement where i have to subtitue a word with another word. $eachline| sed 's/*$//' a) $eachline will hold a value a path for example user/oracle/Test_admin/myfolder/bakup/part_bkptemp_part_bkp_repeated/list.txt b) $eachline| sed 's/*$//' will give me the... (3 Replies)
Discussion started by: aks_1902
3 Replies

9. Shell Programming and Scripting

Question on awk for finding the column number using a match word

Hi Guys, Please help me out in my situation of writing a shell script Exampl:I have a output like asnapply 1 2 3 apply_server=1 apply_schema=ASN asnapply 1 2 3 apply_server=2 apply_schema=ASN Now i need output like asnacmd applysever=1 applyschema=ASN stop asnacmd applysever=2... (16 Replies)
Discussion started by: mallak
16 Replies

10. Shell Programming and Scripting

How to match a exact word in a variable ????

Hi All, Str="online maintenance" if then perform some action I know the above comparison is wrong ..... Actually I am very new to this solaris.. I want to check for online in a variable and perform some action based on that How can I do it? (6 Replies)
Discussion started by: vijaysachin
6 Replies
Login or Register to Ask a Question