views:

35

answers:

2

I was trying to format a chat log for a friend that looks like this:

John Smith > hello Jane doe > hey how are you? John Smith > Pretty good thanks

and she wants to format it like this:

John Smith > hello

Jane doe > hey how are you?

John Smith > Pretty good thanks

Simply entering a new line after > is not good enough as it would not format correctly, so I need to insert a new line 3 white spaces, or 2 words prior to the ">" so the name is captured too.

So far I only have a new line after > :

/usr/bin/perl -p -i -e "s/>/>\n/g" *.txt

Edit: There are about 20+ different chat names involved so it would be great to do this without entering all their names since they may vary, and I'd like to learn from the exercise for fun. Thanks for reading

+1  A: 

Try this one:

perl -p -i -e "s/(\w+\s\w+\s*>)/\n\1/g" log.txt

Test I used for the regex:

[21:21:23] ~ $ echo 'John Smith > hello Jane doe > hey how are you? John Smith > Pretty good thanks Susie Someone > hi guys' > log.txt
[21:21:24] ~ $ more log.txt 
John Smith > hello Jane doe > hey how are you? John Smith > Pretty good thanks Susie Someone > hi guys
[21:21:27] ~ $ perl -p -i -e "s/(\w+\s\w+\s>)/\n\1/g" log.txt
[21:21:34] ~ $ more log.txt 

John Smith > hello 
Jane doe > hey how are you? 
John Smith > Pretty good thanks 
Susie Someone > hi guys
[21:21:37] ~ $ 

It does add an extra newline to the beginning of the file, but if that doesn't bother you then I think it should work.

Edit: It will also fail if someone used a > character in one of their messages for some reason (if it was preceded by a space and two words, anyway).

eldarerathis
Perfect thank you so much!!!!
Steve Martin
Yeah in the rare case that any of these hiccups occur a manual edit will be fine, but it is better then her going through each line and manually editing! :)
Steve Martin
what about cases where text is copied? `John Smith > Jane Doe > name/text by Jane Doe, copied/pasted by John Smith.` It will just appear as a blank John Smith line and then Jane Doe from earlier. Do you have the ability to control the application that creates the log?
vol7ron
If you don't want there to be a blank beginning line, you might want to try this instead: `perl -p -i -e "s/.(\b\w+\s\w+\s>)/\n\1/g" log.txt`
vol7ron
A: 

I know you've already got a script that is "good enough". But I thought I'd suggest an alternate strategy anyhow.

Handle this task in two parts.

Part one: Analyze the raw data and extract a list of user names.

  • Look for repeated word groups (of up to X length) that precede a >.
  • Generate a list of possible user names.

Here a human steps in and approves the list of user names.

Part two: Process the data based on a list of user names.

  • Process the file and match user names to use as delimiters

The advantage of this process is that you can handle inline > characters correctly in your final output. At least as long as no one types in a valid user name followed by a >.

Of course the code will be more complex. Whether the added complexity is worth the improved accuracy is dependent on your needs.

daotoad