Rahul Misra
Rahul Misra

Reputation: 669

How to add a new line BEFORE a line that matches a given pattern?

I have a text file with entries like below

LondonTableABC
ROW1
Cell1
Row2
ParisTableBCD
ROW1
ROW2
NewYorkTableEFG
ROW1
ROW2

I want to insert a line break before the matching pattern "Table" in the file. Find and replace has been my friend for a task like this to insert a new line AFTER the matching pattern but I can't figure out how to insert it BEFORE the matching pattern.

The result I expect after the replacement is

LondonTableABC
ROW1
Cell1
Row2

ParisTableBCD
ROW1
ROW2

NewYorkTableEFG
ROW1
ROW2

Upvotes: 4

Views: 11730

Answers (2)

Toto
Toto

Reputation: 91415

Not sure to well understand your needs, but I guess you want:

  • Ctrl+H
  • Find what: ^.+Table
  • Replace with: \n$0
  • check Match case ( if wanted )
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

^       : begining of line
.+      : 1 or more any character
Table   : literally Table

Replacement:

\n      : line break (you could use \r\n if requested)
$0      : whole match (ie. Table)

Result for given example:

LondonTableABC
ROW1
Cell1
Row2

ParisTableBCD
ROW1
ROW2

NewYorkTableEFG
ROW1
ROW2

Upvotes: 8

MaVRoSCy
MaVRoSCy

Reputation: 17839

Open Find Replace (CTRL+H)

Select "Extended" in search mode

Find what: Pattern

Replace With: \nPattern

Replace All

Upvotes: 4

Related Questions