Remove empty lines in a text file via grep

后端 未结 11 1653
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 23:17

FILE:

hello

world

foo

bar

How can I remove all the empty new lines in this FILE?

Output of command:

11条回答
  •  时光取名叫无心
    2020-12-12 23:54

    Perl might be overkill, but it works just as well.

    Removes all lines which are completely blank:

    perl -ne 'print if /./' file
    

    Removes all lines which are completely blank, or only contain whitespace:

    perl -ne 'print if ! /^\s*$/' file
    

    Variation which edits the original and makes a .bak file:

    perl -i.bak -ne 'print if ! /^\s*$/' file
    

提交回复
热议问题