Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to insert newlines into large block of text at specific locations

Tags:

regex

I have a rather large text file that has a bunch of missing newlines, meaning that it's a mess. I need to break it up into appropriate lines.

The text looks something like this now:

12345 This is a chunk 23456 This is another chunk 34567 This is yet another chunk 45678 This is yet more chunk 56789 Yet another piece of text

I need a regex that will insert a newline (CR/LF pair) before each group of five digits, resulting in something like this:

12345 This is a chunk 
23456 This is another chunk 
34567 This is yet another chunk 
45678 This is yet more chunk 
56789 Yet another piece of text

It can insert one before the first group of digits or not; that I can deal with.

Any ideas? Thanks.

like image 217
Ken White Avatar asked Dec 01 '22 12:12

Ken White


1 Answers

Very simple (but not as "flashy" as possible, since I'm too lazy to use lookaheads):

s/(\d{5})/\r\n\1/gs
like image 196
user54650 Avatar answered Dec 16 '22 09:12

user54650