Sometimes I have a series of files, or just one file, with lots of blank lines. Some systems have a -s option to cat that causes it to compress adjacent blank lines into one. If that option isn't available, you can use crush. The crush script skips all lines that are empty or have only blanks and/or TABs. Here it is:
#!/bin/sed -f /^[ ]*$/d
The brackets, [ ]
, have a
TAB and a space in them.
That file doesn't even use a shell, so it's efficient; the kernel
starts sed directly (45.3)
and gives it the script itself as the input file expected with the
-f option.
If your UNIX can't execute files directly with #!
,
type in this version
instead:
exec sed '/^[ ]*$/d' ${1+"$@"}
It starts a shell, then
exec replaces the shell with sed (45.7).
The ${1+"$@"}
works around a
problem with argument handling (46.7)
in some Bourne shells.
-