UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 25.10 Squash Extra Blank Lines Chapter 25
Showing What's in a File
Next: 25.12 Double Space, Triple Space ...
 

25.11 crush: A cat that Skips all Blank Lines

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.

- JP


Previous: 25.10 Squash Extra Blank Lines UNIX Power ToolsNext: 25.12 Double Space, Triple Space ...
25.10 Squash Extra Blank Lines Book Index25.12 Double Space, Triple Space ...

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