iqzer0
iqzer0

Reputation: 163

Notepad++ Delete 2 rows above the row with a specific keyword

The camel is walking on water.
The cow jumped over the fence
The dog couldn't jump over the fence
The chicken flew over the ocean
The rabbit ate the elephant
The goat is singing like a bird
The dog barks like a woman

I have a long list like that, i need to delete 2 rows above if i find the word "dog" in a row. So the end result should be like

The dog couldn't jump over the fence
The chicken flew over the ocean
The dog barks like a woman

Please let me know how i can get this done :) thanks !!

Upvotes: 0

Views: 58

Answers (2)

Toto
Toto

Reputation: 91385

How about:

Type Ctrl+H then,

Find what: (?:.+\R){2}(?=.+?\bdog\b)
Replace with: NOTHING

Make sure you've checked Regular Expression but NOT dot matches newline.
Then click on Replace all

Explanation:

(?:         : Start NON capture group
  .+        : One or more any character
  \R        : any type of newline
){2}        : this group must occur twice
(?=         : Positive lookahead
  .+?       : One or more any character (non greedy) 
  \bdog\b   : the word dog alone (ie. not mydog or doggy)
)           : end of lookahead

Upvotes: 2

RaGe
RaGe

Reputation: 23657

Input

The camel is walking on water.
The cow jumped over the fence
The dog couldn't jump over the fence
The chicken flew over the ocean
The rabbit ate the elephant
The goat is singing like a bird
The dog barks like a woman

Find and Replace - in regex mode

Find What: .*\r\n.*\r\n(.*dog.*)
Replace with: \1

Result

The dog couldn't jump over the fence
The chicken flew over the ocean
The dog barks like a woman

Explanation

.* matches any character - zero to unlimited times (except newline)
\r\n together represent a newline
(.*dog.*) represents a line containg "dog", with the whole line captured into \1

On finding 3 such lines, you then replace all three lines with the line containing dog.

note: this will delete two lines above any line containing "dog" even if one of those lines themselves contains "dog"

Upvotes: 0

Related Questions