Use sed to add a newline to the end of a file (this will perform an in-place replacement and create a backup file, MYFILE.bak):
$ sed -i.bak '$G' MYFILE
Some programs, such as Emacs, will omit the trailing newline from a file unless configured to add one. Emacs can be configured to add a newline automatically.
backupcommandsconfigurationeditorsemacsin-placeline-endingsnewlinesedshell
This command will replace occurrences of foo with bar in the file X in-place and create a backup of X in X.old:
$ sed -i.old "s/foo/bar/g" filename
backupcommandsin-placesedshell