Awk - Using a Shell Variable in the Reg Expression

Hi all,

I have a shell variable $test1 that holds a value derived from some other processing. What I need to do is use that $test1 as the input to a awk regular expression:

nawk -F"," -v tester=$test1 '
/tester/{
print $0
}
' $inputFile

So what I have is tester which def has a value. I want to search in the $inputFile for the string in tester but Im presuming /tester/ will look for the literal string 'tester' rather then whats contained within it which is the desired string,

Can someone help out with this,

Thanks,

nawk -F',' -v tester=$test1 '
   $0 ~ tester { print $0 }' $inputFile

Hi thanks for the quick response,

After adding the code suggested I am getting a response but it seems to be returning more than I expected. The value of tester is "qwerty1" and in the
$inputFile there is only 1 line with this value but the code is returning all the lines from the input...

Any further suggestions?

post a snippet from the '$inputFile' and the value of '$tester'

Hi,

tester contains: COMM_VOL.TEST
and the sample input file:

,qwerty,qwerty,None,True,0,% ANNU actual/actual,0,False,@Constant,,,:,False,@Linear(),2006/1
0/06,Option Time,
,,,,,,,,,,,,,,,,2006/11/16,0.28
,,,,,,,,,,,,,,,,2006/12/16,0.28
,COMM_VOL.TEST1,COMM_VOL.TEST1,None,True,0,% ANNU actual/actual,0,False,@Constant,,,:,False,@Linear(),2006/1
0/06,Option Time,
,,,,,,,,,,,,,,,,2006/11/16,0.28
,,,,,,,,,,,,,,,,2006/12/16,0.28
,COMM_VOL.TEST2,COMM_VOL.TEST2,None,True,0,% ANNU actual/actual,0,False,@Constant,,,:,False,@Linear(),2006/1
0/06,Option Time,
,,,,,,,,,,,,,,,,2006/11/16,0.28
,,,,,,,,,,,,,,,,2006/12/16,0.28

Therfore I would expect tester to match just the lines in bold & italics but the whole lot is coming back...even the lines with the "," and the line with qwerty in it,

not4,

Here is a ksh subroutine that works for me. The ksh variables for Fs, Col_numb and Pattern must be set before calling the subroutine.

print_line_containing_pattern () {
cat $Infile | $Awk -F"$Fs" -v col=$Col_numb -v pat="$Pattern" '$(col) ~ pat {print $0}'
}
}

My Awk variable is set to

#Awk=/usr/bin/gawk                      # cygwin
Awk="/usr/xpg4/bin/awk"                 # posix (sol)
#Awk="/usr/bin/awk"                     # old awk (sol)
#Awk="/usr/bin/nawk"                    # new awk (sol)

Good Luck !
KW

Heya,
works just fine for me under Solaris. Try this:

nawk -F',' -v tester="$test1" '$0 ~ tester { print $0 }' $inputFile

Note: next time you post either your code samples and/or text file samples, pls do use vB Codes - it makes it easier to read and does convey the unformated text.