0

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?

João Ciocca
  • 776
  • 1
  • 10
  • 23
  • 1
    I would argue that a text editor isn't the right tool for those job. Use sed. – Tripp Kinetics Apr 26 '20 at 16:58
  • you could and I'd agree - I can do this with powershell, bash and other CLI, but the idea here was to understand if this can be achieved on a text editor. My fav is Sublime, but I do also like Notepad++, which Toto actually could come up with an answer to. Personally, I like to think of SO as a way to extend knowledge, go beyond, you know? I already knew how to do with CLI, but had no idea if it could be achieved with a text editor. – João Ciocca Apr 26 '20 at 20:08
  • 1
    `^(?!.*hede).*\s?` Replace with `nothing` – Haji Rahmatullah Apr 20 '21 at 04:57
  • You should put that as an answer, @HajiRahmatullah!! worked like a charm <3 – João Ciocca Apr 20 '21 at 11:45
  • @Joao Ciocca,,It has already an accepted answer, just tried to share alternatives,, – Haji Rahmatullah Apr 20 '21 at 13:17
  • seem to me like your alternative is a better one than the current accepted answer – João Ciocca Apr 20 '21 at 16:28
  • May be not, Toto is much experience..I'm still a beginner – Haji Rahmatullah Apr 21 '21 at 05:27
  • we're all noobs here, and your answer is as valid as anyone else's. I'd be honoured to give an upvote to that answer. Please, indulge me. – João Ciocca Apr 21 '21 at 10:24

3 Answers3

1

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

Toto
  • 89,455
  • 62
  • 89
  • 125
  • It's been a while since I moved away from Notepad++ to Sublime, but this one spot may actually make me go back to it, you know? Thanks Toto! – João Ciocca Apr 26 '20 at 20:09
1

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,
Haji Rahmatullah
  • 390
  • 1
  • 2
  • 11
0

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
Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
  • I have. The idea here is to remove everything that doesn't contain "hede" on a text editor - doing that will find "hede", yes, but it will not remove everything that's not "hede". Did I fail so hard to explain that on the question? – João Ciocca Apr 26 '20 at 14:52
  • I can use `sed`, the same way I can use `get-content somefile.txt | where { $_ -match "hede"}`, but that was not the idea here... – João Ciocca Apr 26 '20 at 20:13