Removing trailing / starting newlines with sed, awk, tr, and friends

后端 未结 16 665
一个人的身影
一个人的身影 2021-01-30 21:04

I would like to remove all of the empty lines from a file, but only when they are at the end/start of a file (that is, if there are no non-empty lines before them, at the start;

16条回答
  •  一整个雨季
    2021-01-30 21:21

    this can be solved easily with sed -z option

    sed -rz 's/^\n+//; s/\n+$/\n/g' file
    Hello
    
    Welcome to
    Unix and Linux
    

提交回复
热议问题