Add a newline only if it doesn't exist

前端 未结 8 600
温柔的废话
温柔的废话 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 08:58

    Using awk :

    awk '/^$/{f=1}END{ if (!f) {print "\r"}}1' inputfile
    

    Match blank line ^$(just like you did) and set up a flag. If flag is not set at the end, place newline character.

    Note: that \r is in OS X. Use \n for other.

提交回复
热议问题