Deblank
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 https://ideone.com/IoMIPT.
Python 3:
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()]
Rust version:
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=930b30b55bac3d1783827bd1cd70f06b
gawk /\S/
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.
Here’s a solution with a vim command:
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
end
function process(fn::AbstractString)
f = open(fn)
lines = readlines(f)
close(f)
n = length(lines)
ind = BitArray(undef, n)
end
Cheers
An unimaginative, under-the-top Haskell version. The octal escapes in the printf produce a Unicode thin space.
#lang racket
(require 2htdp/image)
(let ((white (car (image->color-list (text " " 12 "black")))))
(for ([line (port->lines)])
(unless
(for/and ([pixel (image->color-list (text line 12 "black"))])
(equal? pixel white))
(displayln line))))