Scripting - FIND with Wildcard

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.

I think the “:” may be a special character in regex. Plus, you’d be searching for all CONTAINING, not STARTING, so try:
:put [find where comment~“^BP:”]or if that doesn’t work, try to make it more explicit, like:
:put [find where comment~“^BP:.*$”]

P.S. I like trains…

If its in a script the $ needs to be escaped also.
:put [find where comment~“^BP:.*$”] .. I’m not sure if the : needs to be escaped. You’ll just have to try it.

.(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…

yes, (dollar) has to be escaped

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?

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.
: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"
}

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)

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
	}
}