如何修复许多文件的“文件末尾没有换行符"警告? [英] How to fix “No newline at end of file” warning for lots of files?
问题描述
我有大量的源文件,最后都缺少换行符.
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.
我本质上可能不是在寻找代码,而是可以在Terminal中运行以添加必要的换行符(或某种编程或开发工具)的东西.
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).
推荐答案
为方便起见,将Norman的答案转换为分开的单线.
Converted Norman's answer to a split one-liner for convenience.
for i in * ; do echo $i; \
if diff /dev/null "$i" | tail -1 | \
grep '^\\ No newline' > /dev/null; then echo >> "$i"; \
fi; done
将*替换为所需的任何文件格式,例如*.c
Replace * with whatever file pattern you want, eg *.c
另一个只是告诉您哪些文件已损坏:
And another to just tell you which files are broken:
for i in * ; do \
if diff /dev/null "$i" | tail -1 | \
grep '^\\ No newline' > /dev/null; then echo $i; \
fi; done
这篇关于如何修复许多文件的“文件末尾没有换行符"警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!