sed one-liners to clean blanks

Using sed one-liners, recursively clean from text files such as blank lines and 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:

./clean.sh ~/my_site "*.md"

clean.sh contains:

#!/usr/bin/env bash

set -o errexit

loc=$1
pat=$2

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 include keeping 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.