UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 25.9 Adding and Deleting White Space Chapter 25
Showing What's in a File
Next: 25.11 crush: A cat that Skips all Blank Lines
 

25.10 Squash Extra Blank Lines

Reading output with lots of empty lines can be a waste of screen space. For instance, some System V versions of man (50.1) show all the blank lines between manual pages. To stop that, read your file or pipe it through cat -s. (Many versions of more (25.3) have a similar -s option.) The -s option replaces multiple blank lines with a single blank line. (If your cat doesn't have -s, see the sed alternative at the end.)

cat -s might not always seem to work. The problem is usually that the "empty" lines have SPACE, TAB, or CTRL-m characters on them. The fix is to let sed "erase" lines with those invisible characters on them:

% sed 's/^[[SPACE][TAB][CTRL-v][CTRL-m]]*$//' file | cat -s

In vi (31.6) and many terminal drivers (42.1), the CTRL-v character quotes the CTRL-m (RETURN) so that character doesn't end the current line.

If you don't have cat -s, then sed can do both jobs:

% sed -e 's/^[[SPACE][TAB][CTRL-v][CTRL-m]]*$//' -e '/./,/^$/!d' file

- JP


Previous: 25.9 Adding and Deleting White Space UNIX Power ToolsNext: 25.11 crush: A cat that Skips all Blank Lines
25.9 Adding and Deleting White Space Book Index25.11 crush: A cat that Skips all Blank Lines

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System