I want to add a newline at the end of a file only if it doesn\'t exists, this is to prevent multiple newlines at the end of the file.
I\'m hoping to use sed. Here\'s the
tail -c1 file | read -r _ || echo >> file
gets the last character of the file pipes it into read
, which will exit with a nonzero exit code if it encounters EOF before newline (so, if the last character of the file isn't a newline). If read
exits nonzero, then append a newline onto the file using echo
(if read
exits 0, that satisfies the ||
, so the echo
command isn't run).
From http://backreference.org/2010/05/23/sanitizing-files-with-no-trailing-newline/.