Is there a way to remove empty lines using find and replace?


#1

Hi, I’m going through some log files and it would be really handy to use find and replace to remove lines in their entirety. For example in the log file

Starting
Started
Running
Running
Running
Running
Stopping
Starting

I’d like to find all instances of “Running” and remove that entire line.

I can do a find and replace for “.*Running.*” and replace with “”, but this gives

Starting
Started
[ blank line ]
[ blank line ]
[ blank line ]
[ blank line ]
Stopping
Starting

It’d be really nice if there were a way to remove those blank lines, either during the regex find and replace, or in a second step afterward. Is there a way to do this? Thanks.


#2

Maybe search for something like \r?\n\s*\n with regular expression
And replace by \n or \r\n depending of the line type you have.


#3

This didn’t work for me. \n or \s*\n both identify the correct locations, but I can’t seem to find the right solution to replace it with so that I’ll end up with only the blank lines gone and the rest unaffected. If I put \n, it loops through and nothing changes, if I leave it blank everything ends up on one line.

FWIW, searching for \r?\n\s*\n does not find anything. Nor does \r\n. Any help is greatly appreciated!


#4

If I try to open the same file in Notepad or Wordpad, I don’t see the extra blank lines. So I think it’s a file type problem, but I don’t know how to correct that, or to get Atom to read it correctly.


#5

Maybe this?
Find: ^\r?\n\r?
Leave replace empty


#6

It’s most likely a newline problem. Atom shows you the newline type used in the file on the right-hand side of the status bar (which will usually be CRLF).

If you go to File -> Settings -> Editor and select Show Invisibles, you can also see precisely which characters are at the ends of the lines. If you do that and screenshot the relevant lines, we can help you more.

Notepad

Notepad is far too barebones to be useful for anything of substance. Grab Notepad++ and replace Notepad with it. It’s almost as lightweight and not a feature desert.


#7

Thanks @thtliife, that didn’t work, but read on and maybe you can understand what I don’t.

Thanks @DamnedScholar for the tips, they were very helpful. In the lower right it said Mixed and UTF8. After experimenting with different windows and iso formats, what did it was changing the default file encoding to UTF16le. I guess Atom doesn’t auto detect that type well enough (forgive me if I misspeak this isn’t my forte’). Thanks for the tip on Notepad, I’ve never been a fan of it, I’ve used TextPad in years past, but then switched to a Mac and needed to find a new alternative. SublimeText2 was perfect, but too pricey for someone who doesn’t code for a living. So I’ve been searching for an alternative. I love Atom, but it’s still got a way to go before it’s at ST’s level, but I LOVE the customization. Notepad++ would be fine if it had a dark mode (same with TextPad). At any rate, I’ve been searching for a package that will improve the file type detection without success. Oh, and I’m back on Win after my Mac exploits which explains the Notepad comment.


#8

UTF-8 is safe. I don’t think your issue would have anything to do with the encoding, though. Clicking on Mixed will bring up a modal window where you can select “LF” or “CRLF” and then all of your line endings will swap to the one you pick.

Notepad++ would be fine if it had a dark mode

That would be better, but I use Notepad++ for quick edits where Atom would be too cumbersome to invoke and in that case, the coloration doesn’t matter too much.


#9

So I tried clicking on the Mixed and changing it. But regardless of what I chose, I wasn’t able to use any of those search patterns to remove the empty lines. A clue might be that when I manually removed those lines, I had to backspace three times, even though the show invisibles did not show three things to delete. I did discover that I can click on the UTF8 and change it quickly from there which ROCKS! Atom earns more respect from me every day!


#11


#12

That’s something I wouldn’t have expected to happen.


#13

try ‘delete-whitelines’ package, link


#14

It appears that this is a duplicate of another topic:

We like to unify similar discussions under one topic so that people don’t have to read a bunch of different threads to get the whole story. Please continue the discussion there. I’ll be closing this topic as duplicate.


#15

This topic was automatically closed after 12 hours. New replies are no longer allowed.