I needed to export DHCP leases to CSV format for further use. I found out some examples but not quite suiting my needs so I had to do some changes and updates due to ROS version.
Here is script as I guess someone else would need similar
CSV is universally accepter file extension. Historically it started using comma as separator, but as comma can easy be found in data itself and make mess, other delimiters are allowed, like TAB, which actually became more common than comma.
This is first time in my 40 years of working in IT that I hear someone objects about naming CSV…
order= let you “select columns”, which is how it “matches” OP output. Noteorder is tricky, since the column does have to exist in the output. So, for example, if no DHCP lease had a comment, then above command would fail - this because RouterOS print remove columns if there not used
print detail as-value is needed to make it an array needed by [:serialize], with the detail being that print detail is needed to get full set of attributes. The devil is in the details, as they say.
And now the fun fact, one of the most used programs where you import (and can export) CSV is Excel and similar spreadsheets programs.
In Italian (and AFAIK many other European) Windows version Excel won’t normally parse commas as separators, nor will save CSV files with commas as separators, because what is used is instead the local system separator, which is “;” (semicolon).
In this sense pedjas is right that the C in CSV has become in the years more “CanUseAnythingIFancy” than “Comma”
For additional fun and games, many countries use comma instead of a period as the numeric decimal separator. This along with using semicolon as the field separator leads to Excel being completely unable to import csv’s created on different locale settings. (I don’t know what the current situation is, but it was like this for more than a decade.) This leads to a format that would seem the most universal for exchanging simple tabular numeric data being implemented as one of the worst in terms of compatibility.
Interestingly, Libre/OpenOffice seems to automagically figure out what it’s reading, and even when it doesn’t, there’s an intuitive little wizard for it.
EDIT: Oh, I almost forgot, there was a while when even if comma was used as the decimal separator, it was also used as the column/field separator as well, but every value was in quotes
And this is one of the reasons why the good MS guys invented their own modification to the CSV format, adding a line at the beginning, like:
sep=,
or
sep=;
etcetera allows to tell Excel what field separators Is actually used in the file.