June 28, 2019

There are at least 37.19 bajillion ways to do this, probably a few more. I’ll give six:

awk '!/^[:space:]*$/'
grep -v "^[ \t]*$"
sed '/^[ \t]*$/d'
awk '!/^[ \t]*$/'
grep -vE "^\s*$"
awk '!/^\s*$/'

Those aren’t all the same, because of varying definitions of white space. At fourteen characters, I’m not sure I can write a smaller program. If this task came up in real life, I would probably think first of the sed solution, because editing text as it passes through its filter is what sed is designed to do.

You can run the program at


Pages: 1 2

9 Responses to “Deblank”

  1. Alex B. said

    Python 3:

    def remove_blank_lines(file, encoding='utf8'):
        """Return a list of the non-blank lines in a file"""
        non_blanks = []
        with open(file, 'r', encoding=encoding) as fobj:
            for line in fobj:
                if line.strip():
        return non_blanks
  2. Tovervogel said

    As a pseudo-one-liner in Python, abusing list comprehension syntax:

    import sys; [sys.stdout.write(line) for line in sys.stdin if line.strip()]

  3. Bill Wood said

    Rust version:

    fn strip(s: &str) -> String {
        s.chars().filter(|&c| " \t\n\r".find(c).is_none()).collect()
    fn main() {
        println!("{}", strip(
            "this is a test
            of the emergency broadcast        


  4. fd said

    gawk /\S/

  5. fd said

    There’s supposed to be two backslashes in front of the “S” in that gawk expression. Looks like the text-entry form here stripped one of them.

  6. Daniel said

    Here’s a solution with a vim command:

  7. Zack said

    Intriguing problem, with a practical aspect to it! Here is my approach to it, using Julia:

    function ContainsOnlyWhiteCharacters(text::AbstractString)
    wc = [‘ ‘, ‘\t’, ‘\n’] # white characters

    for char in text
        if !(char in wc); return false; end
    return true


    function process(fn::AbstractString)
    f = open(fn)
    lines = readlines(f)
    n = length(lines)
    ind = BitArray(undef, n)

    for i = 1:n
        ind[i] = !ContainsOnlyWhiteCharacters(lines[i])
    return join(lines[ind], "\n")



  8. Globules said

    An unimaginative, under-the-top Haskell version. The octal escapes in the printf produce a Unicode thin space.

    import           Data.Char (isSpace)
    import qualified Data.Text.Lazy as T
    import qualified Data.Text.Lazy.IO as IO
    main :: IO ()
    main = IO.interact (T.unlines . filter (not . T.all isSpace) . T.lines)
    $ printf "a\nb\n\nc\n  \t\nd\n\342\200\211\ne\n" | ./deblank
  9. mnemenaut said

    #lang racket
    (require 2htdp/image)

    (let ((white (car (image->color-list (text " " 12 "black")))))
    (for ([line (port->lines)])
    (for/and ([pixel (image->color-list (text line 12 "black"))])
    (equal? pixel white))
    (displayln line))))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: