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)
Code: Select all
: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
Code: Select all
#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