sed one-liners to clean blanks

Using sed one-liners, recursively clean from text files:

  • blank lines
  • trailing whitespace

NOTE: ensure the globbing pattern is only for the expected text files or you might goof up PDF files etc. by just using “*”

The script below is used like:

./ ~/my_site "*.md" contains:

#!/usr/bin/env bash

set -o errexit


find $loc -not -path "*/.git*" -type f -name "$pat" -execdir sed --in-place 's/[[:space:]]\+$//' {} \+ -execdir sed --in-place -e :a -e '/^\n*$/{$d;N;};/\n$/ba' {} \+

Note that each “-execdir” command is separate. Add more commands or take out what is unwanted.

Use cases

  • keep files “Git clean” of trailing spaces and extra lines at end of file
  • Matlab editor doesn’t autoclean these lines, so use this script for “*.m” files