Community discussions

 
upnort
newbie
Topic Author
Posts: 44
Joined: Wed Aug 15, 2018 2:03 am

Splitting/parsing variable data  [SOLVED]

Tue Aug 20, 2019 7:43 pm

What is the command equivalent of bash awk or cut? I found the ":parse" function but am unable to get that to succeed.

When I run the following command:

:put [/system resource get version]

I see something like:

6.44.5 (long-term)

I want to return only the version number. That is, use the space as the delimiter.

Thank you! :)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1290
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Splitting/parsing variable data

Tue Aug 20, 2019 7:57 pm

This should do:
{
:local ver [/system resource get version]
:put [:pick $ver 0 [:find $ver " "]]
}

:local ver [/system resource get version] Get the line
[:find $ver " "] gets position of space
[:pick $ver 0 [:find $ver " "]] get text from position 0 to first space

See manual here:
https://wiki.mikrotik.com/wiki/Manual:Scripting
find :find <arg> <arg> <start> return position of substring or array element :put [:find "abc" "a" -1]
pick :pick <var> <start>[<end>] return range of elements or substring. If end position is not specified, will return only one element from an array. :put [:pick "abcde" 1 3]
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
pe1chl
Forum Guru
Forum Guru
Posts: 5697
Joined: Mon Jun 08, 2015 12:09 pm

Re: Splitting/parsing variable data

Tue Aug 20, 2019 8:50 pm

It would be nice when the scripting language was extended with some features known from e.g. Perl, like "split" and "regexp match with capture".
These operations would take a line of characters and either a splitting character (like a space in your case) or a regular expression describing the line and what you want to get from it, and return the results as an array holding the extracted fields.
Until then, indeed you will have to use (possibly repeated) use of find and pick.
 
upnort
newbie
Topic Author
Posts: 44
Joined: Wed Aug 15, 2018 2:03 am

Re: Splitting/parsing variable data

Tue Aug 20, 2019 9:07 pm

Thank you!

Who is online

Users browsing this forum: No registered users and 4 guests