using variable as a pattern for matching 
Author Message
 using variable as a pattern for matching

Here's what I want to do:

BEGIN {
  str = xyx

Quote:
}

{
  if (/str/) # This will match the string literal 'str' !!!
    printa   # I want to match the string literal 'xyz'.
Quote:
}



Tue, 03 May 2005 09:25:47 GMT  
 using variable as a pattern for matching


X Here's what I want to do:
X
X BEGIN {
X   str = xyx
X }
X
X {
X   if (/str/) # This will match the string literal 'str' !!!
X     printa   # I want to match the string literal 'xyz'.
X }

BEGIN{str=".*xyz.*"}
$0 ~ str {print}

You could also use match(), or several other approaches

                                        Bob Harris



Tue, 03 May 2005 10:04:53 GMT  
 using variable as a pattern for matching

Quote:
> Here's what I want to do:

> BEGIN {
>   str = xyx
> }

> {
>   if (/str/) # This will match the string literal 'str' !!!
>     printa   # I want to match the string literal 'xyz'.
> }

1) As your code stands the variable str is set to the value of the
variable xyx in the BEGIN block, and the value of xyx will be set to the
default value of a null string, i.e., "".  In order to set the variable
str to the literal xyx, the xyx must be quoted using double quotes,
thus:

    BEGIN { str = "xyx" }

2) If your only required action is to print the value of $0, the current
record, when it contains the literal value "xyx" (which is contained in
the variable str), then the pattern should be checked outside the braces
that contain the action.  This can be achieved as follows:

    ($0 ~ str) { print $0 }

3) By default awk prints $0, so (2) can be shortened to:

    ($0 ~ str) { print }

4) Awk's default action is { print } so (3) can be shortened to:

    ($0 ~ str)

5) Putting this all together gives:

    BEGIN { str = "xyx" }
    ($0 ~ str)

which should do what you want.

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"



Tue, 03 May 2005 11:03:24 GMT  
 using variable as a pattern for matching

Quote:


> X Here's what I want to do:
> X
> X BEGIN {
> X   str = xyx
> X }
> X
> X {
> X   if (/str/) # This will match the string literal 'str' !!!
> X     printa   # I want to match the string literal 'xyz'.
> X }

> BEGIN{str=".*xyz.*"}
> $0 ~ str {print}

> You could also use match(), or several other approaches

>                                         Bob Harris

It is generally not a good idea to surround literals in a dynamic RE
with .* because it reduces the chance of the RE engine being able to
match the literal part of the pattern quickly.  This is particularly
true for NFA RE engines as the potential amount of backtracking is
reduced.  In other words, $0 ~ "xyx" is quite likely to run faster than
$0 ~ ".*xyx.*" and it will not run more slowly.

For more information on this topic see "Mastering Regular Expressions"
by Jeffrey Friedl, published by O'Reilly.

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"



Tue, 03 May 2005 11:11:38 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Using Variables in pattern matches

2. problems pattern matching a variable

3. how to use a shell variable in awk for pattern matching

4. variables in a pattern match

5. Variable Pattern Matching

6. variable in pattern match

7. Pattern matching using "re"

8. pattern matching using regular expressions

9. can prolog be used for pattern matching?

10. returning a part of string using pattern matching

11. iss-matching - the free Regular Expression / Pattern Matching cluster

 

 
Powered by phpBB® Forum Software