Quick Reg. Exp. for Cleaning Up Text Files

Often, when dealing with text files in my day-to-day duties, I’ll end up with large gaps of newlines and white space that I want to zap. Something line

this is a line

this is another line


This is, yet another, line.
[this is an invisible tab chacater]

More lines!

A decade ago I’d spend tedious/precious minutes going through the file manually and cleaning up the extra lines. Now I just search on the following regular expression and replace it with a single newline

\n[\n\s]+

Or, broken out /x style

\n          #a new-line character 
[\n\s]+     #a character class consisting of any 
            #newline character (\n) or white space (\s), 
            #repeated one or more times (+)

Keep in mind if you’re using BBEdit, you’ll need to use \r instead of \n, since Bare Bones interpreted Apple’s lack-of-official documentation on OS X’s line endings to mean “keep using \r”.

Originally published May 13, 2007