开发者

How to insert new line 3 white spaces or 2 words prior to specific character

开发者 https://www.devze.com 2023-01-11 01:28 出处:网络
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

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 > :开发者_如何学Python

/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


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号