Reputation: 916
I made a mistake in my .editorconfig definition and forgot to add
insert_final_newline = true
So now some files do have a newline at the end while some do not.
I found similar questions such as:
However the solutions provided there add a newline to all files. I need to only add it to those that don't have a final newline already.
EDIT: The question that was linked as answering mine question doesn't do so as the answers are for single file only. I need this recursively for all files. Voting to reopen.
Upvotes: 0
Views: 316
Reputation: 22237
UPDATE : I updated this solution to accomodate the comment by Paul Hodges:
Just test, whether the last character in a file is a newline:
if [[ -z $(tail -c 1 YOUR_FILE) && ! -s YOUR_FILE ]]
then
# File ends in a newline, don't add one
else
# No newline in file, or file empty - add a newline
fi
Explanation:
If a file ends in a newline, tail -c 1
will return this newline character, but command substition will remove it. Therefore $(tail -c 1 ...)
will be empty. However, if the file itself is empty, the $(tail ...)
will also be empty. Therefore we need a check for the file being not-empty (! -s ...
).
Upvotes: 2