23

For e. g., less has option --squeeze-blank-lines (or -s in short) and it squeezes multiple blank lines into single blank line, buuut, less wouldn't do this being used as filter (i. e., having its output sent not to a tty).

Similar option can be found in cat too, it's also called -s usually, and it also makes single blank line instead of several.

What about removing empty/blank lines all together? One approach I can think of is using grep, but may be I've overlooked something more simple?

poige
  • 6,231
  • Depends, how were you thinking of using grep? – terdon Apr 06 '16 at 14:26
  • 1
    my go to command is grep -v ^$ filename – MelBurslan Apr 06 '16 at 14:27
  • 1
    @MelBurslan why not just grep . filename? Or, to also remove non-empty lines with whitespace: grep -E '\S' filename – terdon Apr 06 '16 at 14:34
  • The title of this question asks about empty lines (but the body says "empty/blank"), whereas the target question title clearly says blank (i.e. whitespace-only) lines. Let's all please take care not to have titles mismatching the body. – smci Feb 20 '24 at 04:13

1 Answers1

42

Use awk!

echo -e "dada

ada

bada

" | awk NF

results in

dada
ada
bada
MatthewRock
  • 6,986
  • 9
    This post explains how awk NF works: https://stackoverflow.com/questions/23544804/how-awk-nf-filename-is-working – wisbucky Apr 25 '19 at 22:07