Add a newline only if it doesn't exist

前端 未结 8 619
温柔的废话
温柔的废话 2021-01-31 08:49

I want to add a newline at the end of a file only if it doesn\'t exists, this is to prevent multiple newlines at the end of the file.

I\'m hoping to use sed. Here\'s the

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 09:10

    I solved this task by using dos2unix (or counterparts) with the --newline flag. The advantage is that these tools detect binary files on their own. I like the solution with tail -c1 but filtering binary files beforehand has been really slow for me.

    dos2unix --newline my_file.txt
    

    Eventually I wrote a script that searched my project directory, converted all files to LF (dos2unix) except *.cmd files (CRLF, unix2dos) and used the flag to get the newlines right with one call.

提交回复
热议问题