Add new line character at the end of file in bash

前端 未结 6 605
旧时难觅i
旧时难觅i 2021-01-31 15:33

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

相关标签:
6条回答
  • 2021-01-31 15:35

    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

    0 讨论(0)
  • 2021-01-31 15:36

    This worked for me.

    sed -i -z 's/$/\n/g' file.txt
    
    0 讨论(0)
  • 2021-01-31 15:38

    With sed:

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

    with echo:

    echo  >> file.txt
    
    0 讨论(0)
  • 2021-01-31 15:39

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

    sed -i -e '$a\' file
    
    0 讨论(0)
  • 2021-01-31 15:47
    echo "" >> file.txt
    

    Adds a newline character at the end

    0 讨论(0)
  • 2021-01-31 16:01

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

    echo \ >> file.txt
    
    0 讨论(0)
提交回复
热议问题