Remove empty lines in a text file via grep

后端 未结 11 1597
佛祖请我去吃肉
佛祖请我去吃肉 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:56

    If removing empty lines means lines including any spaces, use:

    grep '\S' FILE
    

    For example:

    $  printf "line1\n\nline2\n \nline3\n\t\nline4\n" > FILE
    $  cat -v FILE
    line1
    
    line2
    
    line3
    
    line4
    $  grep '\S' FILE
    line1
    line2
    line3
    line4
    $  grep . FILE
    line1
    line2
    
    line3
    
    line4
    

    See also:

    • How to remove empty/blank lines (including spaces) in a file in Unix?
    • How to remove blank lines from a file in shell?
    • With sed: Delete empty lines using sed
    • With awk: Remove blank lines using awk

提交回复
热议问题