dardar.moh
dardar.moh

Reputation: 6725

Groupe each five line in one line - Regex NotePad++

I have a file text with a large data like this :

A
B
C
D
E
F
G
H
I
J

I want group each 5 line into one line like this:

ABCDE
FGHIJ

I try with this Regex, but it not working

Find What: (\r\n{3})
Replace with: $1\n

Any suggestion please ?

Upvotes: 1

Views: 46

Answers (3)

SPIce
SPIce

Reputation: 1

If you want to do this using notepad++, and if using regex is not an absolute requirement you could also use a simple macro like this:

  • place the cursor to first row + first column.
  • start recording a macro (Macro -> Start Recording)
  • hit "End" / "Del" four times
  • hit "Pos1"
  • hit "Down arrow"
  • stop recording the macro (Macro -> Stop Recording)
  • eventually save this macro, if needed another time (Macro -> Save Currently Recorded Macro)

Now play this macro multiple times until end of file (Macro -> Run A Macro Multiple Times)

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626853

You can use

^(.*)\R(.*)(?:\R(.*))?(?:\R(.*))?(?:\R(.*))?

To replace with $1$2$3$4$5. The \R stands for any linebreak style, CRLF, CR, or LF. The ^ is necessary (start of line) to make sure we start counting lines from the next after a successful match.

Also note that we need to have at least 1 linebreak to start "merging" lines (see (.*)\R(.*) in the beginning of the pattern).

Pattern details:

  • ^ - start of line
  • (.*)\R - a line with a linebreak at the end, the contents of which are captured into Group 1
  • (.*) - (Group 2) line after the first one captured
  • (?:\R(.*))? - optional (1 or 0) linebreak followed with some (0 or more) characters other than a newline (Group 3)
  • (?:\R(.*))? - ibid. (Group 4)
  • (?:\R(.*))? - ibid. (Group 5)

See the screen below:

enter image description here

Upvotes: 0

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

I would do this like so:

(?:(.*)\r\n)(?:(.*)\r\n)?(?:(.*)\r\n)?(?:(.*)\r\n)?(?:(.*)\r\n)?

And replace with: $1$2$3$4$5\n

Upvotes: 1

Related Questions