Add new line character at the end of file in bash

我是研究僧i 提交于 2020-05-09 18:19:11

问题


How can I use bash command line to add new line character at the end of file named file.txt. I tried use echo but it's not right. Can you show me how can I do it?


回答1:


echo "" >> file.txt

Adds a newline character at the end




回答2:


With sed:

sed -i '' -e '$a\' file.txt

with echo:

echo  >> file.txt



回答3:


Use Vi which automatically creates EOL at EOF on file save.

For example:

vi -escwq foo.txt

or:

ex -scwq foo.txt

To correct multiple files, check: How to fix 'No newline at end of file' for lots of files? at SO




回答4:


This works on centos the empty string above didn't.

sed -i -e '$a\' file



回答5:


GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)

echo \ >> file.txt


来源:https://stackoverflow.com/questions/23055831/add-new-line-character-at-the-end-of-file-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!