Community discussions

MikroTik App
 
User avatar
TheWiFiGuy
Member
Member
Topic Author
Posts: 351
Joined: Thu Nov 24, 2011 7:26 pm
Location: UK

Scripting - FIND with Wildcard

Mon Sep 02, 2013 3:37 pm

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.
----------------------
Mikrotik Consultant.
MTCNA, MTWCE, MTCTCE, MTCRE, MTCINE
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Scripting - FIND with Wildcard

Mon Sep 02, 2013 3:41 pm

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...
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Scripting - FIND with Wildcard

Mon Sep 02, 2013 4:27 pm

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.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6284
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Scripting - FIND with Wildcard

Mon Sep 02, 2013 4:59 pm

.(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.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Scripting - FIND with Wildcard

Mon Sep 02, 2013 5:17 pm

.(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...
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6284
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Scripting - FIND with Wildcard

Tue Sep 03, 2013 2:59 pm

yes, (dollar) has to be escaped
 
User avatar
TheWiFiGuy
Member
Member
Topic Author
Posts: 351
Joined: Thu Nov 24, 2011 7:26 pm
Location: UK

Re: Scripting - FIND with Wildcard

Wed Sep 04, 2013 8:14 am

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?
----------------------
Mikrotik Consultant.
MTCNA, MTWCE, MTCTCE, MTCRE, MTCINE
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Scripting - FIND with Wildcard

Wed Sep 04, 2013 11:18 am

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?
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Scripting - FIND with Wildcard

Wed Sep 04, 2013 3:26 pm

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"
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1878
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Scripting - FIND with Wildcard

Sun Jun 16, 2019 11:40 pm

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"
}
 
Why do not use Splunk to monitor your MikroTik Router(s)? Look at this page in how to set it up.

MikroTik->Splunk
 
 
Fantomas
just joined
Posts: 2
Joined: Wed Jan 13, 2021 12:36 pm

Re: Scripting - FIND with Wildcard

Wed Jan 13, 2021 5:52 pm

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)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1878
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Scripting - FIND with Wildcard

Thu Jan 14, 2021 9:14 pm

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
	}
}
 
Why do not use Splunk to monitor your MikroTik Router(s)? Look at this page in how to set it up.

MikroTik->Splunk
 

Who is online

Users browsing this forum: Bing [Bot] and 49 guests