Problem with script for getting monitor traffic

Hello,
I have one problem with my script. Best way will be paste code here :

# Do not include '.rsc', it is appended automatically
:local filename "wireless-interface-monitor"
# Internal processing below...
# ----------------------------------
:local line
:local word
:local contents
:local fileindex 1

/interface wireless {
   :set contents "/interface wireless monitor\n"
# loop through wireless interfaces, getting their advanced (plus basic) settings
   :foreach r in=[monitor wlan1 as-value] do={
      :set line ""
      :foreach i in=[:toarray $r] do={
	   :set word [:pick [:tostr $i] 0 [:len [:tostr $i]]]
	   :set line ([:tostr $line] . [:tostr $word] . "\n")
         }
#   end foreach i
      }
# delete last character "\n"
      :set line ([:pick [:tostr $line] 0 ([:len [:tostr $line]]-1)])
#   if contents + line is over filesize limit, write out to file
      :if (([:len [:tostr $contents]] + [:len [:tostr $line]]) > 4096) do={
         /system identity export file=($filename . $fileindex)
#      wait for file to appear (this is needed)
         :while ([:len [/file find name=($filename . $fileindex . ".rsc")]] = 0) do={}
         /file set ($filename . $fileindex . ".rsc") contents=[:tostr $contents]
         :set contents "/interface wireless monitor\n"
         :set fileindex ($fileindex + 1)
      }
#   append line to contents
      :set contents ([:tostr $contents] . [:tostr $line])
   }
# write remaining contents
   :if ([:len $contents] > 0) do={
      /system identity export file=($filename . $fileindex)
#   wait for file to appear (this is needed)
      :while ([:len [/file find name=($filename . $fileindex . ".rsc")]] = 0) do={}
      /file set ($filename . $fileindex . ".rsc") contents=[:tostr $contents]
   }
# /interface wireless
}

I think the problem is on line where I setting word and line. *Note that variable word is there because of debug I had the code of word inside “set line” code.
This is what I got from file:

/interface wireless monitor
                           notify-external-fdb=true

So I think that the script badly joining strings in variable line.

The good output should be:

/interface wireless monitor
.id=*4
band=2.4ghz/g
...
notify-external-fdb=true

Could someone help me?

If content of the file has only new line “\n” and no carriage return “\r” then you will get output in console as follows

[admin@dzeltenais_burkaans] /file> set 1 contents="lala\nla"
[admin@dzeltenais_burkaans] /file> :put [get 1 contents ]
lala
    la

Check exact content with “/file print detail” command.