pushin: Squeeze Out Extra White Space

If you're viewing or printing a file with lines that are too long to read, you can use a program like fold () to fold the lines. Or, if there's lots of white space in each line - multiple spaces and/or TABs next to each other - you can use the script at the end of this article. The pushin script replaces series of spaces and/or TABs with a single space, "pushing in" each line as much as it can. It reads from files or standard input and writes to standard output.

Here's an example of lines in a file that aren't too long (we can't print long lines in this tutorial, anyway) but that do have a lot of white space. Imagine how pushin would help with longer lines:

% cat data resistor 349-4991-02 23 capacitor 385-2981-49 16 diode 405-3951-58 8 % pushin data resistor 349-4991-02 23 capacitor 385-2981-49 16 diode 405-3951-58 8

Here's the script:

#!/bin/sed -f s/[ ][ ]*/ /g

Inside each pair of brackets, [ ], the sed substitute command has a space and a TAB. The replacement string is a single space.

That file doesn't use a shell; the kernel starts sed directly () 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 's/[ ][ ]*/ /g' ${1+"$@"}

It starts a shell, then exec replaces the shell with sed (). The ${1+"$@"} works around a problem with argument handling () in some Bourne shells.

- JP