June 28, 2019

Today’s task is easy. I expect to see lots of imaginative and over-the-top solutions:

Write a program that passes its input to its output, removing any lines that are either empty or contain only “white” characters like space and tab.

Your task is to write a program that removes blank lines from files. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.


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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: