Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Using version control systems I get annoyed at the noise when the diff says No newline at end of file.

So I was wondering: How to add a newline at the end of a file to get rid off those messages?

share|improve this question

4 Answers

up vote 17 down vote accepted

Here you go:

sed -i -e '$a\' file

This adds \n at the end of the file only if it doesn't already end in a newline. So if you run it twice it will not add another newline:

$ cd "$(mktemp -d)"
$ printf foo > test.txt
$ sed -e '$a\' test.txt > test-with-eol.txt
$ diff test*
1c1
< foo
\ No newline at end of file
---
> foo
$ echo $?
1
$ sed -e '$a\' test-with-eol.txt > test-still-with-one-eol.txt
$ diff test-with-eol.txt test-still-with-one-eol.txt
$ echo $?
0
share|improve this answer
1  
While this does work, please note that the documentation at your link is misleading. It says '$' denotes the end of file, but in this case $ is matching the end of line (for every line), and is taking advantage of the fact that sed will add the newline if its not there. Actually, removing the . from the regex works fine, too. – jwd Feb 17 '12 at 17:28
1  
A better version, which seems like what the original author was trying for, would be sed -i -e '$a\' file, which really does match only the last line, and is possibly more efficient. – jwd Feb 17 '12 at 17:30
@jwd: From man sed: $ Match the last line. But maybe it works only by accident. Your solution also works. – l0b0 Feb 20 '12 at 11:54
Your solution is also more elegant, and I've tested & committed it, but how can it work? If $ matches the last line, why doesn't it add another newline to a string which already contains a newline? – l0b0 Feb 20 '12 at 12:09
3  
There are two different meanings of $. Inside a regex, such as with the form /<regex>/, it has the usual "match end of line" meaning. Otherwise, used as an address, sed gives it the special "last line in file" meaning. The code works because sed by default appends a newline to its output if it is not already there. The code "$a\" just says "match the last line of the file, and add nothing to it." But implicitly, sed adds the newline to every line it processes (such as this $ line) if it is not already there. – jwd Feb 22 '12 at 19:07
show 2 more comments

Have a look:

$ echo -n foo > foo 
$ cat foo
foo$
$ echo "" >> foo
$ cat foo
foo

so echo "" >> noeol-file should do the trick. (Or did you mean to ask for identifying these files and fixing them?)

edit removed the "" from echo "" >> foo (see @yuyichao's comment) edit2 added the "" again (but see @Keith Thompson's comment)

share|improve this answer
1  
the "" is not necessary (at least for bash) and tail -1 | wc -l can be used to find out the file without a new line at the end – yuyichao Feb 17 '12 at 14:42
@yuyichao: The "" isn't necessary for bash, but I've seen echo implementations that print nothing when invoked without arguments (though none of the ones I can find now do this). echo "" >> noeol-file is probably slightly more robust. printf "\n" >> noeol-file is even more so. – Keith Thompson Feb 17 '12 at 17:17

Another solution using ed. This solution only affect the last line and only if \n is missing:

ed -s file <<< w

It essentially works opening the file for editing through a script, the script is the single w command, that write the file back to disk. It is based on this sentence found in ed(1) man page:

LIMITATIONS
       (...)

       If  a  text (non-binary) file is not terminated by a newline character,
       then ed appends one on reading/writing it.  In the  case  of  a  binary
       file, ed does not append a newline on reading/writing.
share|improve this answer
This does not add a newline for me. – Olhovsky Apr 12 at 1:46

You're better off correcting the editor of the user who last edited the file. If you are the last person to have edited the file - what editor are you using, I'm guessing textmate ..?

share|improve this answer
Vim is the editor in question. But in general, you are right, I should not only fix the symptons ;) – k0pernikus Feb 17 '12 at 13:46
for vim, you have to go out of your way and do the binary-file-on-save dance to get vim to not add a new line at the end of the file - just don't do that dance. OR, to simply correct existing files open them in vim and save the file and vim will 'fix' the missing newline for you (can be easily scripted for multiple files) – AD7six Feb 17 '12 at 13:50
My emacs do not add a newline at end of file. – enzotib Feb 20 '12 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.