Redfish76 has asked for the wisdom of the Perl Monks concerning the following question:

I have a couple of chat logs I would like to parse to a csv. I am a noob to perl but think this should be fairly simple. Below is an example

T 1310234540 19<24SomeUserName>19 This is user chat.
T 1310234540 14<13AnotherUser>14 Some more chat messages.

There are multiple lines in each log an I would like to parse it into a csv. Any idea's?

Replies are listed 'Best First'.
Re: Parse Chat Log to CSV
by blindluke (Hermit) on Sep 17, 2014 at 09:34 UTC

    That is a nice problem you have. Still, it's not defined enough to solve it. You only specified the input, and the output format, but not the output itself. You also did not provide any parsing rules.

    Taking what you have written as it stands, you could just run a simple substitution on every log line, substituting ">" with "," and you have a comma separated csv. This is exactly what you have written, but probably not exactly what you want.

    I could make some assumptions. Let's assume that you want to extract the user names and the message. Where are they? Well, maybe the rules are as follows:

    #!/usr/bin/perl use v5.14; my $input_line = 'T 1310234540 19<24SomeUserName>19 This is user chat. + '; $input_line =~ m/ \< # after the '<' sign, and \d+ # any digits that follow it, (\w+) # capture all letters as $1 \> # then expect the '>' sign \d+\s+ # followed by some other digits and whitespace (.+) # everything that follows is the message /x; my $username = $1; my $message = $2; say "Username: [$username], Message: [$message]";

    But are those the correct rules? What if the user name is '1983_Mike'? Will his name be parsed correctly, or will it just be "Mike"? It won't be either of those. Maybe the user names cannot contain anything other than letters? Maybe not. What if the numbers before the message have their own meaning? Does 19 mean that it's the 19th message sent by SomeUserName in this session? Is it important? Should it form part of your desired output?

    Start by asking yourself those questions, and figuring out what you want to do, exactly. Then, you could use the code above as a starting point, and look at the relevant sections of perlintro, as per the suggestion that was already given.

    Good luck, and don't forget that the chat messages can probably contain commas.

    regards,
    Luke Jefferson

Re: Parse Chat Log to CSV
by Anonymous Monk on Sep 16, 2014 at 23:03 UTC