- Read "in";
- Split it in records, using
^#[0-9]{10}\nas a record separator, without consuming the separator (<> =~ /^#[0-9]{10}\n.*?(?=^#[0-9]{10}\n|\z)/smg;\zis there only because of the last record, which won't be followed by another record separator, so we'll allow the regex to match the end of the file); - Process all records;
- For each record, remove the separator (the line lincluding the timestamp); if the remainder (the command) is in a list of already processed commands, ignore it, otherwise store the command exclusive of the separator in the list of already processed commands and store the command inclusive of the separator in an array;
- Join the elements of the array on an empty string, printing the resulting string.
Breakdown of the regex:
^#[0-9]{10}\n.*?(?=^#[0-9]{10}\n|\z): will match a line starting with a#character, followed by 10 digits and a newline; it will then lazily match anything (including newlines) until a new occurrence of^#[0-9]{10}\nor the end of the string (\z) is found (avoiding to capture the newly found occurrence of^#[0-9]{10}\nin the current match using a zero-length look-ahead assertion (?=) and allowing the next match to capture it);swill allow.to match newlines,mwill allow^and$to match after and before a newline andgwill allow to capture multiple occurrences of the pattern in the string.