Search code examples
textreplacesublimetext

How to remove newlines from a group of n-consecutive lines in sublime text


I've a text file where each line contains a uuid within single quote followed by a comma. A sample of this file would look like the following:

'527a34922f3472506d93f393c1dd5cac',
'7bdce3215c3007ccfb3449702234a2b4',
'b74d228b5c6dbfd95ac989eb7b4837ac',
'59c7694db4effe03984d05b43c46c1ce',
'b038091601beb11c00d28d8ea277cecb',
'c3b489c4b7526adb36b049c76c75835d',
'cfdf54d36262c474103fba5486f3fa48',
'10d3d4c4aa0f162d5ab3a403010c2202',
'1103abf37755c8477f0177478a0f91cd',
...

I'm using Sublime Text 4, and I need to group every 3 lines and pull them into a single line. So, the output I'm expecting is:

'527a34922f3472506d93f393c1dd5cac', '7bdce3215c3007ccfb3449702234a2b4', 'b74d228b5c6dbfd95ac989eb7b4837ac',
'59c7694db4effe03984d05b43c46c1ce', 'b038091601beb11c00d28d8ea277cecb', 'c3b489c4b7526adb36b049c76c75835d',
'cfdf54d36262c474103fba5486f3fa48', '10d3d4c4aa0f162d5ab3a403010c2202', '1103abf37755c8477f0177478a0f91cd',
...

How do I achieve that? I could select every group of 3 lines using this command: ((.*\n){1, 3}), but not able to perform the grouping on each selection.


Solution

  • You can capture the body of every line and match the trailing newline of each, do this for 3 lines and replace them with the three lines separated by spaces instead, and end with another newline:

    Find:

    (.*)\n(.*)\n(.*)\n
    

    Replace:

    \1 \2 \3\n
    

    Demo: https://regex101.com/r/lwmRCQ/1