Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
awk: compare two variables containing parenthesis
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
pacho2
Developer
Developer


Joined: 04 Mar 2005
Posts: 2599
Location: Oviedo, Spain

PostPosted: Thu Mar 21, 2019 11:11 am    Post subject: awk: compare two variables containing parenthesis Reply with quote

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
View user's profile Send private message
guitou
Guru
Guru


Joined: 02 Oct 2003
Posts: 534
Location: France

PostPosted: Thu Mar 21, 2019 1:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
pacho2
Developer
Developer


Joined: 04 Mar 2005
Posts: 2599
Location: Oviedo, Spain

PostPosted: Thu Mar 21, 2019 3:52 pm    Post subject: Reply with quote

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
View user's profile Send private message
pacho2
Developer
Developer


Joined: 04 Mar 2005
Posts: 2599
Location: Oviedo, Spain

PostPosted: Thu Mar 21, 2019 4:06 pm    Post subject: Reply with quote

Yes, that works :D

Thanks!
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21633

PostPosted: Fri Mar 22, 2019 1:43 am    Post subject: Reply with quote

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
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 7470

PostPosted: Fri Mar 22, 2019 2:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
pacho2
Developer
Developer


Joined: 04 Mar 2005
Posts: 2599
Location: Oviedo, Spain

PostPosted: Sat Mar 23, 2019 2:08 pm    Post subject: Reply with quote

OK, thanks for the suggestion, I will look into it :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum