In the face of some regrettable rules that turned the original into a glorified sorting task, I am posting a more challenging variant. Shout out to Luis Mendo for the suggestion of how to improve the original challenge.
You've inherited a server that runs several apps which all output to the same log.
Your task is to de-interleave the lines of the log file by source. Fortunately for you, the person who wrote all the apps was nice enough to leave tags indicating their source.
Each line will look something like this:
[app_name] Something horrible happened!
- App tags are always between square brackets and will contain only alphanumeric characters and underscores.
- App tags are nonempty
- There may be other square brackets later on any given line, but none will form a valid tag.
- There will always be at least one non-space character after a tag.
- The log as a whole may be empty.
- There is no limit to how many unique app tags will be present in the file.
In some cases, an app tag may be missing. When this is the case, the log line belongs to the most recently logged app.
- The first line of the log will always begin with an app tag
- A line beginning with
[is not necessarily tagged. If there is an invalid character between the initial square brackets or no
], then the line is not tagged.
- No empty lines appear in the log
You should output several fully-separated logs with the app tags removed from each log line where they were present. You do not need to preserve leading whitespace on any log line.
Output logs must be in some sort of key-value mapping or reasonable equivalent. A non-exhaustive list of valid output formats:
- A file named after the app tag for each app
- You may assume that the output files do not already exist in the output directory in this case.
- A dictionary/map/hash/whatever that uses app tags as keys and a newline-separated string of the log lines as values.
- A long concatenated string separated by blank lines and preceded by app tags
- A list of [key, value] lists
- A JSON string with app tags as keys and arrays of log lines as values
- A Markdown document with app tags as headers and leading
#s of any line escaped with backslashes.
Basically, if you can't tell which app the log lines came from, the output is invalid.
An entire log might look like this:
[weather] Current temp: 83F [barkeep] Fish enters bar Fish orders beer [stockmarket] PI +3.14 [PI announced merger with E] [barkeep] Fish leaves bar [weather] 40% chance of rain detected [ I have a lovely bunch of coconuts
Which should output three different logs:
Current temp: 83F 40% chance of rain detected [ I have a lovely bunch of coconuts
Fish enters bar Fish orders beer Fish leaves bar
PI +3.14 [PI announced merger with E]
You are not given the names of the app tags ahead of time. You must determine them only by analyzing the log file.
Rules and Scoring
- This is code-golf, so shortest code wins.
- Standard rules and loopholes apply
- Use any convenient IO format, provided that each input line is represented as a string, not a pre-parsed tag + message. Parsing is part of this challenge.
- Output log lines for each app must appear in the same order that they did in the original log.
- You may assume that the input log contains only ASCII characters.