View previous topic :: View next topic |
Author |
Message |
pacho2 Developer
Joined: 04 Mar 2005 Posts: 2599 Location: Oviedo, Spain
|
Posted: Thu Mar 21, 2019 11:11 am Post subject: awk: compare two variables containing parenthesis |
|
|
Hello, I am having issues to compare two variables when they contain a parenthesis. For example this case works fine:
Code: | BEGIN{
new="test"
global="test"
if(global~new) {print "Contains"} else {print "Doesn't contain"}
}
$ awk -f test.awk pp
Contains
|
But this fails: Code: |
BEGIN{
new="test(M)"
global="test(M)"
if(global~new) {print "Contains"} else {print "Doesn't contain"}
}
$ awk -f test.awk pp
Doesn't contain
|
I have tried to escape (with double \\) the parenthesis but still doesn't work.
Thanks a lot for your help |
|
Back to top |
|
|
guitou Guru
Joined: 02 Oct 2003 Posts: 534 Location: France
|
Posted: Thu Mar 21, 2019 1:34 pm Post subject: |
|
|
Hello.
You probably made a mistake when trying without double backslahes... seems to work on my comp:
Code: |
$ cat bin/test.awk
#!/usr/bin/awk
BEGIN{
global="test(M)"
new="^test\\(M\\)$"
if(global ~ new) {print global " matches " new} else {print global " doesn't match " new}
}
$ awk -f bin/test.awk
test(M) matches ^test\(M\)$
|
++
Gi) |
|
Back to top |
|
|
pacho2 Developer
Joined: 04 Mar 2005 Posts: 2599 Location: Oviedo, Spain
|
Posted: Thu Mar 21, 2019 3:52 pm Post subject: |
|
|
I think the problem is related with the new variable needing to escape the () but not the "global" one, because I am checking in a bigger script with this logic and it still fails (as that variable with parenthesis comes from another two variables like newvar=name"("value")"
The idea is to append "newvar" to "global" only when its values are not already there (this is inside a loop):
if(global!~new) {global=global""new";"}
But I guess now I will need to set a "new_to_compare" variable with the escapes while still appending the one without
new_to_compare=name"\\("value"\\)"
if(global!~new_to_compare) {global=global""new";"} |
|
Back to top |
|
|
pacho2 Developer
Joined: 04 Mar 2005 Posts: 2599 Location: Oviedo, Spain
|
Posted: Thu Mar 21, 2019 4:06 pm Post subject: |
|
|
Yes, that works
Thanks! |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21633
|
Posted: Fri Mar 22, 2019 1:43 am Post subject: |
|
|
If you want substring matching, wouldn't it be easier to use index to perform a substring search instead of trying to coerce the candidate string to be regex-safe? |
|
Back to top |
|
|
krinn Watchman
Joined: 02 May 2003 Posts: 7470
|
Posted: Fri Mar 22, 2019 2:43 pm Post subject: |
|
|
agree with Hu, you're fixing the () case, but any special regex case will hit you
you're using a function to compare left argument with the regex result of right argument, not quiet sane for a string comparaison
And you can see how weak it is, if new is just an empty string
Code: | cat bin/test.awk
#!/usr/bin/awk
BEGIN{
global="test(M)"
new=""
if(global ~ new) {print global " matches " new} else {print global " doesn't match " new}
}
$ awk -f bin/test.awk
test(M) matches
|
|
|
Back to top |
|
|
pacho2 Developer
Joined: 04 Mar 2005 Posts: 2599 Location: Oviedo, Spain
|
Posted: Sat Mar 23, 2019 2:08 pm Post subject: |
|
|
OK, thanks for the suggestion, I will look into it |
|
Back to top |
|
|
|