I have a huge number of source files that are all lacking a newline at the end.
How do I automatically add a newline to the end of each of them?
Some may already have a newline, so it should only be added if necessary.
I'm probably not looking for code, per se, but just something I can run in Terminal to add the necessary newlines (or some kind of programming or development tool).
Below is my bash script solution. It first checks that the file is a text file. Then, if it's a text file, it uses tail and od (octal dump) to see if the last character is the newline character. If it isn't, then it appends a newline using echo:
Due To command localization Tim and Norman answer Shall be improved using 'LANG=C' prefix to have a chance to match 'No newline' pattern with every system having any regional parameters
This ensures an ending empty line to every file put on the command line of this script :
And this script detects files lacking of it :
A simple fix for files that are "missing" newline at end of file is simply sed; the following fixes the file "in-place" (using the "-i" option):
Explanation: find all files (
-type f
), runsed
, change the files in-place (-i
), given the following (-e
) script/expression, which matches the end of the file ($
), and perform the "append" action (a\
), but don't actually specify any text to append (nothing after the\
) which is going to add a newline to the end of the file, but only if it's missing. Prints all files found (fixed or not), which is probably unnecessary.Main caveat is that
sed
features vary across platforms, so-i
and-e
may or may not be supported / the same; e.g. older Unix, or MacOS oddities may require slightly different syntax.Try ex-way:
And recursively (with a new globbing option enabled):
This is equivalent to
vi -es
. Change*.c
to extension of your interest.The
ex
/vi
would automatically append newline on save if it's not present.I'm surprised nobody has mentioned that many simple text-processing tools like Awk will add a newline as a side effect. Here is a simple loop which will overwrite a file only if a newline was actually added.
(The temporary file is obviously a bit of a wart.)
IDEone demo: http://ideone.com/HpRHcx