Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active September 17, 2024 01:31
Show Gist options
  • Save fomightez/706c1934a07c08b6c441 to your computer and use it in GitHub Desktop.
Save fomightez/706c1934a07c08b6c441 to your computer and use it in GitHub Desktop.
remove all blank lines using regular expressions

REGEX remove blank lines:

FROM: http://www.ultraedit.com/support/tutorials_power_tips/ultraedit/remove_blank_lines.html


FIND:

^(?:[\t ]*(?:\r?\n|\r))+

REPLACE:

[LEAVE THIS EMPTY BECAUSE YOU DO NOT WANT TO REPLACE WITH ANYTHING]

(Related: If you happen to be where you can use AWK, you can use awk NF.
If you happen to be where you can use SED, you can use sed /^$/d' --
EXAMPLE USE OF SED sed /^$/d < text_with_blanks.txt > no_blanks.txt.)

@palanisamy-hari
Copy link

Thanks

@Naskalin
Copy link

big thanks :)

@Speeedy01
Copy link

love it, works perfectly in VSCode.

Copy link

ghost commented Oct 19, 2019

This is very useful, thanks man

@jpascua313
Copy link

Dude - thank you! Super helpful

@SiqingYu
Copy link

Super helpful.

@michaelj916
Copy link

Works great, thanks so much :)

@esa1975
Copy link

esa1975 commented May 11, 2020

Thanks for this!

@vijaychhipa
Copy link

Thank you so much

@stephen147
Copy link

stephen147 commented Aug 2, 2020

If the line contains a line feed at the end this won't pick it up. Either will my attempt ^[ \t]*$\r?\n

@ve3
Copy link

ve3 commented Jan 6, 2021

Thank you!

@vitran96
Copy link

Thank you!

@javier-alonsor
Copy link

Thanks mate

Copy link

ghost commented Sep 14, 2021

You can just use ^\s$ to match blank lines...

@stephen147
Copy link

You can just use ^\s$ to match blank lines...

That's not a blank line. It's a line with exactly one whitespace character.

Copy link

ghost commented Sep 15, 2021

You can just use ^\s$ to match blank lines...

That's not a blank line. It's a line with exactly one whitespace character.

Oh... What about ^$?

@stephen147
Copy link

Nope. A simple test would have shown it doesn't work. That's basically matching a zero width character. So replacing it with nothing has no effect. Here is a good thread to perhaps learn a bit more about removing empty lines. https://stackoverflow.com/questions/3866034/removing-empty-lines-in-notepad

@mangelozzi
Copy link

mangelozzi commented Nov 12, 2021

This works for me (note the multiline flag):

EMPTY_LINES = re.compile(r'\n\s*\n', re.MULTILINE)
result = EMPTY_LINES.sub('\n', content)

Copy link

ghost commented Nov 15, 2021

Nope. A simple test would have shown it doesn't work. That's basically matching a zero width character. So replacing it with nothing has no effect. Here is a good thread to perhaps learn a bit more about removing empty lines. https://stackoverflow.com/questions/3866034/removing-empty-lines-in-notepad

Would this work: ^ *$? It should match the start of a string, then any amount of whitespace, then the end of a string.

@stephen147
Copy link

Nope, that's still a line the contains a whitespace. It's not a blank line.

@parthiops
Copy link

thats really helpful and I have replaced with a \n for my own convenience

@ypacheco
Copy link

ypacheco commented Sep 3, 2022

Thanks!!!!

@zeelsheladiya
Copy link

super helpful. big thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment