Community discussions

MikroTik App
 
User avatar
KILLPC
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 5:19 pm
Location: Greece
Contact:

dos2unix script

Mon Jul 22, 2024 7:43 am

Hi Mikrotik fans!

Below is a simple dos2unix script where it removes the \r (carriage return) of a text file.

Useful for files or variable content in which you wish to remove the \r (CR)
    :local content $1
    :local cleanedContent ""
    :local start 0
    :local end 1

    # Loop through each character and remove '\r'
    :for i from=0 to=([:len $content]) step=1 do={
        :local char [:pick $content $start $end ]
        :local check [:find $char "\r"]
               :if ($check < 0 ) do={
            :set cleanedContent ($cleanedContent . $char)
        }
        :set start ($start + 1)
        :set end ($end +1)
}

:return $cleanedContent
Usage example for a text file when the above is saved at system scripts with name dos2unix
#Import the function from system scripts with name dos2unix
:local dos2unix [:parse [/system script get dos2unix source]]

#Load the content of the file, call the function and save the cleaned results to the file.
:local content [/file get test.txt contents]
:local cleanedContent [$dos2unix $content]
/file set test.txt contents=$cleanedContent
Or you can just clean the contents of the variable if you like.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12331
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: dos2unix script

Mon Jul 22, 2024 1:03 pm

ignoring others various errors,

why?
        :local check [:find $char "\r"]
               :if ($check < 0 ) do={
           :set cleanedContent ($cleanedContent . $char)
        }

just:
       :if ($char != "\r") do={:set cleanedContent "$cleanedContent$char"}
 
User avatar
KILLPC
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 5:19 pm
Location: Greece
Contact:

Re: dos2unix script

Mon Jul 22, 2024 6:30 pm

For some reason (maybe the mistake is from my side), the != "\r" was not working. I have the latest RoS

I will try again since you pointed that it should work that way ( and yes it should).

Thanks!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3878
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: dos2unix script

Mon Jul 22, 2024 7:27 pm

I have the latest RoS
In the most recent V7 releases there are ":tocrlf" and ":tolf" commands available that do same as "dos2unix" (and reverse unix2dos). There is also now :convert, so we can see the effect those new commands do in hex (e.g. 0a == \n, 0d == \r):
:put [:convert from=raw to=hex [:tolf "0\r\n0\r\n"]]  
300a300a
:put [:convert from=raw to=hex [:tocrlf "0\n0\n"]] 
300d0a300d0a

For completeness, without the :tolf or :tocrlf:
:put [:convert from=raw to=hex "0\r\n0\r\n"]          
# 300d0a300d0a
:put [:convert from=raw to=hex "0\n0\n"]      
# 300a300a
 
User avatar
KILLPC
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 5:19 pm
Location: Greece
Contact:

Re: dos2unix script

Tue Jul 23, 2024 7:28 am

Ah nice!

I will check that too!

Thanks for the info!

Who is online

Users browsing this forum: No registered users and 6 guests