Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 337
Rahul Misra Avatar asked Sep 20 '25 20:09

Rahul Misra


1 Answers

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
like image 177
Toto Avatar answered Sep 23 '25 00:09

Toto