如何修复大量文件的“文件末尾无换行符"警告? [英] How to fix “No newline at end of file” warning for lots of files?

查看:21
本文介绍了如何修复大量文件的“文件末尾无换行符"警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量的源文件,但最后都没有换行符.

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).

推荐答案

为方便起见,将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆