Community discussions

MikroTik App
 
panosdoulkaris
just joined
Topic Author
Posts: 8
Joined: Sun May 17, 2020 11:07 am

Is Find and Replace Possible?

Mon Feb 01, 2021 12:44 pm

Hi all!

Im looking to figure out a way to find and replace strings inside /system note

im making scripts for configuration updates and it works but the only problem is versioning.
right now i have a note e.g.
Router Mode
- Base-Config=V1.5
- VPN=V1.8
- etc,etc....
i want to find a way when i update config e.g. Base-Config from V1.5 to V1.6 to replace /system note -> only Base-Config value (1.5->1.6)
its not possible to replace the whole note because e.g. the first router has Base-Config and VPN the second has Base-Config and something else
there is no problem to change note structure e.g. from -Base-Config=V1.5 to -Base-Config=1.5 or something else.
Is it possible?
 
sin3vil
newbie
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

Re: Is Find and Replace Possible?  [SOLVED]

Mon Feb 01, 2021 1:17 pm

Hi,

Below code produces the below. You can then set it as the note message. It's limited to 4096 characters (including new lines).
local note [sys note get note]
put $note
local baseconfigPosition [find $note "Base-Config"]
local pre [pick $note 0 ($baseconfigPosition+12)]
local oldVersion [pick $note ($baseconfigPosition+12) [find $note "\r\n" ($baseconfigPosition+12)]]
local after [pick $note ($baseconfigPosition+12+[len $oldVersion]) [len $note]]
local newVersion "V1.10"
local newNote ($pre.$newVersion.$after)
put $newNote
Result:
Router Mode
- Base-Config=V1.5
- VPN=V1.8
- etc,etc....

Router Mode
- Base-Config=V1.10
- VPN=V1.8
- etc,etc....
What you're doing is loading existing note in a variable, then using :pick to split the note into three parts, a "pre" part, the oldversion and an "after" part.
You can then concatenate the pre and after parts along with the new version.
 
panosdoulkaris
just joined
Topic Author
Posts: 8
Joined: Sun May 17, 2020 11:07 am

Re: Is Find and Replace Possible?

Mon Feb 01, 2021 2:29 pm

You are the best!!! it perfectly works!!!
Just to understand what you just did:
#Get Note
local note [sys note get note]

#Put note (can be removed)
put $note

#Find where is Base-Config (in my example character 15)
local baseconfigPosition [find $note "Base-Config"]

#Set to pre (from note character 0 to note baseconfigPosition (+12 is for the length of characters from B to = )
# So in my example pre is (Router Mode\r\n- Base-Config=)
local pre [pick $note 0 ($baseconfigPosition+12)]

#set to old version after "pre" up to first /r/n (in my example V1.5)
local oldVersion [pick $note ($baseconfigPosition+12) [find $note "\r\n" ($baseconfigPosition+12)]]

#set to "after" after /r/n up to the end
local after [pick $note ($baseconfigPosition+12+[len $oldVersion]) [len $note]]

local newVersion "V1.10"
local newNote ($pre.$newVersion.$after)
put $newNote
Is it right?

I `ve changed a little bit (removed dots etc)
local note [/system note get note]
local baseconfigPosition [find $note "Base-Config"]
local pre [pick $note 0 ($baseconfigPosition+12)]
local oldVersion [pick $note ($baseconfigPosition+12) [find $note "\r\n" ($baseconfigPosition+12)]]
local after [pick $note ($baseconfigPosition+12+[len $oldVersion]) [len $note]]
local newVersion "V1.10"
/system note set note="$pre$newVersion$after"
 
sin3vil
newbie
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

Re: Is Find and Replace Possible?

Mon Feb 01, 2021 7:48 pm

Yes, comments are correct.

You can possibly make this into a generic function to change any value, assuming an .ini like key=value format.
i.e.
global editNote do={

#first argument is the key, second is the new value
local key $1
local newValue $2

#Get Note
local note [sys note get note]

#Find where is Base-Config (in my example character 15)
local keyPosition [find $note $key]
#key length + 1 for the equal sign
local keyLength ([len $key]+1)

#Set to pre
local pre [pick $note 0 ($keyPosition+$keyLength)]

#get old value, just to calculate its length
local oldValue [pick $note ($keyPosition+$keyLength) [find $note "\r\n" ($keyPosition+$keyLength)]]
#oldValue length
local oldValueLength [len $oldValue]

#set to "after" after /r/n up to the end
local after [pick $note ($keyPosition+$keyLength+$oldValueLength) [len $note]]


local newNote ($pre.$newValue.$after)
put $newNote
}

using it :

[admin@sini2] > put [sys note get note] 
Router Mode
- Base-Config=V1.5
- VPN=V1.8
- etc,etc....
[admin@sini2] > $editNote VPN "custom version"
Router Mode
- Base-Config=V1.5
- VPN=custom version
- etc,etc....
 
panosdoulkaris
just joined
Topic Author
Posts: 8
Joined: Sun May 17, 2020 11:07 am

Re: Is Find and Replace Possible?

Thu Feb 04, 2021 9:45 am

Thats even more great because you can do this with just a line of code instead of adding the same script. Thank you very much!

Who is online

Users browsing this forum: No registered users and 14 guests