João Ciocca
João Ciocca

Reputation: 866

Find lines without specified string and remove empty lines too

So, I know from this question how to find all the lines that don't contain a specific string. But it leaves a lot of empty newlines when I use it, for example, in a text editor substitution (Notepad++, Sublime, etc).

Is there a way to also remove the empty lines left behind by the substitution in the same regex or, as it's mentioned on the accepted answer, "this is not something regex ... should do"?

Example, based on the example from that question:

Input:

aahoho
bbhihi
cchaha
sshede
ddhudu
wwhada
hede
eehidi

Desired output:

sshede
hede

[edit-1]

Let's try this again: what I want is a way to use regex replace to remove everything that does not contain hede on the text editor. If I try .*hede.* it will find all hede:

print showing what .*hede.* finds

But it will not remove. On a short file, this is easy to do manually, but the idea here is to replace on a larger file, with over 1000+ lines, but that would contain anywhere between 20-50 lines with the desired string.

If I use ^((?!hede).)*$ and replace it with nothing, I end up with empty lines:

print showing how the substitution ends up, with empty lines

I thought it was a simple question, for people with a better understanding of regex than me: can a single regex replace also remove those empty lines left behind?

Upvotes: 0

Views: 352

Answers (3)

Haji Rahmatullah
Haji Rahmatullah

Reputation: 430

An alternative try
Find what: ^(?!.*hede).*\s?
Replace with: nothing

Explanation:

 ^        # start of a line
 (?!)     # a Negative Lookahead 
 .         # matches any character (except for line terminators)
 *        # matches the previous token between zero and unlimited times,
 hede   # matches the characters hede literally
 \s       # matches any whitespace character (equivalent to [\r\n\t\f\v ])
 ?         # matches the previous token between zero and one times,

Upvotes: 1

Tripp Kinetics
Tripp Kinetics

Reputation: 5459

Have you tried:

.*hede.*

I don't know why you are doing an inverse search for this.

You can use sed like:

sed -e '/.*hede.*/!d' input.txt

Upvotes: 0

Toto
Toto

Reputation: 91518

Using Notepad++.

  • Ctrl+H
  • Find what: ^((?!hede).)*(?:\R|\z)
  • Replace with: LEAVE EMPTY
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^                   # beginning of line
  ((?!hede).)*      # tempered greedy token, make sure we haven't hede in the line
  (?:\R|\z)         # non capture group, any kind of line break OR end of file

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Upvotes: 1

Related Questions