Community discussions

MikroTik App
 
Admin
just joined
Topic Author
Posts: 5
Joined: Tue Jan 24, 2023 11:29 am

Change word in comment

Tue Jan 24, 2023 12:15 pm

Hi,

I would need a script that would change one word in the comment, which is always the same, while the other words should remain unchanged.

like this
Roterboard now
set [ find default-name=sfp1 ] comment=lalalal_WORDBEFORE_lalalal name=Gi0/1
set [ find default-name=sfp2 ] comment=vavavav_WORDBEFORE_vavavav name=Gi0/2
set [ find default-name=sfp3 ] comment=dvdvdvd_WORDBEFORE_dvdvdvd name=Gi0/3

Roterboard after script
set [ find default-name=sfp1 ] comment=lalalal_WORDAFTER_lalalal name=Gi0/1
set [ find default-name=sfp2 ] comment=vavavav_WORDAFTER_vavavav name=Gi0/2
set [ find default-name=sfp3 ] comment=dvdvdvd_WORDAFTER_dvdvdvd name=Gi0/3

The script should search for the interface with the keyword "WORDBEFORE" in the comment and change it to "WORDAFTER"


Thanks for your help :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Change word in comment

Tue Jan 24, 2023 5:22 pm

and what is needed? it's easy!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Change word in comment

Tue Jan 24, 2023 5:44 pm

search tag # rextended Search and replace function:
:global searep do={
    :local input [:tostr $1] ; :local search  [:tostr $2] ; :local replace [:tostr $3]
    :local start -1 ; :local tmppos 0 ; :local sx "" ; :local dx ""
    :while ([:typeof [:find $input $search $start]] = "num") do={
        :set tmppos [:find $input $search $start]
        :set sx     [:pick $input 0 $tmppos]
        :set dx     [:pick $input ($tmppos + [:len $search]) [:len $input]]
        :set start  ([:len "$sx$replace"] - 1)
        :set input  "$sx$replace$dx"
    }
    :return $input
}

How to use that function:

example code

/interface
{
:local searchthis "WORDBEFORE"
:local replcwthis "WORDAFTER"
    :foreach item in=[find where !dynamic and type="ether" and comment~$searchthis] do={
:put [get $item comment]
        set $item comment=[$searep [get $item comment] $searchthis $replcwthis]
:put [get $item comment]
    }
}
Last edited by rextended on Wed Jan 25, 2023 12:10 pm, edited 1 time in total.
 
Admin
just joined
Topic Author
Posts: 5
Joined: Tue Jan 24, 2023 11:29 am

Re: Change word in comment

Tue Jan 24, 2023 8:13 pm

Hi,

I tried to run script but result is empty comment...
What i did wrong?

Tnx

search tag # rextended Search and replace function:
:global searep do={
    :local input   [:tostr $1]
    :local search  [:tostr $2]
    :local replace [:tostr $3]
    :local start -1 ; :local tmppos 0 ; :local sx "" ; :local dx ""
    :while ([:typeof [:find $input $search $start]] = "num") do={
        :set tmppos [:find $input $search $start]
        :set sx     [:pick $input 0 $tmppos]
        :set dx     [:pick $input ($tmppos + [:len $search]) [:len $input]]
        :set start  ([:len "$sx$replace"] - 1)
        :set input  "$sx$replace$dx"
    }
    :return $input
}

How to use that function:

example code

/interface
{
:local searchthis "WORDBEFORE"
:local replcwthis "WORDAFTER"
    :foreach item in=[find where !dynamic and type="ether" and comment~$searchthis] do={
:put [get $item comment]
        set $item comment=[$searep [get $item comment] $searchthis $replcwthis]
:put [get $item comment]
    }
}
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Change word in comment

Tue Jan 24, 2023 8:56 pm

Check if these three conditions are met:
in=[find where !dynamic and type="ether" and comment~$searchthis]
BR.
 
Admin
just joined
Topic Author
Posts: 5
Joined: Tue Jan 24, 2023 11:29 am

Re: Change word in comment

Tue Jan 24, 2023 9:28 pm

interface prin where !dynamic and type="ether"


Flags: D - dynamic, X - disabled, R - running, S - slave
# NAME TYPE ACTUAL-MTU L2MTU MAX-L2MTU MAC-ADDRESS
Gi1/1 ether 1500 1598 9498 D4:CA:6D:41:EC:C0
1 S Gi1/2 ether 1500 1598 9498 D4:CA:6D:41:EC:C1
2 S Gi1/3 ether 1500 1598 9498 D4:CA:6D:41:EC:C2
3 S ;;; lalalal_WORDBEFORE_lalalal
Gi1/4 ether 1500 1598 9498 D4:CA:6D:41:EC:C3
4 S Gi1/5 ether 1500 1598 9498 D4:CA:6D:41:EC:C4
5 S Gi1/6 ether 1500 1598 9498 D4:CA:6D:41:EC:C5
6 Gi1/7 ether 1500 1598 9498 D4:CA:6D:41:EC:C6
7 Gi1/8 ether 1500 1598 9498 D4:CA:6D:41:EC:C7
8 Gi1/9 ether 1500 1598 9498 D4:CA:6D:41:EC:C8
9 Gi1/10 ether 1500 1598 9498 D4:CA:6D:41:EC:C9
10 Gi1/11 ether 1500 1600 9500 D4:CA:6D:41:EC:CA
11 Gi1/12 ether 1500 1600 9116 D4:CA:6D:41:EC:CB
12 Gi1/13 ether 1500 1600 9116 D4:CA:6D:41:EC:CC



Check if these three conditions are met:
in=[find where !dynamic and type="ether" and comment~$searchthis]
BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Change word in comment

Tue Jan 24, 2023 9:36 pm

you paste both function and example on terminal, or only the example that use the function?

If you do not paste first the function, how to use it?

You have blank results also with this?
:foreach item in=[/interface find where !dynamic and type="ether" and comment~"WORDBEFORE"] do={ :put [/interface get $item comment] }
 
Admin
just joined
Topic Author
Posts: 5
Joined: Tue Jan 24, 2023 11:29 am

Re: Change word in comment

Wed Jan 25, 2023 11:25 am

you paste both function and example on terminal, or only the example that use the function?

If you do not paste first the function, how to use it?

You have blank results also with this?
:foreach item in=[/interface find where !dynamic and type="ether" and comment~"WORDBEFORE"] do={ :put [/interface get $item comment] }
With this code nothing happens
/interface
{
:local searchthis "WORDBEFORE"
:local replcwthis "WORDAFTER"
    :foreach item in=[/interface find where !dynamic and type="ether" and comment~"WORDBEFORE"] do={ :put [/interface get $item comment] }
:put [get $item comment]
        set $item comment=[$searep [get $item comment] $searchthis $replcwthis]
:put [get $item comment]
    }
}

I can give you access to one routerborad if you want to try?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Change word in comment

Wed Jan 25, 2023 12:09 pm

You read what you quoted?

You must use both script at same time, or why I write the function over the example, uselessly?
 
Admin
just joined
Topic Author
Posts: 5
Joined: Tue Jan 24, 2023 11:29 am

Re: Change word in comment

Wed Jan 25, 2023 1:48 pm

You read what you quoted?

You must use both script at same time, or why I write the function over the example, uselessly?
Ahh, sry... i now undstand the script, it works! Thanks a lot!

Who is online

Users browsing this forum: Luanscps and 14 guests