Page 1 of 1

Scripting - FIND with Wildcard

Posted: Mon Sep 02, 2013 3:37 pm
by TheWiFiGuy
Hi

Im trying (unsuccessfully) to find some comments based on using a wildcard:

I have some hotspot ip-binding(s) that have a comment such as 'BP:9'

I simply need a way to find the comments that start 'BP:' , so that I can decrease the number after BP: and when it reaches 0, i'll remove the entry.

I just cant find the correct format of regex and the Where command...

This doesnt work:
:put [find where comment~"BP:"]
any help much appreciated.

Re: Scripting - FIND with Wildcard

Posted: Mon Sep 02, 2013 3:41 pm
by boen_robot
I think the ":" may be a special character in regex. Plus, you'd be searching for all CONTAINING, not STARTING, so try:

ros code

:put [find where comment~"^BP\:"]
or if that doesn't work, try to make it more explicit, like:

ros code

:put [find where comment~"^BP\:.*$"]


P.S. I like trains...

Re: Scripting - FIND with Wildcard

Posted: Mon Sep 02, 2013 4:27 pm
by efaden
I think the ":" may be a special character in regex. Plus, you'd be searching for all CONTAINING, not STARTING, so try:

ros code

:put [find where comment~"^BP\:"]
or if that doesn't work, try to make it more explicit, like:

ros code

:put [find where comment~"^BP\:.*$"]


P.S. I like trains...

If its in a script the $ needs to be escaped also.

ros code

:put [find where comment~"^BP\:.*\$"]
.. I'm not sure if the : needs to be escaped. You'll just have to try it.

Re: Scripting - FIND with Wildcard

Posted: Mon Sep 02, 2013 4:59 pm
by janisk
.(dot) *(star) $(dollar) all are special symbols where (dot) - one symbol; (star) - previous symbol repeated none or any times; (dollar) - end of string.

if you escape them, then script will look for the symbol inside the string and script will not work.

Re: Scripting - FIND with Wildcard

Posted: Mon Sep 02, 2013 5:17 pm
by efaden
.(dot) *(star) $(dollar) all are special symbols where (dot) - one symbol; (star) - previous symbol repeated none or any times; (dollar) - end of string.

if you escape them, then script will look for the symbol inside the string and script will not work.
If you don't escape the $ in the string then inside of a script it tries to match it for a variable...

Re: Scripting - FIND with Wildcard

Posted: Tue Sep 03, 2013 2:59 pm
by janisk
yes, (dollar) has to be escaped

Re: Scripting - FIND with Wildcard

Posted: Wed Sep 04, 2013 8:14 am
by TheWiFiGuy
Thank you guys,

I can now run this fine from the terminal, but when pasting my code into a script , I have a problem with the Tilde character directly after the where statement causing problems.

comment~"^BP\:.*$"]
^-This character gets flagged up when running /sys scr print as being bad and stops the script.

How do I go about escaping that?

Re: Scripting - FIND with Wildcard

Posted: Wed Sep 04, 2013 11:18 am
by boen_robot
You shouldn't be escaping the "~". It's what tells the scripting engine this is a regex match.

How does your whole line look like?

Re: Scripting - FIND with Wildcard

Posted: Wed Sep 04, 2013 3:26 pm
by efaden
You shouldn't be escaping the "~". It's what tells the scripting engine this is a regex match.

How does your whole line look like?
I don't think you actually need the where in the script. Show us the full script. But for example the below code works fine.

ros code

:foreach i in [/interface ethernet find comment=~"^BP:.*\$"] do={
 :put "test"
}

Re: Scripting - FIND with Wildcard

Posted: Sun Jun 16, 2019 11:40 pm
by Jotne

ros code

:foreach i in [/interface ethernet find comment=~"^BP:.*\$"] do={
 :put "test"
}
PS script does not work with both ~ and =

So it should be:
:foreach i in [/interface ethernet find comment~"^BP:.*\$"] do={
 :put "test"
}

Re: Scripting - FIND with Wildcard

Posted: Wed Jan 13, 2021 5:52 pm
by Fantomas
Escaping \$ works fine. But I must be missing something since escaping a dot like \. gives me a syntax error.
I'd like to prevent script from potentially finding files that have longer extensions:
{
:local mm "01"
:local yyyy 2021 
:foreach FILE in=[/file find name~"^disk1/backups/$yyyy-$mm/.+\.txt\$" ] do={
:local name [/file get $FILE name];
:put $name;
}
}

Error: syntax error (line 4 column 63)

Re: Scripting - FIND with Wildcard

Posted: Thu Jan 14, 2021 9:14 pm
by Jotne
Double escape the dot. You can also remove the ; at end of each line (only needed when multiple commands on same line).
{
:local mm "01"
:local yyyy 2021 
:foreach FILE in=[/file find name~"^disk1/backups/$yyyy-$mm/.+\\.txt\$" ] do={
	:local name [/file get $FILE name]
	:put $name
	}
}