Other Ways To Do It (mostly not one-liners) can be found at the thread "How do I remove blank lines from text files?".
Hmmm... personally, I like Ovid's example below better than mine. It *does* seem more directly recognizable.
perl -i.bak -ne 'print if /\S/' file.txt
Seems a bit more intuitive to me :)
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
This snippet removes all non-empty lines containing
only whitespace, but I don't think that's particularly
intuitive behaviour. If I want to remove all "blank"
lines from a file, I probably want:
and if I want to get rid of all the lines with
"useless" whitespace, I probably want to collapse them
to empty lines:
perl -i.bak -ne 's/^\s+$//;print' file.txt
Update: Changed first one-liner
to Ovid's for readability.
-- Chip Salzenberg, Free-Floating Agent of Chaos
A Programming Language, 1962
Programming the 1900 series in COBOL, 1974
Algorithms + Data Structures = Programs, 1976
The C Programming Language, 1978
System V - Interface definition, 1986
Z80 Assembly Language Programming, 1979
TheSOURCE User's Manual, 1979
The C Primer, 1983
Interactive Fortran 77, A Hands on Approach, 1984
Perl.1 man page, 1991
Results (62 votes),