About a week ago, I learned a clever way to remove blank lines:


$ grep .

Now, I’ve just learned a new one, using tr‘s -s or --squeeze-repeats option:


$ tr -s '\n'

This command squeezes consecutive newlines into single one, that means removing the blanks lines. It’s longer in characters, but it could squeeze more than just newlines. Consider this:


$ echo 'abc123def456' | tr -s '[:digit:]' '[\n*]'
abc
def

It does two things:

  1. Translate digits into newlines: SET1 -> SET2
  2. Squeeze newlines from SET2

In other words, to look at it, it’s like the digits are delimiters to separate the text into multiple words, one per line.

Ain’t it squeezing amazingly?