Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Reg Expressions 1

Status
Not open for further replies.

jescat

Technical User
Jul 29, 2004
32
US
I am tring to create a script with a dynamic regex however awk doesn't seem to like comparing a variable to a regex stored in a variable. For example:

regex = "/^abc/"
if ( $0 ~ regex ) {
print 1
} else {
print 0
}

Whether or not the if condition is true or not the script always returns false is there something I'm missing.

Thanks
 
JesCat-

The awk interpretor is parsing your string as if it were a pattern match.

You can do what you want to do by passing the string in from the command line by a couple of methods:

#!/bin/sh

regex=$2

awk ' {

if ( $0 ~ '"$regex"' ) {
print 1
} else {
print 0
}
}' $1

From the command line in the shell type:

awkfile inputfile /^abc/

This will pass the second shell command line argument into the awk script, or alternately:
awk '{

if ( $0 ~ regex ) {
print 1
} else {
print 0
}
}' infile

awk -f awkfile -v regex="/^abc/"

Hope this helps.

Jesse

flogrr
flogr@yahoo.com

 
From a little thing I was working on:
Seems like it might be useful here.


function define_a_phrase(s,l,c, ret) {
if (length(s) > l) {
my_str = substr(s,1,l)
if (my_str ~ c) {
printf "No acceptable content: %3s:(%s=pat)\n", my_str,c
return
} else {
ret = my_str
return ret
}
}
return 1
}
BEGIN {
printf "MAXLength of string: "
getline l < &quot;-&quot;
printf &quot;Pattern to match: &quot;
getline pat < &quot;-&quot; ;# ex /[a-z]/
}

{
all = define_a_phrase($0,l,pat)
if (all) {
print all
}
}
 
These solutions don't seem to be working for my situation here is my script:

IsTrue() {
TRUEYN=`echo ${1} | awk -v regex=&quot;${2}&quot; '{if ($0 ~ '&quot;regex&quot;') print 1}'`
# TRUEYN=`echo ${1} | awk -f /home/stateje/test.awk -v regex=$2`
echo $TRUEYN
}
unixvar=&quot;abcdef&quot;
IsTrue ${unixvar} /^abc/
if (( `IsTrue ${unixvar} /^abc/` )); then
echo true
else
echo false
fi

I find that regardless of whether is conditions is true or false is always returns false. After debugging I found that the regex is being pupulated with the correct infor(/^abc/) however the test doesn't work.
 
I don't think it's the test command. When you explicitly type the re in the awk script everything works fine.

JesCat
 
test [[ `IsTrue &quot;${unixvar}&quot; &quot;/^abc/&quot;` ]]
 
vgersh99

Thanks for all the help. Maybe I am confused, I don't think the issue is with:

if (( `IsTrue ${unixvar} /^abc/` ))

I think the issue is with:

if ( $0 ~ '&quot;regex&quot;' )

If I explicity type &quot;/^abc/&quot; instead of the variable regex the entire script works fine. However if the re is stored in a variable the &quot;if&quot; statement in the awk scripts always returns false.

If I am misunderstanding your solutions I apologize and any clarification would be appreciated.

Thanks
JesCat
 
IsTrue() {
# echo &quot;1: [$1]&quot;
# echo &quot;2: [$2]&quot;
TRUEYN=`echo ${1} | awk -v regex=&quot;${2}&quot; '{print ($0 ~ regex) ? 1 : 0 }'`
# TRUEYN=`echo ${1} | awk -f /home/stateje/test.awk -v regex=$2`
echo $TRUEYN
}
unixvar=&quot;abcdef&quot;
echo `IsTrue &quot;${unixvar}&quot; &quot;^abc&quot;`
if [[ `IsTrue &quot;${unixvar}&quot; &quot;^abc&quot;` = 0 ]]; then
echo true
else
echo false
fi
 
Got It!

Thanks Alot, what I gathered from your script it that when you pass a re you don't wrap it in &quot;/&quot; . Once I passed the re as ^abc not /^abc/ my script works fine. awk must implicitly wrap a re in &quot;/&quot; based on the operator.

Thanks
Alot for you help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top